svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / geom / operation / DrawInts.java @ 37180
History | View | Annotate | Download (4.92 KB)
1 |
package org.gvsig.fmap.geom.operation; |
---|---|
2 |
|
3 |
import java.awt.Graphics2D; |
4 |
import java.awt.geom.AffineTransform; |
5 |
import java.awt.geom.PathIterator; |
6 |
|
7 |
import org.gvsig.fmap.geom.Geometry; |
8 |
import org.gvsig.fmap.geom.GeometryLocator; |
9 |
import org.gvsig.fmap.geom.GeometryManager; |
10 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
11 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
12 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
13 |
import org.gvsig.fmap.mapcontext.ViewPort; |
14 |
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport; |
15 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
16 |
import org.gvsig.tools.task.Cancellable; |
17 |
import org.slf4j.Logger; |
18 |
import org.slf4j.LoggerFactory; |
19 |
|
20 |
public class DrawInts extends GeometryOperation { |
21 |
private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
22 |
public static final String NAME = "drawInts"; |
23 |
public static int CODE = Integer.MIN_VALUE; |
24 |
|
25 |
final static private Logger logger = LoggerFactory.getLogger(DrawInts.class); |
26 |
|
27 |
public Object invoke(Geometry geom, GeometryOperationContext ctx) throws GeometryOperationException{ |
28 |
DrawOperationContext doc=(DrawOperationContext)ctx; |
29 |
ViewPort viewPort = doc.getViewPort(); |
30 |
ISymbol symbol = doc.getSymbol(); |
31 |
Graphics2D g=doc.getGraphics();
|
32 |
Cancellable cancel=doc.getCancellable(); |
33 |
// make the symbol to resize itself with the current rendering context
|
34 |
try {
|
35 |
if (doc.hasDPI()){
|
36 |
double previousSize = ((CartographicSupport)symbol).
|
37 |
toCartographicSize(viewPort, doc.getDPI(), geom); |
38 |
// draw it as normally
|
39 |
Geometry decimatedShape = transformToInts(geom, viewPort.getAffineTransform()); |
40 |
|
41 |
//the AffineTransform has to be null because the transformToInts method
|
42 |
//reprojects the geometry
|
43 |
symbol.draw(g, null, decimatedShape, doc.getFeature(), cancel);
|
44 |
|
45 |
// restore previous size
|
46 |
((CartographicSupport)symbol).setCartographicSize(previousSize, geom); |
47 |
}else{
|
48 |
Geometry decimatedShape = transformToInts(geom, viewPort.getAffineTransform()); |
49 |
symbol.draw(g, viewPort.getAffineTransform(), decimatedShape, |
50 |
doc.getFeature(), cancel); |
51 |
} |
52 |
} catch (CreateGeometryException e) {
|
53 |
e.printStackTrace(); |
54 |
throw new GeometryOperationException(e); |
55 |
} |
56 |
|
57 |
return null; |
58 |
} |
59 |
|
60 |
public int getOperationIndex() { |
61 |
return CODE;
|
62 |
} |
63 |
|
64 |
private Geometry transformToInts(Geometry gp, AffineTransform at) throws CreateGeometryException { |
65 |
GeneralPathX newGp = new GeneralPathX();
|
66 |
double[] theData = new double[6]; |
67 |
double[] aux = new double[6]; |
68 |
|
69 |
// newGp.reset();
|
70 |
PathIterator theIterator;
|
71 |
int theType;
|
72 |
int numParts = 0; |
73 |
|
74 |
java.awt.geom.Point2D ptDst = new java.awt.geom.Point2D.Double();
|
75 |
java.awt.geom.Point2D ptSrc = new java.awt.geom.Point2D.Double();
|
76 |
boolean bFirst = true; |
77 |
int xInt, yInt, antX = -1, antY = -1; |
78 |
|
79 |
|
80 |
theIterator = gp.getPathIterator(null); //, flatness); |
81 |
int numSegmentsAdded = 0; |
82 |
while (!theIterator.isDone()) {
|
83 |
theType = theIterator.currentSegment(theData); |
84 |
|
85 |
switch (theType) {
|
86 |
case PathIterator.SEG_MOVETO: |
87 |
numParts++; |
88 |
ptSrc.setLocation(theData[0], theData[1]); |
89 |
at.transform(ptSrc, ptDst); |
90 |
antX = (int) ptDst.getX();
|
91 |
antY = (int) ptDst.getY();
|
92 |
newGp.moveTo(antX, antY); |
93 |
numSegmentsAdded++; |
94 |
bFirst = true;
|
95 |
break;
|
96 |
|
97 |
case PathIterator.SEG_LINETO: |
98 |
ptSrc.setLocation(theData[0], theData[1]); |
99 |
at.transform(ptSrc, ptDst); |
100 |
xInt = (int) ptDst.getX();
|
101 |
yInt = (int) ptDst.getY();
|
102 |
if ((bFirst) || ((xInt != antX) || (yInt != antY)))
|
103 |
{ |
104 |
newGp.lineTo(xInt, yInt); |
105 |
antX = xInt; |
106 |
antY = yInt; |
107 |
bFirst = false;
|
108 |
numSegmentsAdded++; |
109 |
} |
110 |
break;
|
111 |
|
112 |
case PathIterator.SEG_QUADTO: |
113 |
at.transform(theData,0,aux,0,2); |
114 |
newGp.quadTo(aux[0], aux[1], aux[2], aux[3]); |
115 |
numSegmentsAdded++; |
116 |
break;
|
117 |
|
118 |
case PathIterator.SEG_CUBICTO: |
119 |
at.transform(theData,0,aux,0,3); |
120 |
newGp.curveTo(aux[0], aux[1], aux[2], aux[3], aux[4], aux[5]); |
121 |
numSegmentsAdded++; |
122 |
break;
|
123 |
|
124 |
case PathIterator.SEG_CLOSE: |
125 |
if (numSegmentsAdded < 3) { |
126 |
newGp.lineTo(antX, antY); |
127 |
} |
128 |
newGp.closePath(); |
129 |
|
130 |
break;
|
131 |
} //end switch
|
132 |
|
133 |
theIterator.next(); |
134 |
} //end while loop
|
135 |
|
136 |
Geometry geom = null;
|
137 |
switch (gp.getType()) {
|
138 |
case Geometry.TYPES.POINT:
|
139 |
geom = geomManager.createPoint(ptDst.getX(), ptDst.getY(), SUBTYPES.GEOM2D); |
140 |
break;
|
141 |
case Geometry.TYPES.CURVE:
|
142 |
case Geometry.TYPES.ARC:
|
143 |
geom = geomManager.createCurve(newGp, SUBTYPES.GEOM2D); |
144 |
break;
|
145 |
|
146 |
case Geometry.TYPES.SURFACE:
|
147 |
case Geometry.TYPES.CIRCLE:
|
148 |
case Geometry.TYPES.ELLIPSE:
|
149 |
|
150 |
geom = geomManager.createSurface(newGp, SUBTYPES.GEOM2D); |
151 |
break;
|
152 |
} |
153 |
return geom;
|
154 |
} |
155 |
|
156 |
public static void register() { |
157 |
|
158 |
GeometryManager gm = GeometryLocator.getGeometryManager(); |
159 |
CODE = gm.getGeometryOperationCode(NAME); |
160 |
geomManager.registerGeometryOperation(NAME, new DrawInts());
|
161 |
} |
162 |
|
163 |
|
164 |
} |