svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / rendering / symbols / FGraphicUtilities.java @ 28879
History | View | Annotate | Download (27.4 KB)
1 | 21200 | vcaballero | /*
|
---|---|---|---|
2 | * Created on 28-abr-2004
|
||
3 | *
|
||
4 | * To change the template for this generated file go to
|
||
5 | * Window>Preferences>Java>Code Generation>Code and Comments
|
||
6 | */
|
||
7 | /* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
|
||
8 | *
|
||
9 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
10 | *
|
||
11 | * This program is free software; you can redistribute it and/or
|
||
12 | * modify it under the terms of the GNU General Public License
|
||
13 | * as published by the Free Software Foundation; either version 2
|
||
14 | * of the License, or (at your option) any later version.
|
||
15 | *
|
||
16 | * This program is distributed in the hope that it will be useful,
|
||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
19 | * GNU General Public License for more details.
|
||
20 | *
|
||
21 | * You should have received a copy of the GNU General Public License
|
||
22 | * along with this program; if not, write to the Free Software
|
||
23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
24 | *
|
||
25 | * For more information, contact:
|
||
26 | *
|
||
27 | * Generalitat Valenciana
|
||
28 | * Conselleria d'Infraestructures i Transport
|
||
29 | * Av. Blasco Ib��ez, 50
|
||
30 | * 46010 VALENCIA
|
||
31 | * SPAIN
|
||
32 | *
|
||
33 | * +34 963862235
|
||
34 | * gvsig@gva.es
|
||
35 | * www.gvsig.gva.es
|
||
36 | *
|
||
37 | * or
|
||
38 | *
|
||
39 | * IVER T.I. S.A
|
||
40 | * Salamanca 50
|
||
41 | * 46005 Valencia
|
||
42 | * Spain
|
||
43 | *
|
||
44 | * +34 963163400
|
||
45 | * dac@iver.es
|
||
46 | */
|
||
47 | package org.gvsig.fmap.mapcontext.rendering.symbols; |
||
48 | |||
49 | import java.awt.Color; |
||
50 | import java.awt.Font; |
||
51 | import java.awt.FontMetrics; |
||
52 | import java.awt.Graphics2D; |
||
53 | import java.awt.Rectangle; |
||
54 | import java.awt.geom.AffineTransform; |
||
55 | import java.awt.geom.Point2D; |
||
56 | |||
57 | import org.apache.batik.ext.awt.geom.PathLength; |
||
58 | import org.gvsig.fmap.geom.Geometry; |
||
59 | 27414 | jpiera | import org.gvsig.fmap.geom.GeometryLocator; |
60 | import org.gvsig.fmap.geom.GeometryManager; |
||
61 | import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
||
62 | import org.gvsig.fmap.geom.exception.CreateGeometryException; |
||
63 | 21200 | vcaballero | import org.gvsig.fmap.geom.handler.Handler; |
64 | import org.gvsig.fmap.geom.primitive.GeneralPathX; |
||
65 | import org.gvsig.fmap.geom.util.Converter; |
||
66 | 27024 | jpiera | import org.gvsig.fmap.geom.util.UtilFunctions; |
67 | 22025 | jmvivo | import org.gvsig.fmap.geom.utils.FLabel; |
68 | 21200 | vcaballero | import org.gvsig.fmap.mapcontext.MapContext; |
69 | 27414 | jpiera | import org.slf4j.Logger; |
70 | import org.slf4j.LoggerFactory; |
||
71 | 21200 | vcaballero | |
72 | import com.vividsolutions.jts.geom.Point; |
||
73 | |||
74 | /**
|
||
75 | * Clase con m�todos est�ticos para dibujar sobre el Graphics que se les pase
|
||
76 | * como par�metro.
|
||
77 | *
|
||
78 | * Esta clase deber�a ser privada. Las clases que la usan son GraphicLayer
|
||
79 | * y AnnotationStrategy, pero hay que revisarlas para que no sea necesario.
|
||
80 | * Lo m�s urgente ser�a lo del dibujado de textos, para que sea
|
||
81 | * Hay que quitar las dependecias de FSymbol, y trabajar SIEMPRE con ISymbol.
|
||
82 | * Recordar: Que sea ISymbol el que renderiza.
|
||
83 | * extensible el s�mbolo a usar. NOTA: Ver tambi�n comentario en ISymbol
|
||
84 | *
|
||
85 | * @author fjp
|
||
86 | */
|
||
87 | public class FGraphicUtilities { |
||
88 | 27414 | jpiera | private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
89 | private static final Logger logger = LoggerFactory.getLogger(FGraphicUtilities.class); |
||
90 | 21200 | vcaballero | |
91 | /**
|
||
92 | * Dibuja el s�mbolo que se le pasa como par�metro en el Graphics.
|
||
93 | *
|
||
94 | * @param g2 Graphics2D sobre el que dibujar.
|
||
95 | * @param mT2 Matriz de transformaci�n.
|
||
96 | * @param r Rect�ngulo.
|
||
97 | * @param symbol S�mbolo a dibujar.
|
||
98 | 27414 | jpiera | * @throws CreateGeometryException
|
99 | 21200 | vcaballero | */
|
100 | public static void DrawSymbol(Graphics2D g2, AffineTransform mT2, |
||
101 | 27414 | jpiera | Rectangle r, FSymbol symbol) throws CreateGeometryException { |
102 | 21200 | vcaballero | Geometry geom; |
103 | |||
104 | AffineTransform mT = new AffineTransform(); |
||
105 | mT.setToIdentity(); |
||
106 | |||
107 | Rectangle r2 = new Rectangle(r.x + 2 + (r.width / 3), r.y, r.width / 3, |
||
108 | r.height); |
||
109 | Rectangle r3 = new Rectangle(r.x + 2 + ((2 * r.width) / 3), r.y, |
||
110 | r.width / 3, r.height);
|
||
111 | |||
112 | // g2.clearRect(r.x, r.y, r.width, r.height);
|
||
113 | // System.out.println("r = " + r.toString() + " Color preview:" + symbol.m_Color.toString());
|
||
114 | // System.out.println("symbol.m_symbolType= "+symbol.m_symbolType);
|
||
115 | switch (symbol.getSymbolType()) {
|
||
116 | case Geometry.TYPES.POINT:
|
||
117 | 27414 | jpiera | geom = geomManager.createPoint(r.x + (r.width / 2), r.y + (r.height / 2), SUBTYPES.GEOM2D); |
118 | 21200 | vcaballero | |
119 | // Para no tener que clonarlo si viene en unidades de mapa
|
||
120 | boolean bAux2 = symbol.isSizeInPixels();
|
||
121 | int alturaMetros = symbol.getSize(); // Nota: Cambiar m_Size a float |
||
122 | |||
123 | if (!bAux2) {
|
||
124 | symbol.setSizeInPixels(true);
|
||
125 | symbol.setSize(8); // tama�o fijo |
||
126 | } |
||
127 | |||
128 | symbol.draw(g2, mT, geom, null);
|
||
129 | // FGraphicUtilities.DrawShape(g2, mT, shp, symbol);
|
||
130 | |||
131 | |||
132 | if (!bAux2) {
|
||
133 | symbol.setSize(alturaMetros); |
||
134 | symbol.setSizeInPixels(bAux2); |
||
135 | } |
||
136 | |||
137 | if (symbol.getFont() != null) { |
||
138 | // Para no tener que clonarlo si viene en unidades de mapa
|
||
139 | boolean bAux = symbol.isFontSizeInPixels();
|
||
140 | symbol.setFontSizeInPixels(true);
|
||
141 | FGraphicUtilities.DrawLabel(g2, mT, geom, symbol, |
||
142 | new FLabel("Abcd")); |
||
143 | symbol.setFontSizeInPixels(bAux); |
||
144 | } |
||
145 | |||
146 | break;
|
||
147 | |||
148 | case Geometry.TYPES.CURVE:
|
||
149 | |||
150 | Rectangle rect = mT2.createTransformedShape(r).getBounds();
|
||
151 | GeneralPathX line = new GeneralPathX();
|
||
152 | line.moveTo(rect.x, rect.y + (rect.height / 2));
|
||
153 | |||
154 | // line.lineTo(rect.x + rect.width/3, rect.y + rect.height);
|
||
155 | // line.lineTo(rect.x + 2*rect.width/3, rect.y);
|
||
156 | // line.lineTo(rect.x + rect.width, rect.y + rect.height/2);
|
||
157 | line.curveTo(rect.x + (rect.width / 3),
|
||
158 | rect.y + (2 * rect.height),
|
||
159 | rect.x + ((2 * rect.width) / 3), rect.y - rect.height, |
||
160 | rect.x + rect.width, rect.y + (rect.height / 2));
|
||
161 | |||
162 | 27414 | jpiera | geom = geomManager.createSurface(line, SUBTYPES.GEOM2D); |
163 | 21200 | vcaballero | symbol.draw(g2, mT, geom, null);
|
164 | // FGraphicUtilities.DrawShape(g2, mT, shp, symbol);
|
||
165 | |||
166 | break;
|
||
167 | |||
168 | case Geometry.TYPES.SURFACE:
|
||
169 | |||
170 | GeneralPathX rectAux = new GeneralPathX(r);
|
||
171 | rectAux.transform(mT2); |
||
172 | 27414 | jpiera | geom = geomManager.createSurface(rectAux, SUBTYPES.GEOM2D); |
173 | 21200 | vcaballero | |
174 | // System.out.println("rect = "+rectAux.getBounds());
|
||
175 | symbol.draw(g2, mT, geom, null);
|
||
176 | // FGraphicUtilities.DrawShape(g2, mT, shp, symbol);
|
||
177 | |||
178 | break;
|
||
179 | |||
180 | 22034 | vcaballero | case Geometry.TYPES.GEOMETRY:
|
181 | 21200 | vcaballero | |
182 | // Pol�gono
|
||
183 | r.setSize(r.width / 3, r.height);
|
||
184 | |||
185 | GeneralPathX rectAux2 = new GeneralPathX(r);
|
||
186 | rectAux2.transform(mT2); |
||
187 | 27414 | jpiera | geom = geomManager.createSurface(rectAux2, SUBTYPES.GEOM2D); |
188 | 21200 | vcaballero | symbol.draw(g2, mT, geom, null);
|
189 | // FGraphicUtilities.DrawShape(g2, mT, shp, symbol);
|
||
190 | |||
191 | // L�nea
|
||
192 | rect = mT2.createTransformedShape(r2).getBounds(); |
||
193 | line = new GeneralPathX();
|
||
194 | line.moveTo(rect.x, rect.y + (rect.height / 2));
|
||
195 | |||
196 | line.curveTo(rect.x + (rect.width / 3),
|
||
197 | rect.y + (2 * rect.height),
|
||
198 | rect.x + ((2 * rect.width) / 3), rect.y - rect.height, |
||
199 | rect.x + rect.width, rect.y + (rect.height / 2));
|
||
200 | |||
201 | 27414 | jpiera | geom = geomManager.createCurve(line, SUBTYPES.GEOM2D); |
202 | 21200 | vcaballero | symbol.draw(g2, mT, geom, null);
|
203 | // FGraphicUtilities.DrawShape(g2, mT, shp, symbol);
|
||
204 | |||
205 | // Punto:
|
||
206 | 27414 | jpiera | geom = geomManager.createPoint(r3.x + (r3.width / 2), r3.y +
|
207 | (r3.height / 2), SUBTYPES.GEOM2D);
|
||
208 | 21200 | vcaballero | |
209 | // Para no tener que clonarlo si viene en unidades de mapa
|
||
210 | bAux2 = symbol.isSizeInPixels(); |
||
211 | alturaMetros = symbol.getSize(); // Nota: Cambiar m_Size a float
|
||
212 | |||
213 | if (!bAux2) {
|
||
214 | symbol.setSizeInPixels(true);
|
||
215 | symbol.setSize(4); // tama�o fijo |
||
216 | } |
||
217 | symbol.draw(g2, mT, geom, null);
|
||
218 | // FGraphicUtilities.DrawShape(g2, mT, shp, symbol);
|
||
219 | |||
220 | if (!bAux2) {
|
||
221 | symbol.setSize(alturaMetros); |
||
222 | symbol.setSizeInPixels(bAux2); |
||
223 | } |
||
224 | |||
225 | if (symbol.getFont() != null) { |
||
226 | // Para no tener que clonarlo si viene en unidades de mapa
|
||
227 | boolean bAux = symbol.isFontSizeInPixels();
|
||
228 | symbol.setFontSizeInPixels(true);
|
||
229 | FGraphicUtilities.DrawLabel(g2, mT, geom, symbol, |
||
230 | new FLabel("Abcd")); |
||
231 | symbol.setFontSizeInPixels(bAux); |
||
232 | } |
||
233 | |||
234 | break;
|
||
235 | case Geometry.TYPES.TEXT:
|
||
236 | 27414 | jpiera | geom = geomManager.createPoint(r3.x + (r3.width / 2), r3.y +
|
237 | (r3.height / 2), SUBTYPES.GEOM2D);
|
||
238 | 21200 | vcaballero | boolean bAux = symbol.isFontSizeInPixels();
|
239 | symbol.setFontSizeInPixels(true);
|
||
240 | FGraphicUtilities.DrawLabel(g2, mT, geom, symbol, |
||
241 | new FLabel("Abcd")); |
||
242 | symbol.setFontSizeInPixels(bAux); |
||
243 | break;
|
||
244 | } |
||
245 | } |
||
246 | |||
247 | /**
|
||
248 | * Dibuja el shape que se pasa como par�metro con las caracter�sticas que
|
||
249 | * aporta el s�mbolo sobre el Graphics2D.
|
||
250 | *
|
||
251 | * @param g2 Graphics2D sobre el que dibujar.
|
||
252 | * @param mT Matriz de transformaci�n.
|
||
253 | * @param shp FShape a dibujar.
|
||
254 | * @param theSymbol S�mbolo.
|
||
255 | * @deprecated
|
||
256 | */
|
||
257 | public static void DrawShape(Graphics2D g2, AffineTransform mT, Geometry geom, |
||
258 | FSymbol theSymbol) { |
||
259 | // Hacemos la transformaci�n del shape aqu� dentro... por ahora.
|
||
260 | if (geom == null || theSymbol == null || (!theSymbol.isShapeVisible())) { |
||
261 | return;
|
||
262 | } |
||
263 | g2.setColor(theSymbol.getColor()); |
||
264 | |||
265 | /* if (shp instanceof FPolygon2D)
|
||
266 | {
|
||
267 | System.out.println("Entra pol�gono");
|
||
268 | } */
|
||
269 | int type=geom.getType();
|
||
270 | /* if (shp.getShapeType()>=FShape.Z){
|
||
271 | type=shp.getShapeType()-FShape.Z;
|
||
272 | } */
|
||
273 | switch (type) {
|
||
274 | 26909 | jpiera | case Geometry.TYPES.POINT: //Tipo punto |
275 | 27024 | jpiera | drawSymbolPoint(g2, mT, (org.gvsig.fmap.geom.primitive.Point) geom, theSymbol); |
276 | 21200 | vcaballero | |
277 | break;
|
||
278 | |||
279 | 26909 | jpiera | case Geometry.TYPES.CURVE:
|
280 | case Geometry.TYPES.ARC:
|
||
281 | 21200 | vcaballero | // Shape theShp = mT.createTransformedShape(shp.m_Polyline);
|
282 | // g2.setColor(theSymbol.m_Color);
|
||
283 | if (theSymbol.getStroke() != null) { |
||
284 | g2.setStroke(theSymbol.getStroke()); |
||
285 | } |
||
286 | |||
287 | g2.draw(geom); |
||
288 | |||
289 | break;
|
||
290 | |||
291 | case Geometry.TYPES.SURFACE:
|
||
292 | 26909 | jpiera | case Geometry.TYPES.ELLIPSE:
|
293 | case Geometry.TYPES.CIRCLE:
|
||
294 | |||
295 | 21200 | vcaballero | if (theSymbol.getFill() != null) |
296 | g2.setPaint(theSymbol.getFill()); |
||
297 | |||
298 | if (theSymbol.getColor() != null) |
||
299 | 21298 | vcaballero | if (theSymbol.getStyle() != FSymbol.SYMBOL_STYLE_DGNSPECIAL) {
|
300 | 21200 | vcaballero | g2.fill(geom); |
301 | } |
||
302 | |||
303 | if (theSymbol.isOutlined()) {
|
||
304 | g2.setColor(theSymbol.getOutlineColor()); |
||
305 | |||
306 | if (theSymbol.getStroke() != null) { |
||
307 | g2.setStroke(theSymbol.getStroke()); |
||
308 | } |
||
309 | |||
310 | g2.draw(geom); |
||
311 | } |
||
312 | |||
313 | break;
|
||
314 | |||
315 | } |
||
316 | } |
||
317 | // public static double toMapDistance(AffineTransform at,int d) {
|
||
318 | // double dist = d / at.getScaleX();
|
||
319 | //
|
||
320 | // return dist;
|
||
321 | // }
|
||
322 | // public static int fromMapDistance(AffineTransform at,double d) {
|
||
323 | // Point2D.Double pWorld = new Point2D.Double(1, 1);
|
||
324 | // Point2D.Double pScreen = new Point2D.Double();
|
||
325 | //
|
||
326 | // try {
|
||
327 | // at.deltaTransform(pWorld, pScreen);
|
||
328 | // } catch (Exception e) {
|
||
329 | // System.err.print(e.getMessage());
|
||
330 | // }
|
||
331 | //
|
||
332 | // return (int) (d * pScreen.x);
|
||
333 | // }
|
||
334 | /**
|
||
335 | * Dibuja el FLabel que se pasa como par�metro sobre el Graphics2D.
|
||
336 | *
|
||
337 | * @param g2 Graphics2D sobre el que dibujar.
|
||
338 | * @param mT Matriz de transformaci�n.
|
||
339 | * @param shp FShape a dibujar.
|
||
340 | * @param theSymbol S�mbolo para aplicar.
|
||
341 | * @param theLabel FLabel que contiene el texto que se debe dibujar.
|
||
342 | */
|
||
343 | public static void DrawLabel(Graphics2D g2, AffineTransform mT, Geometry geom, |
||
344 | FSymbol theSymbol, FLabel theLabel) { |
||
345 | float angle;
|
||
346 | float x;
|
||
347 | float y;
|
||
348 | Point2D pAux = null; |
||
349 | |||
350 | // USAR TEXTLAYOUT SI QUEREMOS PERMITIR SELECCIONAR UN TEXTO
|
||
351 | // Y/O EDITARLO "IN SITU"
|
||
352 | |||
353 | /* if (m_labelValues[numReg].length() > 0)
|
||
354 | {
|
||
355 | TextLayout layout = new TextLayout(m_labelValues[numReg], font, frc);
|
||
356 | layout.draw(g2, x, y);
|
||
357 | } */
|
||
358 | if (geom == null) { |
||
359 | return;
|
||
360 | } |
||
361 | |||
362 | // Las etiquetas que pongamos a nulo ser� porque no la queremos dibujar.
|
||
363 | // �til para cuando queramos eliminar duplicados.
|
||
364 | if (theLabel.getString() == null) { |
||
365 | return;
|
||
366 | } |
||
367 | |||
368 | FontMetrics metrics;// = g2.getFontMetrics(); |
||
369 | int width;// = metrics.stringWidth(theLabel.getString()); |
||
370 | int height;// = metrics.getMaxAscent(); |
||
371 | |||
372 | // int height = metrics.getHeight();
|
||
373 | //g2.setFont(theSymbol.getFont());
|
||
374 | //g2.setColor(theSymbol.getFontColor());
|
||
375 | |||
376 | // Aqu� hay que mirar m_Size y m_useSize...
|
||
377 | if (!theSymbol.isFontSizeInPixels()) {
|
||
378 | // Suponemos que m_Size viene en coordenadas de mundo real
|
||
379 | // Esto habr� que cambiarlo. Probablemente usar Style2d de geotools en lugar
|
||
380 | // de FSymbol.
|
||
381 | // CAMBIO: La altura del texto la miramos en FLabel
|
||
382 | // float alturaPixels = (float) (theSymbol.m_FontSize * mT.getScaleX());
|
||
383 | float alturaPixels = (float) (theLabel.getHeight() * mT.getScaleX()); |
||
384 | |||
385 | /* System.out.println("m_bUseSize = " + theSymbol.m_bUseSize +
|
||
386 | " Escala: " + mT.getScaleX() + " alturaPixels = " + alturaPixels); */
|
||
387 | if (alturaPixels < 3) { |
||
388 | return; // No leemos nada |
||
389 | } |
||
390 | |||
391 | 27676 | csanchez | Font nuevaFuente = theSymbol.getFont().deriveFont(theSymbol.getFont().getStyle(),alturaPixels);
|
392 | 21200 | vcaballero | g2.setFont(nuevaFuente); |
393 | g2.setColor(theSymbol.getFontColor()); |
||
394 | metrics=g2.getFontMetrics(); |
||
395 | height= metrics.getMaxAscent(); |
||
396 | width = metrics.stringWidth(theLabel.getString()); |
||
397 | }else {
|
||
398 | metrics = g2.getFontMetrics(); |
||
399 | width = metrics.stringWidth(theLabel.getString()); |
||
400 | height = metrics.getMaxAscent(); |
||
401 | g2.setFont(theSymbol.getFont()); |
||
402 | g2.setColor(theSymbol.getFontColor()); |
||
403 | } |
||
404 | int type=geom.getType();
|
||
405 | switch (type) {
|
||
406 | case Geometry.TYPES.POINT: //Tipo punto |
||
407 | 27024 | jpiera | pAux = new Point2D.Double(((org.gvsig.fmap.geom.primitive.Point) geom).getX(), |
408 | ((org.gvsig.fmap.geom.primitive.Point) geom).getY()); |
||
409 | 21200 | vcaballero | pAux = mT.transform(pAux, null);
|
410 | |||
411 | break;
|
||
412 | |||
413 | case Geometry.TYPES.CURVE:
|
||
414 | |||
415 | //
|
||
416 | if (theLabel.getOrig() == null) // Calculamos el punto y la orientaci�n solo la primera vez |
||
417 | { |
||
418 | PathLength pathLen = new PathLength(geom);
|
||
419 | |||
420 | // if (pathLen.lengthOfPath() < width / mT.getScaleX()) return;
|
||
421 | float midDistance = pathLen.lengthOfPath() / 2; |
||
422 | pAux = pathLen.pointAtLength(midDistance); |
||
423 | angle = pathLen.angleAtLength(midDistance); |
||
424 | |||
425 | if (angle < 0) { |
||
426 | angle = angle + (float) (2 * Math.PI); |
||
427 | } |
||
428 | |||
429 | if ((angle > (Math.PI / 2)) && |
||
430 | (angle < ((3 * Math.PI) / 2))) { |
||
431 | angle = angle - (float) Math.PI; |
||
432 | } |
||
433 | |||
434 | theLabel.setRotation(Math.toDegrees(angle));
|
||
435 | theLabel.setOrig(pAux); |
||
436 | } |
||
437 | |||
438 | pAux = mT.transform(theLabel.getOrig(), null);
|
||
439 | |||
440 | // pAux = theLabel.getOrig();
|
||
441 | // GlyphVector theGlyphs = theSymbol.m_Font.createGlyphVector(g2.getFontRenderContext(), theLabel);
|
||
442 | // Shape txtShp = TextPathLayout.layoutGlyphVector(theGlyphs, shp.m_Polyline,TextPathLayout.ALIGN_MIDDLE);
|
||
443 | // g2.draw(txtShp);
|
||
444 | // System.out.println("Pintando etiqueta " + theLabel );
|
||
445 | break;
|
||
446 | |||
447 | case Geometry.TYPES.SURFACE:
|
||
448 | |||
449 | if (theLabel.getOrig() == null) // Calculamos el punto solo la primera vez |
||
450 | { |
||
451 | 22782 | vcaballero | com.vividsolutions.jts.geom.Geometry geo = Converter.geometryToJts(geom); |
452 | 21200 | vcaballero | |
453 | // System.out.println("Area de " + m_labelValues[numReg] + " = "
|
||
454 | // + geo.getArea());
|
||
455 | // System.out.println(geo.toText());
|
||
456 | Point pJTS = geo.getInteriorPoint();
|
||
457 | 22782 | vcaballero | Geometry pLabel = (Geometry)Converter.jtsToGeometry(pJTS); |
458 | 21200 | vcaballero | theLabel.setRotation(0);
|
459 | theLabel.setOrig(new Point2D.Double( |
||
460 | 27024 | jpiera | ((org.gvsig.fmap.geom.primitive.Point) pLabel).getX(), |
461 | ((org.gvsig.fmap.geom.primitive.Point) pLabel).getX())); |
||
462 | 21200 | vcaballero | } |
463 | |||
464 | pAux = mT.transform(theLabel.getOrig(), null);
|
||
465 | |||
466 | break;
|
||
467 | } |
||
468 | |||
469 | AffineTransform ant = g2.getTransform();
|
||
470 | |||
471 | x = (float) pAux.getX();
|
||
472 | y = (float) pAux.getY();
|
||
473 | |||
474 | AffineTransform Tx = (AffineTransform) ant.clone(); |
||
475 | Tx.translate(x, y); // S3: final translation
|
||
476 | Tx.rotate(theLabel.getRotation()); // S2: rotate around anchor
|
||
477 | g2.setTransform(Tx); |
||
478 | |||
479 | switch (theLabel.getJustification()) {
|
||
480 | case FLabel.LEFT_BOTTOM:
|
||
481 | g2.drawString(theLabel.getString(), 0, 0 - 3); |
||
482 | |||
483 | break;
|
||
484 | |||
485 | case FLabel.LEFT_CENTER:
|
||
486 | g2.drawString(theLabel.getString(), 0, 0 - (height / 2)); |
||
487 | |||
488 | break;
|
||
489 | |||
490 | case FLabel.LEFT_TOP:
|
||
491 | g2.drawString(theLabel.getString(), 0, 0 - height); |
||
492 | |||
493 | break;
|
||
494 | |||
495 | case FLabel.CENTER_BOTTOM:
|
||
496 | g2.drawString(theLabel.getString(), 0 - (width / 2), 0 - 3); |
||
497 | |||
498 | break;
|
||
499 | |||
500 | case FLabel.CENTER_CENTER:
|
||
501 | g2.drawString(theLabel.getString(), 0 - (width / 2), |
||
502 | 0 - (height / 2)); |
||
503 | |||
504 | break;
|
||
505 | |||
506 | case FLabel.CENTER_TOP:
|
||
507 | g2.drawString(theLabel.getString(), 0 - (width / 2), 0 - |
||
508 | height); |
||
509 | |||
510 | break;
|
||
511 | |||
512 | case FLabel.RIGHT_BOTTOM:
|
||
513 | g2.drawString(theLabel.getString(), 0 - width, 0 - 3); |
||
514 | |||
515 | break;
|
||
516 | |||
517 | case FLabel.RIGHT_CENTER:
|
||
518 | g2.drawString(theLabel.getString(), 0 - width, 0 - |
||
519 | (height / 2));
|
||
520 | |||
521 | break;
|
||
522 | |||
523 | case FLabel.RIGHT_TOP:
|
||
524 | g2.drawString(theLabel.getString(), 0 - width, 0 - height); |
||
525 | |||
526 | break;
|
||
527 | } |
||
528 | |||
529 | // Restauramos
|
||
530 | g2.setTransform(ant); |
||
531 | } |
||
532 | /**
|
||
533 | * Dibuja el FLabel que se pasa como par�metro sobre el Graphics2D.
|
||
534 | *
|
||
535 | * @param g2 Graphics2D sobre el que dibujar.
|
||
536 | * @param mT Matriz de transformaci�n.
|
||
537 | * @param shp FShape a dibujar.
|
||
538 | * @param theSymbol S�mbolo para aplicar.
|
||
539 | * @param theLabel FLabel que contiene el texto que se debe dibujar.
|
||
540 | */
|
||
541 | public static void DrawAnnotation(Graphics2D g2, AffineTransform at, |
||
542 | FSymbol theSymbol, FLabel theLabel,FontMetrics metrics,boolean isSelected) { |
||
543 | float x;
|
||
544 | float y;
|
||
545 | Point2D pAux = null; |
||
546 | // Las etiquetas que pongamos a nulo ser� porque no la queremos dibujar.
|
||
547 | // �til para cuando queramos eliminar duplicados.
|
||
548 | if (theLabel.getString() == null) { |
||
549 | return;
|
||
550 | } |
||
551 | |||
552 | // Aqu� hay que mirar m_Size y m_useSize...
|
||
553 | if (!theSymbol.isFontSizeInPixels()) {
|
||
554 | // Suponemos que m_Size viene en coordenadas de mundo real
|
||
555 | // Esto habr� que cambiarlo. Probablemente usar Style2d de geotools en lugar
|
||
556 | // de FSymbol.
|
||
557 | // CAMBIO: La altura del texto la miramos en FLabel
|
||
558 | // float alturaPixels = (float) (theSymbol.m_FontSize * mT.getScaleX());
|
||
559 | float alturaPixels = (float) (theLabel.getHeight() * at.getScaleX()*FLabel.SQUARE); |
||
560 | if (alturaPixels < 3) { |
||
561 | return; // No leemos nada |
||
562 | } |
||
563 | 27676 | csanchez | Font nuevaFuente = theSymbol.getFont().deriveFont(theSymbol.getFont().getStyle(),alturaPixels);
|
564 | 21200 | vcaballero | g2.setFont(nuevaFuente); |
565 | } |
||
566 | |||
567 | |||
568 | if (isSelected){
|
||
569 | g2.setColor(MapContext.getSelectionColor()); |
||
570 | }else{
|
||
571 | g2.setColor(theSymbol.getFontColor()); |
||
572 | } |
||
573 | pAux = at.transform(theLabel.getOrig(), null);
|
||
574 | AffineTransform ant = g2.getTransform();
|
||
575 | |||
576 | x = (float) pAux.getX();
|
||
577 | y = (float) pAux.getY();
|
||
578 | |||
579 | AffineTransform Tx = (AffineTransform) ant.clone(); |
||
580 | Tx.translate(x, y); // S3: final translation
|
||
581 | Tx.rotate(theLabel.getRotation()); // S2: rotate around anchor
|
||
582 | g2.setTransform(Tx); |
||
583 | |||
584 | |||
585 | String s=theLabel.getString();
|
||
586 | |||
587 | |||
588 | //switch (theLabel.getJustification()) {
|
||
589 | |||
590 | //case FLabel.LEFT_BOTTOM:
|
||
591 | g2.drawString(s, 0, 0 - 3); |
||
592 | /*
|
||
593 | break;
|
||
594 | |||
595 | case FLabel.LEFT_CENTER:
|
||
596 | float height = metrics.getMaxAscent();
|
||
597 | g2.drawString(s, 0, 0 - (height / 2));
|
||
598 | |||
599 | break;
|
||
600 | |||
601 | case FLabel.LEFT_TOP:
|
||
602 | height = metrics.getMaxAscent();
|
||
603 | g2.drawString(s, 0, 0 - height);
|
||
604 | |||
605 | break;
|
||
606 | |||
607 | case FLabel.CENTER_BOTTOM:
|
||
608 | float width = metrics.stringWidth(s);
|
||
609 | g2.drawString(s, 0 - (width / 2), 0 - 3);
|
||
610 | |||
611 | break;
|
||
612 | |||
613 | case FLabel.CENTER_CENTER:
|
||
614 | height = metrics.getMaxAscent();
|
||
615 | width = metrics.stringWidth(s);
|
||
616 | g2.drawString(s, 0 - (width / 2),
|
||
617 | 0 - (height / 2));
|
||
618 | |||
619 | break;
|
||
620 | |||
621 | case FLabel.CENTER_TOP:
|
||
622 | width = metrics.stringWidth(s);
|
||
623 | height = metrics.getMaxAscent();
|
||
624 | g2.drawString(s, 0 - (width / 2), 0 -
|
||
625 | height);
|
||
626 | |||
627 | break;
|
||
628 | |||
629 | case FLabel.RIGHT_BOTTOM:
|
||
630 | width = metrics.stringWidth(s);
|
||
631 | g2.drawString(s, 0 - width, 0 - 3);
|
||
632 | |||
633 | break;
|
||
634 | |||
635 | case FLabel.RIGHT_CENTER:
|
||
636 | width = metrics.stringWidth(s);
|
||
637 | height = metrics.getMaxAscent();
|
||
638 | g2.drawString(s, 0 - width, 0 -
|
||
639 | (height / 2));
|
||
640 | |||
641 | break;
|
||
642 | |||
643 | case FLabel.RIGHT_TOP:
|
||
644 | width = metrics.stringWidth(s);
|
||
645 | height = metrics.getMaxAscent();
|
||
646 | g2.drawString(s, 0 - width, 0 - height);
|
||
647 | |||
648 | break;
|
||
649 | }
|
||
650 | */
|
||
651 | ///Rectangle2D borde=vp.fromMapRectangle(theLabel.getBoundBox());//theLabel.getBoundingBox();
|
||
652 | ///g2.setColor(Color.blue);
|
||
653 | ///g2.drawRect((int)borde.getX(),(int)borde.getY(),(int)borde.getWidth(),(int)borde.getHeight());
|
||
654 | // Restauramos
|
||
655 | g2.setTransform(ant); |
||
656 | } |
||
657 | |||
658 | /**
|
||
659 | * Dibuja un punto sobre el Graphics2D que se pasa como par�metro.
|
||
660 | *
|
||
661 | * @param g2 Graphics2D sobre el que dibujar.
|
||
662 | * @param mT MAtriz de transformaci�n.
|
||
663 | * @param pAux punto a dibujar.
|
||
664 | * @param theSymbol S�mbolo a aplicar.
|
||
665 | */
|
||
666 | private static void drawSymbolPoint(Graphics2D g2, AffineTransform mT, |
||
667 | 27024 | jpiera | org.gvsig.fmap.geom.primitive.Point pAux, FSymbol theSymbol) { |
668 | 21200 | vcaballero | int x;
|
669 | int y;
|
||
670 | x = (int) pAux.getX();
|
||
671 | y = (int) pAux.getY();
|
||
672 | |||
673 | /*if (x==0){
|
||
674 | x=100;
|
||
675 | }
|
||
676 | if (y==0){
|
||
677 | y=100;
|
||
678 | }
|
||
679 | */
|
||
680 | Rectangle rectAux = new Rectangle(); |
||
681 | |||
682 | // Aqu� hay que mirar m_Size y m_useSize...
|
||
683 | float radio_simbolo;
|
||
684 | radio_simbolo = theSymbol.getSize() / 2;
|
||
685 | // theSymbol.setSizeInPixels(true);
|
||
686 | |||
687 | if (!theSymbol.isSizeInPixels()) {
|
||
688 | // Suponemos que m_Size viene en coordenadas de mundo real
|
||
689 | radio_simbolo = (float) (theSymbol.getSize() * mT.getScaleX());
|
||
690 | |||
691 | /* System.out.println("m_bUseSize = " + theSymbol.m_bUseSize +
|
||
692 | " Escala: " + mT.getScaleX() + " alturaPixels = " + alturaPixels); */
|
||
693 | // if (radio_simbolo < 1) return; // No dibujamos nada
|
||
694 | rectAux.setRect(x - radio_simbolo, y - radio_simbolo, |
||
695 | radio_simbolo * 2, radio_simbolo * 2); |
||
696 | } else {
|
||
697 | // m_Size viene en pixels
|
||
698 | rectAux.setRect(x - radio_simbolo, y - radio_simbolo, |
||
699 | theSymbol.getSize(), theSymbol.getSize()); |
||
700 | } |
||
701 | |||
702 | // continue; //radioSimbolo_en_pixels = 3;
|
||
703 | if (theSymbol.getFill() != null) { |
||
704 | g2.setPaint(theSymbol.getFill()); |
||
705 | } |
||
706 | |||
707 | if (theSymbol.getStroke() != null) { |
||
708 | g2.setStroke(theSymbol.getStroke()); |
||
709 | } |
||
710 | |||
711 | if (radio_simbolo < 2) { |
||
712 | g2.fillRect(rectAux.x, rectAux.y, rectAux.width, rectAux.height); |
||
713 | |||
714 | return;
|
||
715 | } |
||
716 | |||
717 | switch (theSymbol.getStyle()) {
|
||
718 | 21298 | vcaballero | case FSymbol.SYMBOL_STYLE_MARKER_CIRCLE: // Circulito |
719 | 21200 | vcaballero | |
720 | if (theSymbol.getColor() != null) { |
||
721 | g2.fillOval(rectAux.x, rectAux.y, rectAux.width, |
||
722 | rectAux.height); |
||
723 | } |
||
724 | |||
725 | if (theSymbol.isOutlined()) {
|
||
726 | g2.setColor(theSymbol.getOutlineColor()); |
||
727 | g2.drawOval(rectAux.x, rectAux.y, rectAux.width, |
||
728 | rectAux.height); |
||
729 | } |
||
730 | |||
731 | break;
|
||
732 | |||
733 | 21298 | vcaballero | case FSymbol.SYMBOL_STYLE_MARKER_SQUARE: // Cuadrado |
734 | case FSymbol.SYMBOL_STYLE_FILL_SOLID:
|
||
735 | 21200 | vcaballero | g2.fillRect(rectAux.x, rectAux.y, rectAux.width, rectAux.height); |
736 | |||
737 | if (theSymbol.isOutlined()) {
|
||
738 | g2.setColor(theSymbol.getOutlineColor()); |
||
739 | g2.drawRect(rectAux.x, rectAux.y, rectAux.width, |
||
740 | rectAux.height); |
||
741 | } |
||
742 | |||
743 | break;
|
||
744 | |||
745 | 21298 | vcaballero | case FSymbol.SYMBOL_STYLE_MARKER_TRIANGLE: // Triangulo |
746 | 21200 | vcaballero | |
747 | // y = r*sin30, x = r*cos30
|
||
748 | GeneralPathX genPath = new GeneralPathX();
|
||
749 | genPath.moveTo(x - (int) (radio_simbolo * 0.866), |
||
750 | y + (int) (radio_simbolo * 0.5)); |
||
751 | genPath.lineTo(x + (int) (radio_simbolo * 0.866), |
||
752 | y + (int) (radio_simbolo * 0.5)); |
||
753 | genPath.lineTo(x, y - (float) radio_simbolo);
|
||
754 | genPath.closePath(); |
||
755 | |||
756 | g2.fill(genPath); |
||
757 | |||
758 | break;
|
||
759 | |||
760 | 21298 | vcaballero | case FSymbol.SYMBOL_STYLE_MARKER_CROSS: // cruz |
761 | case FSymbol.SYMBOL_STYLE_DGNSPECIAL: // Cruz |
||
762 | 21200 | vcaballero | |
763 | GeneralPathX genPathCruz = new GeneralPathX();
|
||
764 | genPathCruz.moveTo(x, y - radio_simbolo); |
||
765 | genPathCruz.lineTo(x, y + radio_simbolo); |
||
766 | genPathCruz.moveTo(x - radio_simbolo, y); |
||
767 | genPathCruz.lineTo(x + radio_simbolo, y); |
||
768 | g2.draw(genPathCruz); |
||
769 | |||
770 | break;
|
||
771 | |||
772 | case 34: // TrueType marker |
||
773 | |||
774 | /* lf.lfHeight = -radioSimbolo_en_pixels;
|
||
775 | angulo = pSimbolo->m_Rotation; // En radianes, de -pi a pi
|
||
776 | angulo = -180.0 * angulo / PI;
|
||
777 | |||
778 | lf.lfEscapement = (long) angulo*10;
|
||
779 | lf.lfOrientation = (long) angulo*10;
|
||
780 | |||
781 | fuente.CreateFontIndirect(&lf);
|
||
782 | pOldFont = pDC->SelectObject(&fuente);
|
||
783 | |||
784 | pDC->TextOut(pAPI.x, pAPI.y+radioSimbolo_en_pixels/2,elChar,1);
|
||
785 | |||
786 | pDC->SelectObject(pOldFont);
|
||
787 | fuente.DeleteObject();
|
||
788 | |||
789 | break; */
|
||
790 | 21298 | vcaballero | case FSymbol.SYMBOL_STYLE_MARKER_IMAGEN: // Icono |
791 | 21200 | vcaballero | { |
792 | if (theSymbol.getIcon() != null) { |
||
793 | float w;
|
||
794 | float h;
|
||
795 | |||
796 | if (!theSymbol.isSizeInPixels()) {
|
||
797 | // Suponemos que m_Size viene en coordenadas de mundo real
|
||
798 | // Por ejemplo, nos valemos del ancho para fijar la escala
|
||
799 | w = (float) (theSymbol.getSize() * mT.getScaleX());
|
||
800 | h = (theSymbol.getIcon().getHeight(null) * w) / theSymbol.getIcon()
|
||
801 | .getWidth(null);
|
||
802 | |||
803 | rectAux.setRect(x - w, y - h, w * 2, h * 2); |
||
804 | } else {
|
||
805 | // m_Size viene en pixels
|
||
806 | w = theSymbol.getSize(); |
||
807 | h = (theSymbol.getIcon().getHeight(null) * w) / theSymbol.getIcon()
|
||
808 | .getWidth(null);
|
||
809 | rectAux.setRect(x - w/2, y - h/2, w, h); |
||
810 | } |
||
811 | /* if (theSymbol.getImgObserver() != null)
|
||
812 | {
|
||
813 | g2.setColor(Color.WHITE);
|
||
814 | g2.fillRect(rectAux.x, rectAux.y, rectAux.width, rectAux.height);
|
||
815 | } */
|
||
816 | g2.drawImage(theSymbol.getIcon(), rectAux.x, rectAux.y, |
||
817 | rectAux.width, rectAux.height, theSymbol.getImgObserver()); |
||
818 | } else {
|
||
819 | String strImg = "Image"; // Utilities.getMessage(FGraphicUtilities.class,"imagen"); |
||
820 | FontMetrics metrics = g2.getFontMetrics();
|
||
821 | int width = metrics.stringWidth(strImg);
|
||
822 | int height = metrics.getMaxAscent();
|
||
823 | |||
824 | g2.drawString(strImg, x - (width / 2), y - 2 + |
||
825 | (height / 2));
|
||
826 | } |
||
827 | |||
828 | break;
|
||
829 | } |
||
830 | |||
831 | /* DrawIconEx(pDC->m_hDC, pAPI.x-(pSimbolo->m_widthIco/2), pAPI.y-(pSimbolo->m_heightIco/2),
|
||
832 | pSimbolo->m_hIcon, pSimbolo->m_widthIco, pSimbolo->m_heightIco, 0 , NULL, DI_NORMAL);
|
||
833 | break; */
|
||
834 | 26909 | jpiera | // TODO This case has been commented: check it!!!
|
835 | // case Geometry.TYPES.POINT|Geometry.TYPES.Z: // Circulito
|
||
836 | //
|
||
837 | // if (theSymbol.getColor() != null) {
|
||
838 | // g2.fillOval(rectAux.x, rectAux.y, rectAux.width,
|
||
839 | // rectAux.height);
|
||
840 | // }
|
||
841 | //
|
||
842 | // if (theSymbol.isOutlined()) {
|
||
843 | // g2.setColor(theSymbol.getOutlineColor());
|
||
844 | // g2.drawOval(rectAux.x, rectAux.y, rectAux.width,
|
||
845 | // rectAux.height);
|
||
846 | // }
|
||
847 | //
|
||
848 | // break;
|
||
849 | 21200 | vcaballero | } // del switch estilo
|
850 | } |
||
851 | public static void DrawHandlers(Graphics2D g, AffineTransform at, |
||
852 | Handler[] handlers,ISymbol symbol) { |
||
853 | |||
854 | for (int i = 0; i < handlers.length; i++) { |
||
855 | Point2D point = handlers[i].getPoint();
|
||
856 | at.transform(point, point); |
||
857 | g.setColor(((IFillSymbol)symbol).getFillColor()); |
||
858 | g.fillRect((int) (point.getX() - 3), (int) (point.getY() - 3), 7, 7); |
||
859 | g.setColor(((ILineSymbol)symbol).getColor()); |
||
860 | g.drawRect((int) (point.getX() - 5), (int) (point.getY() - 5), 10, 10); |
||
861 | g.drawString( "" + i, (int) (point.getX() - 5), (int) (point.getY() - 5)); |
||
862 | } |
||
863 | } |
||
864 | |||
865 | public static void DrawVertex(Graphics2D g, AffineTransform at, Handler handlers) { |
||
866 | //for (int i = 0; i < handlers.length; i++) {
|
||
867 | Point2D point = handlers.getPoint();
|
||
868 | at.transform(point, point); |
||
869 | g.setColor(Color.black);
|
||
870 | g.drawLine((int)point.getX()-2,(int)point.getY()-10,(int)point.getX()-2,(int)point.getY()+10); |
||
871 | g.drawLine((int)point.getX()+2,(int)point.getY()-10,(int)point.getX()+2,(int)point.getY()+10); |
||
872 | g.drawLine((int)point.getX()-10,(int)point.getY()-2,(int)point.getX()+10,(int)point.getY()-2); |
||
873 | g.drawLine((int)point.getX()-10,(int)point.getY()+2,(int)point.getX()+10,(int)point.getY()+2); |
||
874 | g.setColor(Color.red);
|
||
875 | g.drawLine((int)point.getX()-1,(int)point.getY()-10,(int)point.getX()-1,(int)point.getY()+10); |
||
876 | g.drawLine((int)point.getX()+1,(int)point.getY()-10,(int)point.getX()+1,(int)point.getY()+10); |
||
877 | g.drawLine((int)point.getX()-10,(int)point.getY()-1,(int)point.getX()+10,(int)point.getY()-1); |
||
878 | g.drawLine((int)point.getX()-10,(int)point.getY()+1,(int)point.getX()+10,(int)point.getY()+1); |
||
879 | |||
880 | //}
|
||
881 | } |
||
882 | } |