/**
* Copyright (c) 2009-2011, chunquedong(YangJiandong)
*
* This file is part of ChunMap project
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
*
* History:
* 2010-05-05 Jed Young Creation
*/
package chunmap.view.render;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import chunmap.data.feature.Feature;
import chunmap.data.feature.Raster;
import chunmap.data.feature.Shape;
import chunmap.model.elem.Envelope;
import chunmap.model.geom.GeoPoint;
import chunmap.model.geom.Geometry;
import chunmap.model.geom.GeometryCollection;
import chunmap.model.geom.LineString;
import chunmap.model.geom.Polygon;
import chunmap.view.View;
public class GeneralSymbol extends Symbol{
private Style2D style;
private DrawHelper drawHelper = new DrawHelper();
private Graphics2D g;
private View view;
public GeneralSymbol(){
this(new Style2D());
}
public GeneralSymbol(Style2D style){
this.style=style;
drawHelper.setStyle(style);
}
//可能同时被用来绘图,会出现串色现象
@Override
public synchronized void draw(Graphics g, Feature data, View view) {
if (!style.isVisible())return;
this.g=(Graphics2D)g;
this.view=view;
if(data instanceof Shape){
Shape shp=(Shape)data;
drawShape(shp);
}else if(data instanceof Raster){
Raster shp=(Raster)data;
drawRaster(shp);
}
}
private void drawRaster(Raster tile) {
// draw raster
Image image=tile.getImage();
Envelope env = tile.getEnvelop().transform(view.world2Screen());
if ( image!= null) {
double x=(int)(env.getMinX()-0.5);
double y=(int)(env.getMinY()-0.5);
double w=env.getWidth()+0.5;
double h=env.getHeight()+0.5;
drawHelper.drawImage(g, image, (int)x , (int) y, (int)w ,(int)h);
// drawHelper.drawLineString(g, env.toPolygon().getShell(),
// (Style2D) defaultStyle);
}else{
g.setColor(Color.black);
g.drawString("no data. engine by Chun-Map"
, (int)env.getCenter().getX(), (int)env.getCenter().getY());
}
}
private void drawShape(Shape shape) {
Geometry geo = shape.getGeometry();
if (geo == null) return;
draw(geo);
}
// -----------------------------------------------------------画几何体
private void draw(Geometry geo) {
selectDraw(geo);
}
private void selectDraw(Geometry geo) {
if (geo instanceof GeoPoint) {
drawHelper.drawPoint(g, geo,view);
} else if (geo instanceof LineString) {
drawHelper.drawLineString(g, geo,view);
} else if (geo instanceof Polygon) {
drawHelper.drawPolygon(g, geo,view);
} else if (geo instanceof GeometryCollection) {
GeometryCollection geoCollection = (GeometryCollection) geo;
for (Geometry geo2 : geoCollection) {
draw(geo2);
}
} else {
throw new UnsupportedOperationException();
}
}
}