root / branches / v2_0_0_prep / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / rendering / symbols / FGraphicUtilities.java @ 21200
History | View | Annotate | Download (27.1 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.handler.Handler; |
60 |
import org.gvsig.fmap.geom.primitive.Curve2D; |
61 |
import org.gvsig.fmap.geom.primitive.FShape; |
62 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
63 |
import org.gvsig.fmap.geom.primitive.Surface2D; |
64 |
import org.gvsig.fmap.geom.util.Converter; |
65 |
import org.gvsig.fmap.geometry.utils.FLabel; |
66 |
import org.gvsig.fmap.mapcontext.MapContext; |
67 |
|
68 |
import com.vividsolutions.jts.geom.Point; |
69 |
|
70 |
/**
|
71 |
* Clase con m�todos est�ticos para dibujar sobre el Graphics que se les pase
|
72 |
* como par�metro.
|
73 |
*
|
74 |
* Esta clase deber�a ser privada. Las clases que la usan son GraphicLayer
|
75 |
* y AnnotationStrategy, pero hay que revisarlas para que no sea necesario.
|
76 |
* Lo m�s urgente ser�a lo del dibujado de textos, para que sea
|
77 |
* Hay que quitar las dependecias de FSymbol, y trabajar SIEMPRE con ISymbol.
|
78 |
* Recordar: Que sea ISymbol el que renderiza.
|
79 |
* extensible el s�mbolo a usar. NOTA: Ver tambi�n comentario en ISymbol
|
80 |
*
|
81 |
* @author fjp
|
82 |
*/
|
83 |
public class FGraphicUtilities { |
84 |
|
85 |
|
86 |
/**
|
87 |
* Dibuja el s�mbolo que se le pasa como par�metro en el Graphics.
|
88 |
*
|
89 |
* @param g2 Graphics2D sobre el que dibujar.
|
90 |
* @param mT2 Matriz de transformaci�n.
|
91 |
* @param r Rect�ngulo.
|
92 |
* @param symbol S�mbolo a dibujar.
|
93 |
*/
|
94 |
public static void DrawSymbol(Graphics2D g2, AffineTransform mT2, |
95 |
Rectangle r, FSymbol symbol) {
|
96 |
Geometry geom; |
97 |
|
98 |
AffineTransform mT = new AffineTransform(); |
99 |
mT.setToIdentity(); |
100 |
|
101 |
Rectangle r2 = new Rectangle(r.x + 2 + (r.width / 3), r.y, r.width / 3, |
102 |
r.height); |
103 |
Rectangle r3 = new Rectangle(r.x + 2 + ((2 * r.width) / 3), r.y, |
104 |
r.width / 3, r.height);
|
105 |
|
106 |
// g2.clearRect(r.x, r.y, r.width, r.height);
|
107 |
// System.out.println("r = " + r.toString() + " Color preview:" + symbol.m_Color.toString());
|
108 |
// System.out.println("symbol.m_symbolType= "+symbol.m_symbolType);
|
109 |
switch (symbol.getSymbolType()) {
|
110 |
case Geometry.TYPES.POINT:
|
111 |
geom = new org.gvsig.fmap.geom.primitive.Point2D(r.x + (r.width / 2), r.y + (r.height / 2)); |
112 |
|
113 |
// Para no tener que clonarlo si viene en unidades de mapa
|
114 |
boolean bAux2 = symbol.isSizeInPixels();
|
115 |
int alturaMetros = symbol.getSize(); // Nota: Cambiar m_Size a float |
116 |
|
117 |
if (!bAux2) {
|
118 |
symbol.setSizeInPixels(true);
|
119 |
symbol.setSize(8); // tama�o fijo |
120 |
} |
121 |
|
122 |
symbol.draw(g2, mT, geom, null);
|
123 |
// FGraphicUtilities.DrawShape(g2, mT, shp, symbol);
|
124 |
|
125 |
|
126 |
if (!bAux2) {
|
127 |
symbol.setSize(alturaMetros); |
128 |
symbol.setSizeInPixels(bAux2); |
129 |
} |
130 |
|
131 |
if (symbol.getFont() != null) { |
132 |
// Para no tener que clonarlo si viene en unidades de mapa
|
133 |
boolean bAux = symbol.isFontSizeInPixels();
|
134 |
symbol.setFontSizeInPixels(true);
|
135 |
FGraphicUtilities.DrawLabel(g2, mT, geom, symbol, |
136 |
new FLabel("Abcd")); |
137 |
symbol.setFontSizeInPixels(bAux); |
138 |
} |
139 |
|
140 |
break;
|
141 |
|
142 |
case Geometry.TYPES.CURVE:
|
143 |
|
144 |
Rectangle rect = mT2.createTransformedShape(r).getBounds();
|
145 |
GeneralPathX line = new GeneralPathX();
|
146 |
line.moveTo(rect.x, rect.y + (rect.height / 2));
|
147 |
|
148 |
// line.lineTo(rect.x + rect.width/3, rect.y + rect.height);
|
149 |
// line.lineTo(rect.x + 2*rect.width/3, rect.y);
|
150 |
// line.lineTo(rect.x + rect.width, rect.y + rect.height/2);
|
151 |
line.curveTo(rect.x + (rect.width / 3),
|
152 |
rect.y + (2 * rect.height),
|
153 |
rect.x + ((2 * rect.width) / 3), rect.y - rect.height, |
154 |
rect.x + rect.width, rect.y + (rect.height / 2));
|
155 |
|
156 |
geom = new Surface2D(line);
|
157 |
symbol.draw(g2, mT, geom, null);
|
158 |
// FGraphicUtilities.DrawShape(g2, mT, shp, symbol);
|
159 |
|
160 |
break;
|
161 |
|
162 |
case Geometry.TYPES.SURFACE:
|
163 |
|
164 |
GeneralPathX rectAux = new GeneralPathX(r);
|
165 |
rectAux.transform(mT2); |
166 |
geom = new Surface2D(rectAux);
|
167 |
|
168 |
// System.out.println("rect = "+rectAux.getBounds());
|
169 |
symbol.draw(g2, mT, geom, null);
|
170 |
// FGraphicUtilities.DrawShape(g2, mT, shp, symbol);
|
171 |
|
172 |
break;
|
173 |
|
174 |
case Geometry.TYPES.AGGREGATE:
|
175 |
|
176 |
// Pol�gono
|
177 |
r.setSize(r.width / 3, r.height);
|
178 |
|
179 |
GeneralPathX rectAux2 = new GeneralPathX(r);
|
180 |
rectAux2.transform(mT2); |
181 |
geom = new Surface2D(rectAux2);
|
182 |
symbol.draw(g2, mT, geom, null);
|
183 |
// FGraphicUtilities.DrawShape(g2, mT, shp, symbol);
|
184 |
|
185 |
// L�nea
|
186 |
rect = mT2.createTransformedShape(r2).getBounds(); |
187 |
line = new GeneralPathX();
|
188 |
line.moveTo(rect.x, rect.y + (rect.height / 2));
|
189 |
|
190 |
line.curveTo(rect.x + (rect.width / 3),
|
191 |
rect.y + (2 * rect.height),
|
192 |
rect.x + ((2 * rect.width) / 3), rect.y - rect.height, |
193 |
rect.x + rect.width, rect.y + (rect.height / 2));
|
194 |
|
195 |
geom = new Curve2D(line);
|
196 |
symbol.draw(g2, mT, geom, null);
|
197 |
// FGraphicUtilities.DrawShape(g2, mT, shp, symbol);
|
198 |
|
199 |
// Punto:
|
200 |
geom = new org.gvsig.fmap.geom.primitive.Point2D(r3.x + (r3.width / 2), r3.y + |
201 |
(r3.height / 2));
|
202 |
|
203 |
// Para no tener que clonarlo si viene en unidades de mapa
|
204 |
bAux2 = symbol.isSizeInPixels(); |
205 |
alturaMetros = symbol.getSize(); // Nota: Cambiar m_Size a float
|
206 |
|
207 |
if (!bAux2) {
|
208 |
symbol.setSizeInPixels(true);
|
209 |
symbol.setSize(4); // tama�o fijo |
210 |
} |
211 |
symbol.draw(g2, mT, geom, null);
|
212 |
// FGraphicUtilities.DrawShape(g2, mT, shp, symbol);
|
213 |
|
214 |
if (!bAux2) {
|
215 |
symbol.setSize(alturaMetros); |
216 |
symbol.setSizeInPixels(bAux2); |
217 |
} |
218 |
|
219 |
if (symbol.getFont() != null) { |
220 |
// Para no tener que clonarlo si viene en unidades de mapa
|
221 |
boolean bAux = symbol.isFontSizeInPixels();
|
222 |
symbol.setFontSizeInPixels(true);
|
223 |
FGraphicUtilities.DrawLabel(g2, mT, geom, symbol, |
224 |
new FLabel("Abcd")); |
225 |
symbol.setFontSizeInPixels(bAux); |
226 |
} |
227 |
|
228 |
break;
|
229 |
case Geometry.TYPES.TEXT:
|
230 |
geom = new org.gvsig.fmap.geom.primitive.Point2D(r3.x + (r3.width / 2), r3.y + |
231 |
(r3.height / 2));
|
232 |
boolean bAux = symbol.isFontSizeInPixels();
|
233 |
symbol.setFontSizeInPixels(true);
|
234 |
FGraphicUtilities.DrawLabel(g2, mT, geom, symbol, |
235 |
new FLabel("Abcd")); |
236 |
symbol.setFontSizeInPixels(bAux); |
237 |
break;
|
238 |
} |
239 |
} |
240 |
|
241 |
/**
|
242 |
* Dibuja el shape que se pasa como par�metro con las caracter�sticas que
|
243 |
* aporta el s�mbolo sobre el Graphics2D.
|
244 |
*
|
245 |
* @param g2 Graphics2D sobre el que dibujar.
|
246 |
* @param mT Matriz de transformaci�n.
|
247 |
* @param shp FShape a dibujar.
|
248 |
* @param theSymbol S�mbolo.
|
249 |
* @deprecated
|
250 |
*/
|
251 |
public static void DrawShape(Graphics2D g2, AffineTransform mT, Geometry geom, |
252 |
FSymbol theSymbol) { |
253 |
// Hacemos la transformaci�n del shape aqu� dentro... por ahora.
|
254 |
if (geom == null || theSymbol == null || (!theSymbol.isShapeVisible())) { |
255 |
return;
|
256 |
} |
257 |
g2.setColor(theSymbol.getColor()); |
258 |
|
259 |
/* if (shp instanceof FPolygon2D)
|
260 |
{
|
261 |
System.out.println("Entra pol�gono");
|
262 |
} */
|
263 |
int type=geom.getType();
|
264 |
/* if (shp.getShapeType()>=FShape.Z){
|
265 |
type=shp.getShapeType()-FShape.Z;
|
266 |
} */
|
267 |
switch (type) {
|
268 |
case Geometry.TYPES.POINT: //Tipo punto |
269 |
case Geometry.TYPES.POINT + Geometry.TYPES.Z:
|
270 |
drawSymbolPoint(g2, mT, (org.gvsig.fmap.geom.primitive.Point2D) geom, theSymbol); |
271 |
|
272 |
break;
|
273 |
|
274 |
case Geometry.TYPES.CURVE:
|
275 |
case Geometry.TYPES.CURVE + Geometry.TYPES.Z:
|
276 |
case Geometry.TYPES.ARC:
|
277 |
case Geometry.TYPES.ARC + Geometry.TYPES.Z:
|
278 |
// Shape theShp = mT.createTransformedShape(shp.m_Polyline);
|
279 |
// g2.setColor(theSymbol.m_Color);
|
280 |
if (theSymbol.getStroke() != null) { |
281 |
g2.setStroke(theSymbol.getStroke()); |
282 |
} |
283 |
|
284 |
g2.draw(geom); |
285 |
|
286 |
break;
|
287 |
|
288 |
case Geometry.TYPES.SURFACE:
|
289 |
case Geometry.TYPES.SURFACE + Geometry.TYPES.Z:
|
290 |
case Geometry.TYPES.ELLIPSE:
|
291 |
case Geometry.TYPES.ELLIPSE + Geometry.TYPES.Z:
|
292 |
case Geometry.TYPES.CIRCLE:
|
293 |
case Geometry.TYPES.CIRCLE + Geometry.TYPES.Z:
|
294 |
|
295 |
if (theSymbol.getFill() != null) |
296 |
g2.setPaint(theSymbol.getFill()); |
297 |
|
298 |
if (theSymbol.getColor() != null) |
299 |
if (theSymbol.getStyle() != ISymbol.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(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 |
if (type>=Geometry.TYPES.Z){
|
406 |
type=type-Geometry.TYPES.Z; |
407 |
} |
408 |
switch (type) {
|
409 |
case Geometry.TYPES.POINT: //Tipo punto |
410 |
pAux = new Point2D.Double(((org.gvsig.fmap.geom.primitive.Point2D) geom).getX(), |
411 |
((org.gvsig.fmap.geom.primitive.Point2D) geom).getY()); |
412 |
pAux = mT.transform(pAux, null);
|
413 |
|
414 |
break;
|
415 |
|
416 |
case Geometry.TYPES.CURVE:
|
417 |
|
418 |
//
|
419 |
if (theLabel.getOrig() == null) // Calculamos el punto y la orientaci�n solo la primera vez |
420 |
{ |
421 |
PathLength pathLen = new PathLength(geom);
|
422 |
|
423 |
// if (pathLen.lengthOfPath() < width / mT.getScaleX()) return;
|
424 |
float midDistance = pathLen.lengthOfPath() / 2; |
425 |
pAux = pathLen.pointAtLength(midDistance); |
426 |
angle = pathLen.angleAtLength(midDistance); |
427 |
|
428 |
if (angle < 0) { |
429 |
angle = angle + (float) (2 * Math.PI); |
430 |
} |
431 |
|
432 |
if ((angle > (Math.PI / 2)) && |
433 |
(angle < ((3 * Math.PI) / 2))) { |
434 |
angle = angle - (float) Math.PI; |
435 |
} |
436 |
|
437 |
theLabel.setRotation(Math.toDegrees(angle));
|
438 |
theLabel.setOrig(pAux); |
439 |
} |
440 |
|
441 |
pAux = mT.transform(theLabel.getOrig(), null);
|
442 |
|
443 |
// pAux = theLabel.getOrig();
|
444 |
// GlyphVector theGlyphs = theSymbol.m_Font.createGlyphVector(g2.getFontRenderContext(), theLabel);
|
445 |
// Shape txtShp = TextPathLayout.layoutGlyphVector(theGlyphs, shp.m_Polyline,TextPathLayout.ALIGN_MIDDLE);
|
446 |
// g2.draw(txtShp);
|
447 |
// System.out.println("Pintando etiqueta " + theLabel );
|
448 |
break;
|
449 |
|
450 |
case Geometry.TYPES.SURFACE:
|
451 |
|
452 |
if (theLabel.getOrig() == null) // Calculamos el punto solo la primera vez |
453 |
{ |
454 |
com.vividsolutions.jts.geom.Geometry geo = Converter.java2d_to_jts((FShape)geom); |
455 |
|
456 |
// System.out.println("Area de " + m_labelValues[numReg] + " = "
|
457 |
// + geo.getArea());
|
458 |
// System.out.println(geo.toText());
|
459 |
Point pJTS = geo.getInteriorPoint();
|
460 |
Geometry pLabel = (Geometry)Converter.jts_to_java2d(pJTS); |
461 |
theLabel.setRotation(0);
|
462 |
theLabel.setOrig(new Point2D.Double( |
463 |
((org.gvsig.fmap.geom.primitive.Point2D) pLabel).getX(), |
464 |
((org.gvsig.fmap.geom.primitive.Point2D) pLabel).getX())); |
465 |
} |
466 |
|
467 |
pAux = mT.transform(theLabel.getOrig(), null);
|
468 |
|
469 |
break;
|
470 |
} |
471 |
|
472 |
AffineTransform ant = g2.getTransform();
|
473 |
|
474 |
x = (float) pAux.getX();
|
475 |
y = (float) pAux.getY();
|
476 |
|
477 |
AffineTransform Tx = (AffineTransform) ant.clone(); |
478 |
Tx.translate(x, y); // S3: final translation
|
479 |
Tx.rotate(theLabel.getRotation()); // S2: rotate around anchor
|
480 |
g2.setTransform(Tx); |
481 |
|
482 |
switch (theLabel.getJustification()) {
|
483 |
case FLabel.LEFT_BOTTOM:
|
484 |
g2.drawString(theLabel.getString(), 0, 0 - 3); |
485 |
|
486 |
break;
|
487 |
|
488 |
case FLabel.LEFT_CENTER:
|
489 |
g2.drawString(theLabel.getString(), 0, 0 - (height / 2)); |
490 |
|
491 |
break;
|
492 |
|
493 |
case FLabel.LEFT_TOP:
|
494 |
g2.drawString(theLabel.getString(), 0, 0 - height); |
495 |
|
496 |
break;
|
497 |
|
498 |
case FLabel.CENTER_BOTTOM:
|
499 |
g2.drawString(theLabel.getString(), 0 - (width / 2), 0 - 3); |
500 |
|
501 |
break;
|
502 |
|
503 |
case FLabel.CENTER_CENTER:
|
504 |
g2.drawString(theLabel.getString(), 0 - (width / 2), |
505 |
0 - (height / 2)); |
506 |
|
507 |
break;
|
508 |
|
509 |
case FLabel.CENTER_TOP:
|
510 |
g2.drawString(theLabel.getString(), 0 - (width / 2), 0 - |
511 |
height); |
512 |
|
513 |
break;
|
514 |
|
515 |
case FLabel.RIGHT_BOTTOM:
|
516 |
g2.drawString(theLabel.getString(), 0 - width, 0 - 3); |
517 |
|
518 |
break;
|
519 |
|
520 |
case FLabel.RIGHT_CENTER:
|
521 |
g2.drawString(theLabel.getString(), 0 - width, 0 - |
522 |
(height / 2));
|
523 |
|
524 |
break;
|
525 |
|
526 |
case FLabel.RIGHT_TOP:
|
527 |
g2.drawString(theLabel.getString(), 0 - width, 0 - height); |
528 |
|
529 |
break;
|
530 |
} |
531 |
|
532 |
// Restauramos
|
533 |
g2.setTransform(ant); |
534 |
} |
535 |
/**
|
536 |
* Dibuja el FLabel que se pasa como par�metro sobre el Graphics2D.
|
537 |
*
|
538 |
* @param g2 Graphics2D sobre el que dibujar.
|
539 |
* @param mT Matriz de transformaci�n.
|
540 |
* @param shp FShape a dibujar.
|
541 |
* @param theSymbol S�mbolo para aplicar.
|
542 |
* @param theLabel FLabel que contiene el texto que se debe dibujar.
|
543 |
*/
|
544 |
public static void DrawAnnotation(Graphics2D g2, AffineTransform at, |
545 |
FSymbol theSymbol, FLabel theLabel,FontMetrics metrics,boolean isSelected) { |
546 |
float x;
|
547 |
float y;
|
548 |
Point2D pAux = null; |
549 |
// Las etiquetas que pongamos a nulo ser� porque no la queremos dibujar.
|
550 |
// �til para cuando queramos eliminar duplicados.
|
551 |
if (theLabel.getString() == null) { |
552 |
return;
|
553 |
} |
554 |
|
555 |
// Aqu� hay que mirar m_Size y m_useSize...
|
556 |
if (!theSymbol.isFontSizeInPixels()) {
|
557 |
// Suponemos que m_Size viene en coordenadas de mundo real
|
558 |
// Esto habr� que cambiarlo. Probablemente usar Style2d de geotools en lugar
|
559 |
// de FSymbol.
|
560 |
// CAMBIO: La altura del texto la miramos en FLabel
|
561 |
// float alturaPixels = (float) (theSymbol.m_FontSize * mT.getScaleX());
|
562 |
float alturaPixels = (float) (theLabel.getHeight() * at.getScaleX()*FLabel.SQUARE); |
563 |
if (alturaPixels < 3) { |
564 |
return; // No leemos nada |
565 |
} |
566 |
Font nuevaFuente = theSymbol.getFont().deriveFont(alturaPixels);
|
567 |
g2.setFont(nuevaFuente); |
568 |
} |
569 |
|
570 |
|
571 |
if (isSelected){
|
572 |
g2.setColor(MapContext.getSelectionColor()); |
573 |
}else{
|
574 |
g2.setColor(theSymbol.getFontColor()); |
575 |
} |
576 |
pAux = at.transform(theLabel.getOrig(), null);
|
577 |
AffineTransform ant = g2.getTransform();
|
578 |
|
579 |
x = (float) pAux.getX();
|
580 |
y = (float) pAux.getY();
|
581 |
|
582 |
AffineTransform Tx = (AffineTransform) ant.clone(); |
583 |
Tx.translate(x, y); // S3: final translation
|
584 |
Tx.rotate(theLabel.getRotation()); // S2: rotate around anchor
|
585 |
g2.setTransform(Tx); |
586 |
|
587 |
|
588 |
String s=theLabel.getString();
|
589 |
|
590 |
|
591 |
//switch (theLabel.getJustification()) {
|
592 |
|
593 |
//case FLabel.LEFT_BOTTOM:
|
594 |
g2.drawString(s, 0, 0 - 3); |
595 |
/*
|
596 |
break;
|
597 |
|
598 |
case FLabel.LEFT_CENTER:
|
599 |
float height = metrics.getMaxAscent();
|
600 |
g2.drawString(s, 0, 0 - (height / 2));
|
601 |
|
602 |
break;
|
603 |
|
604 |
case FLabel.LEFT_TOP:
|
605 |
height = metrics.getMaxAscent();
|
606 |
g2.drawString(s, 0, 0 - height);
|
607 |
|
608 |
break;
|
609 |
|
610 |
case FLabel.CENTER_BOTTOM:
|
611 |
float width = metrics.stringWidth(s);
|
612 |
g2.drawString(s, 0 - (width / 2), 0 - 3);
|
613 |
|
614 |
break;
|
615 |
|
616 |
case FLabel.CENTER_CENTER:
|
617 |
height = metrics.getMaxAscent();
|
618 |
width = metrics.stringWidth(s);
|
619 |
g2.drawString(s, 0 - (width / 2),
|
620 |
0 - (height / 2));
|
621 |
|
622 |
break;
|
623 |
|
624 |
case FLabel.CENTER_TOP:
|
625 |
width = metrics.stringWidth(s);
|
626 |
height = metrics.getMaxAscent();
|
627 |
g2.drawString(s, 0 - (width / 2), 0 -
|
628 |
height);
|
629 |
|
630 |
break;
|
631 |
|
632 |
case FLabel.RIGHT_BOTTOM:
|
633 |
width = metrics.stringWidth(s);
|
634 |
g2.drawString(s, 0 - width, 0 - 3);
|
635 |
|
636 |
break;
|
637 |
|
638 |
case FLabel.RIGHT_CENTER:
|
639 |
width = metrics.stringWidth(s);
|
640 |
height = metrics.getMaxAscent();
|
641 |
g2.drawString(s, 0 - width, 0 -
|
642 |
(height / 2));
|
643 |
|
644 |
break;
|
645 |
|
646 |
case FLabel.RIGHT_TOP:
|
647 |
width = metrics.stringWidth(s);
|
648 |
height = metrics.getMaxAscent();
|
649 |
g2.drawString(s, 0 - width, 0 - height);
|
650 |
|
651 |
break;
|
652 |
}
|
653 |
*/
|
654 |
///Rectangle2D borde=vp.fromMapRectangle(theLabel.getBoundBox());//theLabel.getBoundingBox();
|
655 |
///g2.setColor(Color.blue);
|
656 |
///g2.drawRect((int)borde.getX(),(int)borde.getY(),(int)borde.getWidth(),(int)borde.getHeight());
|
657 |
// Restauramos
|
658 |
g2.setTransform(ant); |
659 |
} |
660 |
|
661 |
/**
|
662 |
* Dibuja un punto sobre el Graphics2D que se pasa como par�metro.
|
663 |
*
|
664 |
* @param g2 Graphics2D sobre el que dibujar.
|
665 |
* @param mT MAtriz de transformaci�n.
|
666 |
* @param pAux punto a dibujar.
|
667 |
* @param theSymbol S�mbolo a aplicar.
|
668 |
*/
|
669 |
private static void drawSymbolPoint(Graphics2D g2, AffineTransform mT, |
670 |
org.gvsig.fmap.geom.primitive.Point2D pAux, FSymbol theSymbol) { |
671 |
int x;
|
672 |
int y;
|
673 |
x = (int) pAux.getX();
|
674 |
y = (int) pAux.getY();
|
675 |
|
676 |
/*if (x==0){
|
677 |
x=100;
|
678 |
}
|
679 |
if (y==0){
|
680 |
y=100;
|
681 |
}
|
682 |
*/
|
683 |
Rectangle rectAux = new Rectangle(); |
684 |
|
685 |
// Aqu� hay que mirar m_Size y m_useSize...
|
686 |
float radio_simbolo;
|
687 |
radio_simbolo = theSymbol.getSize() / 2;
|
688 |
// theSymbol.setSizeInPixels(true);
|
689 |
|
690 |
if (!theSymbol.isSizeInPixels()) {
|
691 |
// Suponemos que m_Size viene en coordenadas de mundo real
|
692 |
radio_simbolo = (float) (theSymbol.getSize() * mT.getScaleX());
|
693 |
|
694 |
/* System.out.println("m_bUseSize = " + theSymbol.m_bUseSize +
|
695 |
" Escala: " + mT.getScaleX() + " alturaPixels = " + alturaPixels); */
|
696 |
// if (radio_simbolo < 1) return; // No dibujamos nada
|
697 |
rectAux.setRect(x - radio_simbolo, y - radio_simbolo, |
698 |
radio_simbolo * 2, radio_simbolo * 2); |
699 |
} else {
|
700 |
// m_Size viene en pixels
|
701 |
rectAux.setRect(x - radio_simbolo, y - radio_simbolo, |
702 |
theSymbol.getSize(), theSymbol.getSize()); |
703 |
} |
704 |
|
705 |
// continue; //radioSimbolo_en_pixels = 3;
|
706 |
if (theSymbol.getFill() != null) { |
707 |
g2.setPaint(theSymbol.getFill()); |
708 |
} |
709 |
|
710 |
if (theSymbol.getStroke() != null) { |
711 |
g2.setStroke(theSymbol.getStroke()); |
712 |
} |
713 |
|
714 |
if (radio_simbolo < 2) { |
715 |
g2.fillRect(rectAux.x, rectAux.y, rectAux.width, rectAux.height); |
716 |
|
717 |
return;
|
718 |
} |
719 |
|
720 |
switch (theSymbol.getStyle()) {
|
721 |
case ISymbol.SYMBOL_STYLE_MARKER_CIRCLE: // Circulito |
722 |
|
723 |
if (theSymbol.getColor() != null) { |
724 |
g2.fillOval(rectAux.x, rectAux.y, rectAux.width, |
725 |
rectAux.height); |
726 |
} |
727 |
|
728 |
if (theSymbol.isOutlined()) {
|
729 |
g2.setColor(theSymbol.getOutlineColor()); |
730 |
g2.drawOval(rectAux.x, rectAux.y, rectAux.width, |
731 |
rectAux.height); |
732 |
} |
733 |
|
734 |
break;
|
735 |
|
736 |
case ISymbol.SYMBOL_STYLE_MARKER_SQUARE: // Cuadrado |
737 |
case ISymbol.SYMBOL_STYLE_FILL_SOLID:
|
738 |
g2.fillRect(rectAux.x, rectAux.y, rectAux.width, rectAux.height); |
739 |
|
740 |
if (theSymbol.isOutlined()) {
|
741 |
g2.setColor(theSymbol.getOutlineColor()); |
742 |
g2.drawRect(rectAux.x, rectAux.y, rectAux.width, |
743 |
rectAux.height); |
744 |
} |
745 |
|
746 |
break;
|
747 |
|
748 |
case ISymbol.SYMBOL_STYLE_MARKER_TRIANGLE: // Triangulo |
749 |
|
750 |
// y = r*sin30, x = r*cos30
|
751 |
GeneralPathX genPath = new GeneralPathX();
|
752 |
genPath.moveTo(x - (int) (radio_simbolo * 0.866), |
753 |
y + (int) (radio_simbolo * 0.5)); |
754 |
genPath.lineTo(x + (int) (radio_simbolo * 0.866), |
755 |
y + (int) (radio_simbolo * 0.5)); |
756 |
genPath.lineTo(x, y - (float) radio_simbolo);
|
757 |
genPath.closePath(); |
758 |
|
759 |
g2.fill(genPath); |
760 |
|
761 |
break;
|
762 |
|
763 |
case ISymbol.SYMBOL_STYLE_MARKER_CROSS: // cruz |
764 |
case ISymbol.SYMBOL_STYLE_DGNSPECIAL: // Cruz |
765 |
|
766 |
GeneralPathX genPathCruz = new GeneralPathX();
|
767 |
genPathCruz.moveTo(x, y - radio_simbolo); |
768 |
genPathCruz.lineTo(x, y + radio_simbolo); |
769 |
genPathCruz.moveTo(x - radio_simbolo, y); |
770 |
genPathCruz.lineTo(x + radio_simbolo, y); |
771 |
g2.draw(genPathCruz); |
772 |
|
773 |
break;
|
774 |
|
775 |
case 34: // TrueType marker |
776 |
|
777 |
/* lf.lfHeight = -radioSimbolo_en_pixels;
|
778 |
angulo = pSimbolo->m_Rotation; // En radianes, de -pi a pi
|
779 |
angulo = -180.0 * angulo / PI;
|
780 |
|
781 |
lf.lfEscapement = (long) angulo*10;
|
782 |
lf.lfOrientation = (long) angulo*10;
|
783 |
|
784 |
fuente.CreateFontIndirect(&lf);
|
785 |
pOldFont = pDC->SelectObject(&fuente);
|
786 |
|
787 |
pDC->TextOut(pAPI.x, pAPI.y+radioSimbolo_en_pixels/2,elChar,1);
|
788 |
|
789 |
pDC->SelectObject(pOldFont);
|
790 |
fuente.DeleteObject();
|
791 |
|
792 |
break; */
|
793 |
case ISymbol.SYMBOL_STYLE_MARKER_IMAGEN: // Icono |
794 |
{ |
795 |
if (theSymbol.getIcon() != null) { |
796 |
float w;
|
797 |
float h;
|
798 |
|
799 |
if (!theSymbol.isSizeInPixels()) {
|
800 |
// Suponemos que m_Size viene en coordenadas de mundo real
|
801 |
// Por ejemplo, nos valemos del ancho para fijar la escala
|
802 |
w = (float) (theSymbol.getSize() * mT.getScaleX());
|
803 |
h = (theSymbol.getIcon().getHeight(null) * w) / theSymbol.getIcon()
|
804 |
.getWidth(null);
|
805 |
|
806 |
rectAux.setRect(x - w, y - h, w * 2, h * 2); |
807 |
} else {
|
808 |
// m_Size viene en pixels
|
809 |
w = theSymbol.getSize(); |
810 |
h = (theSymbol.getIcon().getHeight(null) * w) / theSymbol.getIcon()
|
811 |
.getWidth(null);
|
812 |
rectAux.setRect(x - w/2, y - h/2, w, h); |
813 |
} |
814 |
/* if (theSymbol.getImgObserver() != null)
|
815 |
{
|
816 |
g2.setColor(Color.WHITE);
|
817 |
g2.fillRect(rectAux.x, rectAux.y, rectAux.width, rectAux.height);
|
818 |
} */
|
819 |
g2.drawImage(theSymbol.getIcon(), rectAux.x, rectAux.y, |
820 |
rectAux.width, rectAux.height, theSymbol.getImgObserver()); |
821 |
} else {
|
822 |
String strImg = "Image"; // Utilities.getMessage(FGraphicUtilities.class,"imagen"); |
823 |
FontMetrics metrics = g2.getFontMetrics();
|
824 |
int width = metrics.stringWidth(strImg);
|
825 |
int height = metrics.getMaxAscent();
|
826 |
|
827 |
g2.drawString(strImg, x - (width / 2), y - 2 + |
828 |
(height / 2));
|
829 |
} |
830 |
|
831 |
break;
|
832 |
} |
833 |
|
834 |
/* DrawIconEx(pDC->m_hDC, pAPI.x-(pSimbolo->m_widthIco/2), pAPI.y-(pSimbolo->m_heightIco/2),
|
835 |
pSimbolo->m_hIcon, pSimbolo->m_widthIco, pSimbolo->m_heightIco, 0 , NULL, DI_NORMAL);
|
836 |
break; */
|
837 |
case Geometry.TYPES.POINT|Geometry.TYPES.Z: // Circulito |
838 |
|
839 |
if (theSymbol.getColor() != null) { |
840 |
g2.fillOval(rectAux.x, rectAux.y, rectAux.width, |
841 |
rectAux.height); |
842 |
} |
843 |
|
844 |
if (theSymbol.isOutlined()) {
|
845 |
g2.setColor(theSymbol.getOutlineColor()); |
846 |
g2.drawOval(rectAux.x, rectAux.y, rectAux.width, |
847 |
rectAux.height); |
848 |
} |
849 |
|
850 |
break;
|
851 |
} // del switch estilo
|
852 |
} |
853 |
public static void DrawHandlers(Graphics2D g, AffineTransform at, |
854 |
Handler[] handlers,ISymbol symbol) { |
855 |
|
856 |
for (int i = 0; i < handlers.length; i++) { |
857 |
Point2D point = handlers[i].getPoint();
|
858 |
at.transform(point, point); |
859 |
g.setColor(((IFillSymbol)symbol).getFillColor()); |
860 |
g.fillRect((int) (point.getX() - 3), (int) (point.getY() - 3), 7, 7); |
861 |
g.setColor(((ILineSymbol)symbol).getColor()); |
862 |
g.drawRect((int) (point.getX() - 5), (int) (point.getY() - 5), 10, 10); |
863 |
g.drawString( "" + i, (int) (point.getX() - 5), (int) (point.getY() - 5)); |
864 |
} |
865 |
} |
866 |
|
867 |
public static void DrawVertex(Graphics2D g, AffineTransform at, Handler handlers) { |
868 |
//for (int i = 0; i < handlers.length; i++) {
|
869 |
Point2D point = handlers.getPoint();
|
870 |
at.transform(point, point); |
871 |
g.setColor(Color.black);
|
872 |
g.drawLine((int)point.getX()-2,(int)point.getY()-10,(int)point.getX()-2,(int)point.getY()+10); |
873 |
g.drawLine((int)point.getX()+2,(int)point.getY()-10,(int)point.getX()+2,(int)point.getY()+10); |
874 |
g.drawLine((int)point.getX()-10,(int)point.getY()-2,(int)point.getX()+10,(int)point.getY()-2); |
875 |
g.drawLine((int)point.getX()-10,(int)point.getY()+2,(int)point.getX()+10,(int)point.getY()+2); |
876 |
g.setColor(Color.red);
|
877 |
g.drawLine((int)point.getX()-1,(int)point.getY()-10,(int)point.getX()-1,(int)point.getY()+10); |
878 |
g.drawLine((int)point.getX()+1,(int)point.getY()-10,(int)point.getX()+1,(int)point.getY()+10); |
879 |
g.drawLine((int)point.getX()-10,(int)point.getY()-1,(int)point.getX()+10,(int)point.getY()-1); |
880 |
g.drawLine((int)point.getX()-10,(int)point.getY()+1,(int)point.getX()+10,(int)point.getY()+1); |
881 |
|
882 |
//}
|
883 |
} |
884 |
} |