svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / rendering / symbols / FGraphicUtilities.java @ 29097
History | View | Annotate | Download (27.5 KB)
1 |
/*
|
---|---|
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 |
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 |
import org.gvsig.fmap.geom.handler.Handler; |
64 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
65 |
import org.gvsig.fmap.geom.util.Converter; |
66 |
import org.gvsig.fmap.geom.util.UtilFunctions; |
67 |
import org.gvsig.fmap.geom.utils.FLabel; |
68 |
import org.gvsig.fmap.mapcontext.MapContext; |
69 |
import org.slf4j.Logger; |
70 |
import org.slf4j.LoggerFactory; |
71 |
|
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 |
private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
89 |
private static final Logger logger = LoggerFactory.getLogger(FGraphicUtilities.class); |
90 |
|
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 |
* @throws CreateGeometryException
|
99 |
*/
|
100 |
public static void DrawSymbol(Graphics2D g2, AffineTransform mT2, |
101 |
Rectangle r, FSymbol symbol) throws CreateGeometryException { |
102 |
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 |
geom = geomManager.createPoint(r.x + (r.width / 2), r.y + (r.height / 2), SUBTYPES.GEOM2D); |
118 |
|
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 |
geom = geomManager.createSurface(line, SUBTYPES.GEOM2D); |
163 |
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 |
geom = geomManager.createSurface(rectAux, SUBTYPES.GEOM2D); |
173 |
|
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 |
case Geometry.TYPES.GEOMETRY:
|
181 |
|
182 |
// Pol�gono
|
183 |
r.setSize(r.width / 3, r.height);
|
184 |
|
185 |
GeneralPathX rectAux2 = new GeneralPathX(r);
|
186 |
rectAux2.transform(mT2); |
187 |
geom = geomManager.createSurface(rectAux2, SUBTYPES.GEOM2D); |
188 |
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 |
geom = geomManager.createCurve(line, SUBTYPES.GEOM2D); |
202 |
symbol.draw(g2, mT, geom, null);
|
203 |
// FGraphicUtilities.DrawShape(g2, mT, shp, symbol);
|
204 |
|
205 |
// Punto:
|
206 |
geom = geomManager.createPoint(r3.x + (r3.width / 2), r3.y +
|
207 |
(r3.height / 2), SUBTYPES.GEOM2D);
|
208 |
|
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 |
geom = geomManager.createPoint(r3.x + (r3.width / 2), r3.y +
|
237 |
(r3.height / 2), SUBTYPES.GEOM2D);
|
238 |
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 |
case Geometry.TYPES.POINT: //Tipo punto |
275 |
drawSymbolPoint(g2, mT, (org.gvsig.fmap.geom.primitive.Point) geom, theSymbol); |
276 |
|
277 |
break;
|
278 |
|
279 |
case Geometry.TYPES.CURVE:
|
280 |
case Geometry.TYPES.ARC:
|
281 |
// 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 |
case Geometry.TYPES.ELLIPSE:
|
293 |
case Geometry.TYPES.CIRCLE:
|
294 |
|
295 |
if (theSymbol.getFill() != null) |
296 |
g2.setPaint(theSymbol.getFill()); |
297 |
|
298 |
if (theSymbol.getColor() != null) |
299 |
if (theSymbol.getStyle() != FSymbol.SYMBOL_STYLE_DGNSPECIAL) {
|
300 |
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 |
Font nuevaFuente = theSymbol.getFont().deriveFont(theSymbol.getFont().getStyle(),alturaPixels);
|
392 |
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 |
pAux = new Point2D.Double(((org.gvsig.fmap.geom.primitive.Point) geom).getX(), |
408 |
((org.gvsig.fmap.geom.primitive.Point) geom).getY()); |
409 |
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 |
com.vividsolutions.jts.geom.Geometry geo = Converter.geometryToJts(geom); |
452 |
|
453 |
// System.out.println("Area de " + m_labelValues[numReg] + " = "
|
454 |
// + geo.getArea());
|
455 |
// System.out.println(geo.toText());
|
456 |
Point pJTS = geo.getInteriorPoint();
|
457 |
Geometry pLabel; |
458 |
try {
|
459 |
pLabel = (Geometry)Converter.jtsToGeometry(pJTS); |
460 |
theLabel.setRotation(0);
|
461 |
theLabel.setOrig(new Point2D.Double( |
462 |
((org.gvsig.fmap.geom.primitive.Point) pLabel).getX(), |
463 |
((org.gvsig.fmap.geom.primitive.Point) pLabel).getX())); |
464 |
} catch (CreateGeometryException e) {
|
465 |
logger.error("Impossible to create the label", e);
|
466 |
} |
467 |
} |
468 |
|
469 |
pAux = mT.transform(theLabel.getOrig(), null);
|
470 |
|
471 |
break;
|
472 |
} |
473 |
|
474 |
AffineTransform ant = g2.getTransform();
|
475 |
|
476 |
x = (float) pAux.getX();
|
477 |
y = (float) pAux.getY();
|
478 |
|
479 |
AffineTransform Tx = (AffineTransform) ant.clone(); |
480 |
Tx.translate(x, y); // S3: final translation
|
481 |
Tx.rotate(theLabel.getRotation()); // S2: rotate around anchor
|
482 |
g2.setTransform(Tx); |
483 |
|
484 |
switch (theLabel.getJustification()) {
|
485 |
case FLabel.LEFT_BOTTOM:
|
486 |
g2.drawString(theLabel.getString(), 0, 0 - 3); |
487 |
|
488 |
break;
|
489 |
|
490 |
case FLabel.LEFT_CENTER:
|
491 |
g2.drawString(theLabel.getString(), 0, 0 - (height / 2)); |
492 |
|
493 |
break;
|
494 |
|
495 |
case FLabel.LEFT_TOP:
|
496 |
g2.drawString(theLabel.getString(), 0, 0 - height); |
497 |
|
498 |
break;
|
499 |
|
500 |
case FLabel.CENTER_BOTTOM:
|
501 |
g2.drawString(theLabel.getString(), 0 - (width / 2), 0 - 3); |
502 |
|
503 |
break;
|
504 |
|
505 |
case FLabel.CENTER_CENTER:
|
506 |
g2.drawString(theLabel.getString(), 0 - (width / 2), |
507 |
0 - (height / 2)); |
508 |
|
509 |
break;
|
510 |
|
511 |
case FLabel.CENTER_TOP:
|
512 |
g2.drawString(theLabel.getString(), 0 - (width / 2), 0 - |
513 |
height); |
514 |
|
515 |
break;
|
516 |
|
517 |
case FLabel.RIGHT_BOTTOM:
|
518 |
g2.drawString(theLabel.getString(), 0 - width, 0 - 3); |
519 |
|
520 |
break;
|
521 |
|
522 |
case FLabel.RIGHT_CENTER:
|
523 |
g2.drawString(theLabel.getString(), 0 - width, 0 - |
524 |
(height / 2));
|
525 |
|
526 |
break;
|
527 |
|
528 |
case FLabel.RIGHT_TOP:
|
529 |
g2.drawString(theLabel.getString(), 0 - width, 0 - height); |
530 |
|
531 |
break;
|
532 |
} |
533 |
|
534 |
// Restauramos
|
535 |
g2.setTransform(ant); |
536 |
} |
537 |
/**
|
538 |
* Dibuja el FLabel que se pasa como par�metro sobre el Graphics2D.
|
539 |
*
|
540 |
* @param g2 Graphics2D sobre el que dibujar.
|
541 |
* @param mT Matriz de transformaci�n.
|
542 |
* @param shp FShape a dibujar.
|
543 |
* @param theSymbol S�mbolo para aplicar.
|
544 |
* @param theLabel FLabel que contiene el texto que se debe dibujar.
|
545 |
*/
|
546 |
public static void DrawAnnotation(Graphics2D g2, AffineTransform at, |
547 |
FSymbol theSymbol, FLabel theLabel,FontMetrics metrics,boolean isSelected) { |
548 |
float x;
|
549 |
float y;
|
550 |
Point2D pAux = null; |
551 |
// Las etiquetas que pongamos a nulo ser� porque no la queremos dibujar.
|
552 |
// �til para cuando queramos eliminar duplicados.
|
553 |
if (theLabel.getString() == null) { |
554 |
return;
|
555 |
} |
556 |
|
557 |
// Aqu� hay que mirar m_Size y m_useSize...
|
558 |
if (!theSymbol.isFontSizeInPixels()) {
|
559 |
// Suponemos que m_Size viene en coordenadas de mundo real
|
560 |
// Esto habr� que cambiarlo. Probablemente usar Style2d de geotools en lugar
|
561 |
// de FSymbol.
|
562 |
// CAMBIO: La altura del texto la miramos en FLabel
|
563 |
// float alturaPixels = (float) (theSymbol.m_FontSize * mT.getScaleX());
|
564 |
float alturaPixels = (float) (theLabel.getHeight() * at.getScaleX()*FLabel.SQUARE); |
565 |
if (alturaPixels < 3) { |
566 |
return; // No leemos nada |
567 |
} |
568 |
Font nuevaFuente = theSymbol.getFont().deriveFont(theSymbol.getFont().getStyle(),alturaPixels);
|
569 |
g2.setFont(nuevaFuente); |
570 |
} |
571 |
|
572 |
|
573 |
if (isSelected){
|
574 |
g2.setColor(MapContext.getSelectionColor()); |
575 |
}else{
|
576 |
g2.setColor(theSymbol.getFontColor()); |
577 |
} |
578 |
pAux = at.transform(theLabel.getOrig(), null);
|
579 |
AffineTransform ant = g2.getTransform();
|
580 |
|
581 |
x = (float) pAux.getX();
|
582 |
y = (float) pAux.getY();
|
583 |
|
584 |
AffineTransform Tx = (AffineTransform) ant.clone(); |
585 |
Tx.translate(x, y); // S3: final translation
|
586 |
Tx.rotate(theLabel.getRotation()); // S2: rotate around anchor
|
587 |
g2.setTransform(Tx); |
588 |
|
589 |
|
590 |
String s=theLabel.getString();
|
591 |
|
592 |
|
593 |
//switch (theLabel.getJustification()) {
|
594 |
|
595 |
//case FLabel.LEFT_BOTTOM:
|
596 |
g2.drawString(s, 0, 0 - 3); |
597 |
/*
|
598 |
break;
|
599 |
|
600 |
case FLabel.LEFT_CENTER:
|
601 |
float height = metrics.getMaxAscent();
|
602 |
g2.drawString(s, 0, 0 - (height / 2));
|
603 |
|
604 |
break;
|
605 |
|
606 |
case FLabel.LEFT_TOP:
|
607 |
height = metrics.getMaxAscent();
|
608 |
g2.drawString(s, 0, 0 - height);
|
609 |
|
610 |
break;
|
611 |
|
612 |
case FLabel.CENTER_BOTTOM:
|
613 |
float width = metrics.stringWidth(s);
|
614 |
g2.drawString(s, 0 - (width / 2), 0 - 3);
|
615 |
|
616 |
break;
|
617 |
|
618 |
case FLabel.CENTER_CENTER:
|
619 |
height = metrics.getMaxAscent();
|
620 |
width = metrics.stringWidth(s);
|
621 |
g2.drawString(s, 0 - (width / 2),
|
622 |
0 - (height / 2));
|
623 |
|
624 |
break;
|
625 |
|
626 |
case FLabel.CENTER_TOP:
|
627 |
width = metrics.stringWidth(s);
|
628 |
height = metrics.getMaxAscent();
|
629 |
g2.drawString(s, 0 - (width / 2), 0 -
|
630 |
height);
|
631 |
|
632 |
break;
|
633 |
|
634 |
case FLabel.RIGHT_BOTTOM:
|
635 |
width = metrics.stringWidth(s);
|
636 |
g2.drawString(s, 0 - width, 0 - 3);
|
637 |
|
638 |
break;
|
639 |
|
640 |
case FLabel.RIGHT_CENTER:
|
641 |
width = metrics.stringWidth(s);
|
642 |
height = metrics.getMaxAscent();
|
643 |
g2.drawString(s, 0 - width, 0 -
|
644 |
(height / 2));
|
645 |
|
646 |
break;
|
647 |
|
648 |
case FLabel.RIGHT_TOP:
|
649 |
width = metrics.stringWidth(s);
|
650 |
height = metrics.getMaxAscent();
|
651 |
g2.drawString(s, 0 - width, 0 - height);
|
652 |
|
653 |
break;
|
654 |
}
|
655 |
*/
|
656 |
///Rectangle2D borde=vp.fromMapRectangle(theLabel.getBoundBox());//theLabel.getBoundingBox();
|
657 |
///g2.setColor(Color.blue);
|
658 |
///g2.drawRect((int)borde.getX(),(int)borde.getY(),(int)borde.getWidth(),(int)borde.getHeight());
|
659 |
// Restauramos
|
660 |
g2.setTransform(ant); |
661 |
} |
662 |
|
663 |
/**
|
664 |
* Dibuja un punto sobre el Graphics2D que se pasa como par�metro.
|
665 |
*
|
666 |
* @param g2 Graphics2D sobre el que dibujar.
|
667 |
* @param mT MAtriz de transformaci�n.
|
668 |
* @param pAux punto a dibujar.
|
669 |
* @param theSymbol S�mbolo a aplicar.
|
670 |
*/
|
671 |
private static void drawSymbolPoint(Graphics2D g2, AffineTransform mT, |
672 |
org.gvsig.fmap.geom.primitive.Point pAux, FSymbol theSymbol) { |
673 |
int x;
|
674 |
int y;
|
675 |
x = (int) pAux.getX();
|
676 |
y = (int) pAux.getY();
|
677 |
|
678 |
/*if (x==0){
|
679 |
x=100;
|
680 |
}
|
681 |
if (y==0){
|
682 |
y=100;
|
683 |
}
|
684 |
*/
|
685 |
Rectangle rectAux = new Rectangle(); |
686 |
|
687 |
// Aqu� hay que mirar m_Size y m_useSize...
|
688 |
float radio_simbolo;
|
689 |
radio_simbolo = theSymbol.getSize() / 2;
|
690 |
// theSymbol.setSizeInPixels(true);
|
691 |
|
692 |
if (!theSymbol.isSizeInPixels()) {
|
693 |
// Suponemos que m_Size viene en coordenadas de mundo real
|
694 |
radio_simbolo = (float) (theSymbol.getSize() * mT.getScaleX());
|
695 |
|
696 |
/* System.out.println("m_bUseSize = " + theSymbol.m_bUseSize +
|
697 |
" Escala: " + mT.getScaleX() + " alturaPixels = " + alturaPixels); */
|
698 |
// if (radio_simbolo < 1) return; // No dibujamos nada
|
699 |
rectAux.setRect(x - radio_simbolo, y - radio_simbolo, |
700 |
radio_simbolo * 2, radio_simbolo * 2); |
701 |
} else {
|
702 |
// m_Size viene en pixels
|
703 |
rectAux.setRect(x - radio_simbolo, y - radio_simbolo, |
704 |
theSymbol.getSize(), theSymbol.getSize()); |
705 |
} |
706 |
|
707 |
// continue; //radioSimbolo_en_pixels = 3;
|
708 |
if (theSymbol.getFill() != null) { |
709 |
g2.setPaint(theSymbol.getFill()); |
710 |
} |
711 |
|
712 |
if (theSymbol.getStroke() != null) { |
713 |
g2.setStroke(theSymbol.getStroke()); |
714 |
} |
715 |
|
716 |
if (radio_simbolo < 2) { |
717 |
g2.fillRect(rectAux.x, rectAux.y, rectAux.width, rectAux.height); |
718 |
|
719 |
return;
|
720 |
} |
721 |
|
722 |
switch (theSymbol.getStyle()) {
|
723 |
case FSymbol.SYMBOL_STYLE_MARKER_CIRCLE: // Circulito |
724 |
|
725 |
if (theSymbol.getColor() != null) { |
726 |
g2.fillOval(rectAux.x, rectAux.y, rectAux.width, |
727 |
rectAux.height); |
728 |
} |
729 |
|
730 |
if (theSymbol.isOutlined()) {
|
731 |
g2.setColor(theSymbol.getOutlineColor()); |
732 |
g2.drawOval(rectAux.x, rectAux.y, rectAux.width, |
733 |
rectAux.height); |
734 |
} |
735 |
|
736 |
break;
|
737 |
|
738 |
case FSymbol.SYMBOL_STYLE_MARKER_SQUARE: // Cuadrado |
739 |
case FSymbol.SYMBOL_STYLE_FILL_SOLID:
|
740 |
g2.fillRect(rectAux.x, rectAux.y, rectAux.width, rectAux.height); |
741 |
|
742 |
if (theSymbol.isOutlined()) {
|
743 |
g2.setColor(theSymbol.getOutlineColor()); |
744 |
g2.drawRect(rectAux.x, rectAux.y, rectAux.width, |
745 |
rectAux.height); |
746 |
} |
747 |
|
748 |
break;
|
749 |
|
750 |
case FSymbol.SYMBOL_STYLE_MARKER_TRIANGLE: // Triangulo |
751 |
|
752 |
// y = r*sin30, x = r*cos30
|
753 |
GeneralPathX genPath = new GeneralPathX();
|
754 |
genPath.moveTo(x - (int) (radio_simbolo * 0.866), |
755 |
y + (int) (radio_simbolo * 0.5)); |
756 |
genPath.lineTo(x + (int) (radio_simbolo * 0.866), |
757 |
y + (int) (radio_simbolo * 0.5)); |
758 |
genPath.lineTo(x, y - (float) radio_simbolo);
|
759 |
genPath.closePath(); |
760 |
|
761 |
g2.fill(genPath); |
762 |
|
763 |
break;
|
764 |
|
765 |
case FSymbol.SYMBOL_STYLE_MARKER_CROSS: // cruz |
766 |
case FSymbol.SYMBOL_STYLE_DGNSPECIAL: // Cruz |
767 |
|
768 |
GeneralPathX genPathCruz = new GeneralPathX();
|
769 |
genPathCruz.moveTo(x, y - radio_simbolo); |
770 |
genPathCruz.lineTo(x, y + radio_simbolo); |
771 |
genPathCruz.moveTo(x - radio_simbolo, y); |
772 |
genPathCruz.lineTo(x + radio_simbolo, y); |
773 |
g2.draw(genPathCruz); |
774 |
|
775 |
break;
|
776 |
|
777 |
case 34: // TrueType marker |
778 |
|
779 |
/* lf.lfHeight = -radioSimbolo_en_pixels;
|
780 |
angulo = pSimbolo->m_Rotation; // En radianes, de -pi a pi
|
781 |
angulo = -180.0 * angulo / PI;
|
782 |
|
783 |
lf.lfEscapement = (long) angulo*10;
|
784 |
lf.lfOrientation = (long) angulo*10;
|
785 |
|
786 |
fuente.CreateFontIndirect(&lf);
|
787 |
pOldFont = pDC->SelectObject(&fuente);
|
788 |
|
789 |
pDC->TextOut(pAPI.x, pAPI.y+radioSimbolo_en_pixels/2,elChar,1);
|
790 |
|
791 |
pDC->SelectObject(pOldFont);
|
792 |
fuente.DeleteObject();
|
793 |
|
794 |
break; */
|
795 |
case FSymbol.SYMBOL_STYLE_MARKER_IMAGEN: // Icono |
796 |
{ |
797 |
if (theSymbol.getIcon() != null) { |
798 |
float w;
|
799 |
float h;
|
800 |
|
801 |
if (!theSymbol.isSizeInPixels()) {
|
802 |
// Suponemos que m_Size viene en coordenadas de mundo real
|
803 |
// Por ejemplo, nos valemos del ancho para fijar la escala
|
804 |
w = (float) (theSymbol.getSize() * mT.getScaleX());
|
805 |
h = (theSymbol.getIcon().getHeight(null) * w) / theSymbol.getIcon()
|
806 |
.getWidth(null);
|
807 |
|
808 |
rectAux.setRect(x - w, y - h, w * 2, h * 2); |
809 |
} else {
|
810 |
// m_Size viene en pixels
|
811 |
w = theSymbol.getSize(); |
812 |
h = (theSymbol.getIcon().getHeight(null) * w) / theSymbol.getIcon()
|
813 |
.getWidth(null);
|
814 |
rectAux.setRect(x - w/2, y - h/2, w, h); |
815 |
} |
816 |
/* if (theSymbol.getImgObserver() != null)
|
817 |
{
|
818 |
g2.setColor(Color.WHITE);
|
819 |
g2.fillRect(rectAux.x, rectAux.y, rectAux.width, rectAux.height);
|
820 |
} */
|
821 |
g2.drawImage(theSymbol.getIcon(), rectAux.x, rectAux.y, |
822 |
rectAux.width, rectAux.height, theSymbol.getImgObserver()); |
823 |
} else {
|
824 |
String strImg = "Image"; // Utilities.getMessage(FGraphicUtilities.class,"imagen"); |
825 |
FontMetrics metrics = g2.getFontMetrics();
|
826 |
int width = metrics.stringWidth(strImg);
|
827 |
int height = metrics.getMaxAscent();
|
828 |
|
829 |
g2.drawString(strImg, x - (width / 2), y - 2 + |
830 |
(height / 2));
|
831 |
} |
832 |
|
833 |
break;
|
834 |
} |
835 |
|
836 |
/* DrawIconEx(pDC->m_hDC, pAPI.x-(pSimbolo->m_widthIco/2), pAPI.y-(pSimbolo->m_heightIco/2),
|
837 |
pSimbolo->m_hIcon, pSimbolo->m_widthIco, pSimbolo->m_heightIco, 0 , NULL, DI_NORMAL);
|
838 |
break; */
|
839 |
// TODO This case has been commented: check it!!!
|
840 |
// case Geometry.TYPES.POINT|Geometry.TYPES.Z: // Circulito
|
841 |
//
|
842 |
// if (theSymbol.getColor() != null) {
|
843 |
// g2.fillOval(rectAux.x, rectAux.y, rectAux.width,
|
844 |
// rectAux.height);
|
845 |
// }
|
846 |
//
|
847 |
// if (theSymbol.isOutlined()) {
|
848 |
// g2.setColor(theSymbol.getOutlineColor());
|
849 |
// g2.drawOval(rectAux.x, rectAux.y, rectAux.width,
|
850 |
// rectAux.height);
|
851 |
// }
|
852 |
//
|
853 |
// break;
|
854 |
} // del switch estilo
|
855 |
} |
856 |
public static void DrawHandlers(Graphics2D g, AffineTransform at, |
857 |
Handler[] handlers,ISymbol symbol) { |
858 |
|
859 |
for (int i = 0; i < handlers.length; i++) { |
860 |
Point2D point = handlers[i].getPoint();
|
861 |
at.transform(point, point); |
862 |
g.setColor(((IFillSymbol)symbol).getFillColor()); |
863 |
g.fillRect((int) (point.getX() - 3), (int) (point.getY() - 3), 7, 7); |
864 |
g.setColor(((ILineSymbol)symbol).getColor()); |
865 |
g.drawRect((int) (point.getX() - 5), (int) (point.getY() - 5), 10, 10); |
866 |
g.drawString( "" + i, (int) (point.getX() - 5), (int) (point.getY() - 5)); |
867 |
} |
868 |
} |
869 |
|
870 |
public static void DrawVertex(Graphics2D g, AffineTransform at, Handler handlers) { |
871 |
//for (int i = 0; i < handlers.length; i++) {
|
872 |
Point2D point = handlers.getPoint();
|
873 |
at.transform(point, point); |
874 |
g.setColor(Color.black);
|
875 |
g.drawLine((int)point.getX()-2,(int)point.getY()-10,(int)point.getX()-2,(int)point.getY()+10); |
876 |
g.drawLine((int)point.getX()+2,(int)point.getY()-10,(int)point.getX()+2,(int)point.getY()+10); |
877 |
g.drawLine((int)point.getX()-10,(int)point.getY()-2,(int)point.getX()+10,(int)point.getY()-2); |
878 |
g.drawLine((int)point.getX()-10,(int)point.getY()+2,(int)point.getX()+10,(int)point.getY()+2); |
879 |
g.setColor(Color.red);
|
880 |
g.drawLine((int)point.getX()-1,(int)point.getY()-10,(int)point.getX()-1,(int)point.getY()+10); |
881 |
g.drawLine((int)point.getX()+1,(int)point.getY()-10,(int)point.getX()+1,(int)point.getY()+10); |
882 |
g.drawLine((int)point.getX()-10,(int)point.getY()-1,(int)point.getX()+10,(int)point.getY()-1); |
883 |
g.drawLine((int)point.getX()-10,(int)point.getY()+1,(int)point.getX()+10,(int)point.getY()+1); |
884 |
|
885 |
//}
|
886 |
} |
887 |
} |