Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_911 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / v02 / FGraphicUtilities.java @ 11388

History | View | Annotate | Download (24.4 KB)

1 452 fjp
/*
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 1100 fjp
/* 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 452 fjp
package com.iver.cit.gvsig.fmap.core.v02;
48
49 4213 caballero
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
59 452 fjp
import com.iver.cit.gvsig.fmap.core.FPoint2D;
60
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
61
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
62
import com.iver.cit.gvsig.fmap.core.FShape;
63
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
64 3833 caballero
import com.iver.cit.gvsig.fmap.core.Handler;
65 8765 jjdelcerro
import com.iver.cit.gvsig.fmap.core.ISymbol;
66 452 fjp
import com.vividsolutions.jts.geom.Geometry;
67
import com.vividsolutions.jts.geom.Point;
68
69
70
/**
71 1005 vcaballero
 * Clase con m?todos est?ticos para dibujar sobre el Graphics que se les pase
72
 * como par?metro.
73 8765 jjdelcerro
 *
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 452 fjp
 *
81 1005 vcaballero
 * @author fjp
82 452 fjp
 */
83
public class FGraphicUtilities {
84 4256 caballero
85
86 1005 vcaballero
        /**
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
                FShape shp;
97 452 fjp
98 1005 vcaballero
                AffineTransform mT = new AffineTransform();
99
                mT.setToIdentity();
100 452 fjp
101 1005 vcaballero
                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 452 fjp
106 1005 vcaballero
                // 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 FConstant.SYMBOL_TYPE_POINT:
111
                                shp = new FPoint2D(r.x + (r.width / 2), r.y + (r.height / 2));
112 452 fjp
113 1005 vcaballero
                                //  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 452 fjp
117 1005 vcaballero
                                if (!bAux2) {
118
                                        symbol.setSizeInPixels(true);
119
                                        symbol.setSize(8); // tama?o fijo
120
                                }
121 452 fjp
122 8765 jjdelcerro
                                symbol.draw(g2, mT, shp);
123
                                // FGraphicUtilities.DrawShape(g2, mT, shp, symbol);
124
125 452 fjp
126 1005 vcaballero
                                if (!bAux2) {
127
                                        symbol.setSize(alturaMetros);
128
                                        symbol.setSizeInPixels(bAux2);
129
                                }
130 452 fjp
131 1005 vcaballero
                                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, shp, symbol,
136
                                                new FLabel("Abcd"));
137
                                        symbol.setFontSizeInPixels(bAux);
138
                                }
139 452 fjp
140 1005 vcaballero
                                break;
141 452 fjp
142 1005 vcaballero
                        case FConstant.SYMBOL_TYPE_LINE:
143 452 fjp
144 1005 vcaballero
                                Rectangle rect = mT2.createTransformedShape(r).getBounds();
145
                                GeneralPathX line = new GeneralPathX();
146
                                line.moveTo(rect.x, rect.y + (rect.height / 2));
147 452 fjp
148 3748 caballero
                                // line.lineTo(rect.x + rect.width/3, rect.y + rect.height);
149
                                // line.lineTo(rect.x + 2*rect.width/3, rect.y);
150 1005 vcaballero
                                // 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 452 fjp
156 1005 vcaballero
                                shp = new FPolyline2D(line);
157 8765 jjdelcerro
                                symbol.draw(g2, mT, shp);
158
                                // FGraphicUtilities.DrawShape(g2, mT, shp, symbol);
159 452 fjp
160 1005 vcaballero
                                break;
161 452 fjp
162 1005 vcaballero
                        case FConstant.SYMBOL_TYPE_FILL:
163 810 fjp
164 1005 vcaballero
                                GeneralPathX rectAux = new GeneralPathX(r);
165
                                rectAux.transform(mT2);
166
                                shp = new FPolygon2D(rectAux);
167 810 fjp
168 1005 vcaballero
                                // System.out.println("rect = "+rectAux.getBounds());
169 8765 jjdelcerro
                                symbol.draw(g2, mT, shp);
170
                                // FGraphicUtilities.DrawShape(g2, mT, shp, symbol);
171 810 fjp
172 1005 vcaballero
                                break;
173 843 fjp
174 1005 vcaballero
                        case FShape.MULTI:
175 843 fjp
176 1005 vcaballero
                                // Pol?gono
177
                                r.resize(r.width / 3, r.height);
178 843 fjp
179 1005 vcaballero
                                GeneralPathX rectAux2 = new GeneralPathX(r);
180
                                rectAux2.transform(mT2);
181
                                shp = new FPolygon2D(rectAux2);
182 8765 jjdelcerro
                                symbol.draw(g2, mT, shp);
183
                                // FGraphicUtilities.DrawShape(g2, mT, shp, symbol);
184 843 fjp
185 1005 vcaballero
                                // L?nea
186
                                rect = mT2.createTransformedShape(r2).getBounds();
187
                                line = new GeneralPathX();
188
                                line.moveTo(rect.x, rect.y + (rect.height / 2));
189 843 fjp
190 1005 vcaballero
                                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 843 fjp
195 1005 vcaballero
                                shp = new FPolyline2D(line);
196 8765 jjdelcerro
                                symbol.draw(g2, mT, shp);
197
                                // FGraphicUtilities.DrawShape(g2, mT, shp, symbol);
198 843 fjp
199 1005 vcaballero
                                // Punto:
200
                                shp = new FPoint2D(r3.x + (r3.width / 2), r3.y +
201
                                                (r3.height / 2));
202 452 fjp
203 1005 vcaballero
                                //  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 452 fjp
207 1005 vcaballero
                                if (!bAux2) {
208
                                        symbol.setSizeInPixels(true);
209
                                        symbol.setSize(4); // tama?o fijo
210
                                }
211 8765 jjdelcerro
                                symbol.draw(g2, mT, shp);
212
                                // FGraphicUtilities.DrawShape(g2, mT, shp, symbol);
213 452 fjp
214 1005 vcaballero
                                if (!bAux2) {
215
                                        symbol.setSize(alturaMetros);
216
                                        symbol.setSizeInPixels(bAux2);
217
                                }
218 452 fjp
219 1005 vcaballero
                                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, shp, symbol,
224
                                                new FLabel("Abcd"));
225
                                        symbol.setFontSizeInPixels(bAux);
226
                                }
227 452 fjp
228 1005 vcaballero
                                break;
229 4296 caballero
                        case FConstant.SYMBOL_TYPE_TEXT:
230
                                shp = new FPoint2D(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, shp, symbol,
235
                                        new FLabel("Abcd"));
236
                                symbol.setFontSizeInPixels(bAux);
237
                                break;
238 1005 vcaballero
                }
239
        }
240 452 fjp
241 1005 vcaballero
        /**
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
         */
250
        public static void DrawShape(Graphics2D g2, AffineTransform mT, FShape shp,
251
                FSymbol theSymbol) {
252
                // Hacemos la transformaci?n del shape aqu? dentro... por ahora.
253 3035 fjp
                if (shp == null || theSymbol == null || (!theSymbol.isShapeVisible())) {
254 1005 vcaballero
                        return;
255
                }
256 1402 fjp
        g2.setColor(theSymbol.getColor());
257 452 fjp
258 1005 vcaballero
                /* if (shp instanceof FPolygon2D)
259
                   {
260
                           System.out.println("Entra pol?gono");
261
                   } */
262 2196 vcaballero
                int type=shp.getShapeType();
263 2354 fjp
                /* if (shp.getShapeType()>=FShape.Z){
264 2196 vcaballero
                        type=shp.getShapeType()-FShape.Z;
265 2354 fjp
                } */
266 2196 vcaballero
                switch (type) {
267 1005 vcaballero
                        case FShape.POINT: //Tipo punto
268 2354 fjp
            case FShape.POINT + FShape.Z:
269 1005 vcaballero
                                drawSymbolPoint(g2, mT, (FPoint2D) shp, theSymbol);
270 452 fjp
271 1005 vcaballero
                                break;
272 452 fjp
273 1005 vcaballero
                        case FShape.LINE:
274 2354 fjp
            case FShape.LINE + FShape.Z:
275 3783 caballero
                        case FShape.ARC:
276
                        case FShape.ARC + FShape.Z:
277 1005 vcaballero
                                // Shape theShp = mT.createTransformedShape(shp.m_Polyline);
278
                                // g2.setColor(theSymbol.m_Color);
279
                                if (theSymbol.getStroke() != null) {
280
                                        g2.setStroke(theSymbol.getStroke());
281
                                }
282 452 fjp
283 1005 vcaballero
                                g2.draw(shp);
284 452 fjp
285 1005 vcaballero
                                break;
286 452 fjp
287 1005 vcaballero
                        case FShape.POLYGON:
288 2354 fjp
            case FShape.POLYGON + FShape.Z:
289 6013 fjp
                        case FShape.ELLIPSE:
290
                        case FShape.ELLIPSE + FShape.Z:
291
            case FShape.CIRCLE:
292
                        case FShape.CIRCLE + FShape.Z:
293
294 1484 fjp
                            if (theSymbol.getFill() != null)
295
                                g2.setPaint(theSymbol.getFill());
296
297 3748 caballero
                            if (theSymbol.getColor() != null)
298 1402 fjp
                                    if (theSymbol.getStyle() != FConstant.SYMBOL_STYLE_DGNSPECIAL) {
299
                                        g2.fill(shp);
300 3748 caballero
                                }
301 452 fjp
302 1005 vcaballero
                                if (theSymbol.isOutlined()) {
303
                                        g2.setColor(theSymbol.getOutlineColor());
304 452 fjp
305 1005 vcaballero
                                        if (theSymbol.getStroke() != null) {
306
                                                g2.setStroke(theSymbol.getStroke());
307
                                        }
308 452 fjp
309 1005 vcaballero
                                        g2.draw(shp);
310
                                }
311 452 fjp
312 1005 vcaballero
                                break;
313 3748 caballero
314 1005 vcaballero
                }
315
        }
316 452 fjp
317 1005 vcaballero
        /**
318
         * Dibuja el FLabel que se pasa como par?metro sobre el Graphics2D.
319
         *
320
         * @param g2 Graphics2D sobre el que dibujar.
321
         * @param mT Matriz de transformaci?n.
322
         * @param shp FShape a dibujar.
323
         * @param theSymbol S?mbolo para aplicar.
324
         * @param theLabel FLabel que contiene el texto que se debe dibujar.
325
         */
326
        public static void DrawLabel(Graphics2D g2, AffineTransform mT, FShape shp,
327
                FSymbol theSymbol, FLabel theLabel) {
328
                float angle;
329
                float x;
330
                float y;
331
                Point2D pAux = null;
332 452 fjp
333 1005 vcaballero
                // USAR TEXTLAYOUT SI QUEREMOS PERMITIR SELECCIONAR UN TEXTO
334
                // Y/O EDITARLO "IN SITU"
335 452 fjp
336 1005 vcaballero
                /* if (m_labelValues[numReg].length() > 0)
337
                   {
338
                           TextLayout layout = new TextLayout(m_labelValues[numReg], font, frc);
339
                           layout.draw(g2, x, y);
340
                   } */
341
                if (shp == null) {
342
                        return;
343
                }
344 452 fjp
345 1005 vcaballero
                // Las etiquetas que pongamos a nulo ser? porque no la queremos dibujar.
346
                // ?til para cuando queramos eliminar duplicados.
347
                if (theLabel.getString() == null) {
348
                        return;
349
                }
350 452 fjp
351 1005 vcaballero
                FontMetrics metrics = g2.getFontMetrics();
352
                int width = metrics.stringWidth(theLabel.getString());
353
                int height = metrics.getMaxAscent();
354 452 fjp
355 1005 vcaballero
                // int height = metrics.getHeight();
356
                g2.setFont(theSymbol.getFont());
357
                g2.setColor(theSymbol.getFontColor());
358 452 fjp
359 1005 vcaballero
                // Aqu? hay que mirar m_Size y m_useSize...
360
                if (!theSymbol.isFontSizeInPixels()) {
361
                        // Suponemos que m_Size viene en coordenadas de mundo real
362
                        // Esto habr? que cambiarlo. Probablemente usar Style2d de geotools en lugar
363
                        // de FSymbol.
364
                        // CAMBIO: La altura del texto la miramos en FLabel
365
                        // float alturaPixels = (float) (theSymbol.m_FontSize * mT.getScaleX());
366
                        float alturaPixels = (float) (theLabel.getHeight() * mT.getScaleX());
367 452 fjp
368 1005 vcaballero
                        /* System.out.println("m_bUseSize = " + theSymbol.m_bUseSize +
369
                           " Escala: " + mT.getScaleX() + " alturaPixels = " + alturaPixels); */
370
                        if (alturaPixels < 3) {
371
                                return; // No leemos nada
372
                        }
373 452 fjp
374 1005 vcaballero
                        Font nuevaFuente = theSymbol.getFont().deriveFont(alturaPixels);
375
                        g2.setFont(nuevaFuente);
376
                        width = g2.getFontMetrics().stringWidth(theLabel.getString());
377
                }
378 452 fjp
379 2196 vcaballero
                int type=shp.getShapeType();
380
                if (shp.getShapeType()>=FShape.Z){
381
                        type=shp.getShapeType()-FShape.Z;
382
                }
383
                switch (type) {
384 1005 vcaballero
                        case FShape.POINT: //Tipo punto
385
                                pAux = new Point2D.Double(((FPoint2D) shp).getX(),
386
                                                ((FPoint2D) shp).getY());
387
                                pAux = mT.transform(pAux, null);
388 452 fjp
389 1005 vcaballero
                                break;
390 452 fjp
391 1005 vcaballero
                        case FShape.LINE:
392 452 fjp
393 3748 caballero
                                //
394 1005 vcaballero
                                if (theLabel.getOrig() == null) // Calculamos el punto y la orientaci?n solo la primera vez
395
                                 {
396
                                        PathLength pathLen = new PathLength(shp);
397 452 fjp
398 1005 vcaballero
                                        // if (pathLen.lengthOfPath() < width / mT.getScaleX()) return;
399
                                        float midDistance = pathLen.lengthOfPath() / 2;
400
                                        pAux = pathLen.pointAtLength(midDistance);
401
                                        angle = pathLen.angleAtLength(midDistance);
402 452 fjp
403 1005 vcaballero
                                        if (angle < 0) {
404
                                                angle = angle + (float) (2 * Math.PI);
405
                                        }
406 452 fjp
407 1005 vcaballero
                                        if ((angle > (Math.PI / 2)) &&
408
                                                        (angle < ((3 * Math.PI) / 2))) {
409
                                                angle = angle - (float) Math.PI;
410
                                        }
411 452 fjp
412 1005 vcaballero
                                        theLabel.setRotation(Math.toDegrees(angle));
413
                                        theLabel.setOrig(pAux);
414
                                }
415 452 fjp
416 1005 vcaballero
                                pAux = mT.transform(theLabel.getOrig(), null);
417 452 fjp
418 1005 vcaballero
                                // pAux = theLabel.getOrig();
419
                                // GlyphVector theGlyphs = theSymbol.m_Font.createGlyphVector(g2.getFontRenderContext(), theLabel);
420 3748 caballero
                                // Shape txtShp = TextPathLayout.layoutGlyphVector(theGlyphs, shp.m_Polyline,TextPathLayout.ALIGN_MIDDLE);
421 1005 vcaballero
                                // g2.draw(txtShp);
422
                                // System.out.println("Pintando etiqueta " + theLabel );
423
                                break;
424 452 fjp
425 1005 vcaballero
                        case FShape.POLYGON:
426 452 fjp
427 1005 vcaballero
                                if (theLabel.getOrig() == null) // Calculamos el punto solo la primera vez
428
                                 {
429
                                        Geometry geo = FConverter.java2d_to_jts(shp);
430 452 fjp
431 1005 vcaballero
                                        // System.out.println("Area de " + m_labelValues[numReg] + " = "
432
                                        //   + geo.getArea());
433 3748 caballero
                                        //   System.out.println(geo.toText());
434 1005 vcaballero
                                        Point pJTS = geo.getInteriorPoint();
435
                                        FShape pLabel = FConverter.jts_to_java2d(pJTS);
436
                                        theLabel.setRotation(0);
437
                                        theLabel.setOrig(new Point2D.Double(
438
                                                        ((FPoint2D) pLabel).getX(),
439
                                                        ((FPoint2D) pLabel).getX()));
440
                                }
441 452 fjp
442 1005 vcaballero
                                pAux = mT.transform(theLabel.getOrig(), null);
443 452 fjp
444 1005 vcaballero
                                break;
445
                }
446 452 fjp
447 1005 vcaballero
                AffineTransform ant = g2.getTransform();
448 452 fjp
449 1005 vcaballero
                x = (float) pAux.getX();
450
                y = (float) pAux.getY();
451 452 fjp
452 1005 vcaballero
                AffineTransform Tx = (AffineTransform) ant.clone();
453
                Tx.translate(x, y); // S3: final translation
454 4226 caballero
                Tx.rotate(theLabel.getRotation()); // S2: rotate around anchor
455 1005 vcaballero
                g2.setTransform(Tx);
456 452 fjp
457 1005 vcaballero
                switch (theLabel.getJustification()) {
458
                        case FLabel.LEFT_BOTTOM:
459
                                g2.drawString(theLabel.getString(), 0, 0 - 3);
460 452 fjp
461 1005 vcaballero
                                break;
462 452 fjp
463 1005 vcaballero
                        case FLabel.LEFT_CENTER:
464
                                g2.drawString(theLabel.getString(), 0, 0 - (height / 2));
465 452 fjp
466 1005 vcaballero
                                break;
467 452 fjp
468 1005 vcaballero
                        case FLabel.LEFT_TOP:
469
                                g2.drawString(theLabel.getString(), 0, 0 - height);
470 452 fjp
471 1005 vcaballero
                                break;
472 452 fjp
473 1005 vcaballero
                        case FLabel.CENTER_BOTTOM:
474
                                g2.drawString(theLabel.getString(), 0 - (width / 2), 0 - 3);
475 452 fjp
476 1005 vcaballero
                                break;
477 452 fjp
478 1005 vcaballero
                        case FLabel.CENTER_CENTER:
479
                                g2.drawString(theLabel.getString(), 0 - (width / 2),
480
                                        0 - (height / 2));
481 452 fjp
482 1005 vcaballero
                                break;
483 452 fjp
484 1005 vcaballero
                        case FLabel.CENTER_TOP:
485
                                g2.drawString(theLabel.getString(), 0 - (width / 2), 0 -
486
                                        height);
487 452 fjp
488 1005 vcaballero
                                break;
489
490
                        case FLabel.RIGHT_BOTTOM:
491
                                g2.drawString(theLabel.getString(), 0 - width, 0 - 3);
492
493
                                break;
494
495
                        case FLabel.RIGHT_CENTER:
496
                                g2.drawString(theLabel.getString(), 0 - width, 0 -
497
                                        (height / 2));
498
499
                                break;
500
501
                        case FLabel.RIGHT_TOP:
502
                                g2.drawString(theLabel.getString(), 0 - width, 0 - height);
503
504
                                break;
505
                }
506
507
                // Restauramos
508
                g2.setTransform(ant);
509
        }
510 4213 caballero
        /**
511
         * Dibuja el FLabel que se pasa como par?metro sobre el Graphics2D.
512
         *
513
         * @param g2 Graphics2D sobre el que dibujar.
514
         * @param mT Matriz de transformaci?n.
515
         * @param shp FShape a dibujar.
516
         * @param theSymbol S?mbolo para aplicar.
517
         * @param theLabel FLabel que contiene el texto que se debe dibujar.
518
         */
519 4256 caballero
        public static void DrawAnnotation(Graphics2D g2, AffineTransform at,
520
                FSymbol theSymbol, FLabel theLabel,FontMetrics metrics,boolean isSelected) {
521 4213 caballero
                float x;
522
                float y;
523
                Point2D pAux = null;
524
                // Las etiquetas que pongamos a nulo ser? porque no la queremos dibujar.
525
                // ?til para cuando queramos eliminar duplicados.
526
                if (theLabel.getString() == null) {
527
                        return;
528
                }
529
530 4296 caballero
//                 Aqu? hay que mirar m_Size y m_useSize...
531
                if (!theSymbol.isFontSizeInPixels()) {
532 4213 caballero
                        // Suponemos que m_Size viene en coordenadas de mundo real
533
                        // Esto habr? que cambiarlo. Probablemente usar Style2d de geotools en lugar
534
                        // de FSymbol.
535
                        // CAMBIO: La altura del texto la miramos en FLabel
536
                        // float alturaPixels = (float) (theSymbol.m_FontSize * mT.getScaleX());
537 4296 caballero
                        float alturaPixels = (float) (theLabel.getHeight() * at.getScaleX()*FLabel.SQUARE);
538 4213 caballero
                        if (alturaPixels < 3) {
539
                                return; // No leemos nada
540
                        }
541 4296 caballero
                        Font nuevaFuente = theSymbol.getFont().deriveFont(alturaPixels);
542 4213 caballero
                        g2.setFont(nuevaFuente);
543
                }
544
545 4296 caballero
546
                if (isSelected){
547
                        g2.setColor(FSymbol.getSelectionColor());
548
                }else{
549
                        g2.setColor(theSymbol.getFontColor());
550
                }
551
                pAux = at.transform(theLabel.getOrig(), null);
552 4213 caballero
                AffineTransform ant = g2.getTransform();
553
554
                x = (float) pAux.getX();
555
                y = (float) pAux.getY();
556
557
                AffineTransform Tx = (AffineTransform) ant.clone();
558
                Tx.translate(x, y); // S3: final translation
559 4226 caballero
                Tx.rotate(theLabel.getRotation()); // S2: rotate around anchor
560 4213 caballero
                g2.setTransform(Tx);
561
562 4256 caballero
563
                String s=theLabel.getString();
564
565
566 5511 caballero
                //switch (theLabel.getJustification()) {
567 4213 caballero
568 5511 caballero
                 //case FLabel.LEFT_BOTTOM:
569 4226 caballero
                                g2.drawString(s, 0, 0 - 3);
570 5511 caballero
/*
571 4213 caballero
                                break;
572 4226 caballero

573 4213 caballero
                        case FLabel.LEFT_CENTER:
574 4256 caballero
                                float height = metrics.getMaxAscent();
575 4226 caballero
                                g2.drawString(s, 0, 0 - (height / 2));
576

577 4213 caballero
                                break;
578 4226 caballero

579 4213 caballero
                        case FLabel.LEFT_TOP:
580 4256 caballero
                                height = metrics.getMaxAscent();
581 4226 caballero
                                g2.drawString(s, 0, 0 - height);
582

583 4213 caballero
                                break;
584 4226 caballero

585 4213 caballero
                        case FLabel.CENTER_BOTTOM:
586 4256 caballero
                                float width = metrics.stringWidth(s);
587 4226 caballero
                                g2.drawString(s, 0 - (width / 2), 0 - 3);
588

589 4213 caballero
                                break;
590 4226 caballero

591 4213 caballero
                        case FLabel.CENTER_CENTER:
592 4256 caballero
                                height = metrics.getMaxAscent();
593
                                width = metrics.stringWidth(s);
594 4226 caballero
                                g2.drawString(s, 0 - (width / 2),
595
                                        0 - (height / 2));
596

597 4213 caballero
                                break;
598 4226 caballero

599 4213 caballero
                        case FLabel.CENTER_TOP:
600 4256 caballero
                                width = metrics.stringWidth(s);
601
                                height = metrics.getMaxAscent();
602 4226 caballero
                                g2.drawString(s, 0 - (width / 2), 0 -
603
                                        height);
604

605 4213 caballero
                                break;
606 4226 caballero

607 4213 caballero
                        case FLabel.RIGHT_BOTTOM:
608 4256 caballero
                                width = metrics.stringWidth(s);
609 4226 caballero
                                g2.drawString(s, 0 - width, 0 - 3);
610

611 4213 caballero
                                break;
612 4226 caballero

613 4213 caballero
                        case FLabel.RIGHT_CENTER:
614 4256 caballero
                                width = metrics.stringWidth(s);
615
                                height = metrics.getMaxAscent();
616 4226 caballero
                                g2.drawString(s, 0 - width, 0 -
617
                                        (height / 2));
618

619 4213 caballero
                                break;
620 4226 caballero

621 4213 caballero
                        case FLabel.RIGHT_TOP:
622 4256 caballero
                                width = metrics.stringWidth(s);
623
                                height = metrics.getMaxAscent();
624 4226 caballero
                                g2.drawString(s, 0 - width, 0 - height);
625

626 4213 caballero
                                break;
627
                }
628 5511 caballero
                */
629 4256 caballero
                ///Rectangle2D borde=vp.fromMapRectangle(theLabel.getBoundBox());//theLabel.getBoundingBox();
630
                ///g2.setColor(Color.blue);
631
                ///g2.drawRect((int)borde.getX(),(int)borde.getY(),(int)borde.getWidth(),(int)borde.getHeight());
632 4213 caballero
                // Restauramos
633
                g2.setTransform(ant);
634
        }
635
636 1005 vcaballero
        /**
637
         * Dibuja un punto sobre el Graphics2D que se pasa como par?metro.
638
         *
639
         * @param g2 Graphics2D sobre el que dibujar.
640
         * @param mT MAtriz de transformaci?n.
641
         * @param pAux punto a dibujar.
642
         * @param theSymbol S?mbolo a aplicar.
643
         */
644
        private static void drawSymbolPoint(Graphics2D g2, AffineTransform mT,
645
                FPoint2D pAux, FSymbol theSymbol) {
646
                int x;
647
                int y;
648
                x = (int) pAux.getX();
649
                y = (int) pAux.getY();
650
651 452 fjp
                /*if (x==0){
652 1005 vcaballero
                   x=100;
653
                   }
654
                   if (y==0){
655
                           y=100;
656
                   }
657
                 */
658
                Rectangle rectAux = new Rectangle();
659
660
                // Aqu? hay que mirar m_Size y m_useSize...
661
                float radio_simbolo;
662
                radio_simbolo = theSymbol.getSize() / 2;
663 5259 fjp
                // theSymbol.setSizeInPixels(true);
664 1005 vcaballero
665
                if (!theSymbol.isSizeInPixels()) {
666
                        // Suponemos que m_Size viene en coordenadas de mundo real
667
                        radio_simbolo = (float) (theSymbol.getSize() * mT.getScaleX());
668
669
                        /* System.out.println("m_bUseSize = " + theSymbol.m_bUseSize +
670
                           " Escala: " + mT.getScaleX() + " alturaPixels = " + alturaPixels); */
671
                        // if (radio_simbolo < 1) return; // No dibujamos nada
672
                        rectAux.setRect(x - radio_simbolo, y - radio_simbolo,
673
                                radio_simbolo * 2, radio_simbolo * 2);
674
                } else {
675
                        // m_Size viene en pixels
676
                        rectAux.setRect(x - radio_simbolo, y - radio_simbolo,
677
                                theSymbol.getSize(), theSymbol.getSize());
678 452 fjp
                }
679 1005 vcaballero
680
                //         continue; //radioSimbolo_en_pixels = 3;
681
                if (theSymbol.getFill() != null) {
682
                        g2.setPaint(theSymbol.getFill());
683 452 fjp
                }
684
685 1005 vcaballero
                if (theSymbol.getStroke() != null) {
686
                        g2.setStroke(theSymbol.getStroke());
687
                }
688 452 fjp
689 1005 vcaballero
                if (radio_simbolo < 2) {
690
                        g2.fillRect(rectAux.x, rectAux.y, rectAux.width, rectAux.height);
691 452 fjp
692 1005 vcaballero
                        return;
693
                }
694 452 fjp
695 1005 vcaballero
                switch (theSymbol.getStyle()) {
696
                        case FConstant.SYMBOL_STYLE_MARKER_CIRCLE: // Circulito
697 452 fjp
698 1005 vcaballero
                                if (theSymbol.getColor() != null) {
699
                                        g2.fillOval(rectAux.x, rectAux.y, rectAux.width,
700
                                                rectAux.height);
701
                                }
702 452 fjp
703 1005 vcaballero
                                if (theSymbol.isOutlined()) {
704
                                        g2.setColor(theSymbol.getOutlineColor());
705
                                        g2.drawOval(rectAux.x, rectAux.y, rectAux.width,
706
                                                rectAux.height);
707
                                }
708 452 fjp
709 1005 vcaballero
                                break;
710 452 fjp
711 1005 vcaballero
                        case FConstant.SYMBOL_STYLE_MARKER_SQUARE: // Cuadrado
712 5259 fjp
                        case FConstant.SYMBOL_STYLE_FILL_SOLID:
713 1005 vcaballero
                                g2.fillRect(rectAux.x, rectAux.y, rectAux.width, rectAux.height);
714 452 fjp
715 1005 vcaballero
                                if (theSymbol.isOutlined()) {
716
                                        g2.setColor(theSymbol.getOutlineColor());
717
                                        g2.drawRect(rectAux.x, rectAux.y, rectAux.width,
718
                                                rectAux.height);
719
                                }
720 452 fjp
721 1005 vcaballero
                                break;
722 452 fjp
723 1005 vcaballero
                        case FConstant.SYMBOL_STYLE_MARKER_TRIANGLE: // Triangulo
724 452 fjp
725 1005 vcaballero
                                // y = r*sin30, x = r*cos30
726
                                GeneralPathX genPath = new GeneralPathX();
727
                                genPath.moveTo(x - (int) (radio_simbolo * 0.866),
728
                                        y + (int) (radio_simbolo * 0.5));
729
                                genPath.lineTo(x + (int) (radio_simbolo * 0.866),
730
                                        y + (int) (radio_simbolo * 0.5));
731
                                genPath.lineTo(x, y - (float) radio_simbolo);
732
                                genPath.closePath();
733 452 fjp
734 1005 vcaballero
                                g2.fill(genPath);
735 452 fjp
736 1005 vcaballero
                                break;
737 452 fjp
738 1402 fjp
                        case FConstant.SYMBOL_STYLE_MARKER_CROSS: // cruz
739
                        case FConstant.SYMBOL_STYLE_DGNSPECIAL: // Cruz
740 452 fjp
741 1005 vcaballero
                                GeneralPathX genPathCruz = new GeneralPathX();
742
                                genPathCruz.moveTo(x, y - radio_simbolo);
743
                                genPathCruz.lineTo(x, y + radio_simbolo);
744
                                genPathCruz.moveTo(x - radio_simbolo, y);
745
                                genPathCruz.lineTo(x + radio_simbolo, y);
746
                                g2.draw(genPathCruz);
747 452 fjp
748 1005 vcaballero
                                break;
749 452 fjp
750 1005 vcaballero
                        case 34: // TrueType marker
751 452 fjp
752 1005 vcaballero
                        /* lf.lfHeight = -radioSimbolo_en_pixels;
753
                           angulo = pSimbolo->m_Rotation;  // En radianes, de -pi a pi
754
                           angulo = -180.0 * angulo / PI;
755 3748 caballero

756 1005 vcaballero
                           lf.lfEscapement = (long) angulo*10;
757
                           lf.lfOrientation = (long) angulo*10;
758 3748 caballero

759 1005 vcaballero
                           fuente.CreateFontIndirect(&lf);
760
                           pOldFont = pDC->SelectObject(&fuente);
761 3748 caballero

762 1005 vcaballero
                           pDC->TextOut(pAPI.x, pAPI.y+radioSimbolo_en_pixels/2,elChar,1);
763 3748 caballero

764 1005 vcaballero
                           pDC->SelectObject(pOldFont);
765
                           fuente.DeleteObject();
766 3748 caballero

767 1005 vcaballero
                           break; */
768
                        case FConstant.SYMBOL_STYLE_MARKER_IMAGEN: // Icono
769
                         {
770
                                if (theSymbol.getIcon() != null) {
771
                                        float w;
772
                                        float h;
773 452 fjp
774 1836 fernando
                                        if (!theSymbol.isSizeInPixels()) {
775 1005 vcaballero
                                                // Suponemos que m_Size viene en coordenadas de mundo real
776
                                                // Por ejemplo, nos valemos del ancho para fijar la escala
777
                                                w = (float) (theSymbol.getSize() * mT.getScaleX());
778
                                                h = (theSymbol.getIcon().getHeight(null) * w) / theSymbol.getIcon()
779
                                                                                                                                                                 .getWidth(null);
780 452 fjp
781 1005 vcaballero
                                                rectAux.setRect(x - w, y - h, w * 2, h * 2);
782
                                        } else {
783
                                                // m_Size viene en pixels
784
                                                w = theSymbol.getSize();
785
                                                h = (theSymbol.getIcon().getHeight(null) * w) / theSymbol.getIcon()
786
                                                                                                                                                                 .getWidth(null);
787 3589 caballero
                                                rectAux.setRect(x - w/2, y - h/2, w, h);
788 1005 vcaballero
                                        }
789 4757 fjp
                                        /* if (theSymbol.getImgObserver() != null)
790
                                        {
791
                                                g2.setColor(Color.WHITE);
792
                                                g2.fillRect(rectAux.x, rectAux.y, rectAux.width, rectAux.height);
793
                                        } */
794 1005 vcaballero
                                        g2.drawImage(theSymbol.getIcon(), rectAux.x, rectAux.y,
795 4757 fjp
                                                rectAux.width, rectAux.height, theSymbol.getImgObserver());
796 1005 vcaballero
                                } else {
797 3748 caballero
                                        String strImg = "Image"; // Utilities.getMessage(FGraphicUtilities.class,"imagen");
798 1005 vcaballero
                                        FontMetrics metrics = g2.getFontMetrics();
799
                                        int width = metrics.stringWidth(strImg);
800
                                        int height = metrics.getMaxAscent();
801 452 fjp
802 1005 vcaballero
                                        g2.drawString(strImg, x - (width / 2), y - 2 +
803
                                                (height / 2));
804
                                }
805 452 fjp
806 1005 vcaballero
                                break;
807
                        }
808 452 fjp
809 1005 vcaballero
                        /* DrawIconEx(pDC->m_hDC, pAPI.x-(pSimbolo->m_widthIco/2), pAPI.y-(pSimbolo->m_heightIco/2),
810
                           pSimbolo->m_hIcon, pSimbolo->m_widthIco, pSimbolo->m_heightIco, 0 , NULL, DI_NORMAL);
811
                           break; */
812
                        case FConstant.SYMBOL_STYLE_POINTZ: // Circulito
813 452 fjp
814 1005 vcaballero
                                if (theSymbol.getColor() != null) {
815
                                        g2.fillOval(rectAux.x, rectAux.y, rectAux.width,
816
                                                rectAux.height);
817
                                }
818 452 fjp
819 1005 vcaballero
                                if (theSymbol.isOutlined()) {
820
                                        g2.setColor(theSymbol.getOutlineColor());
821
                                        g2.drawOval(rectAux.x, rectAux.y, rectAux.width,
822
                                                rectAux.height);
823
                                }
824 452 fjp
825 1005 vcaballero
                                break;
826
                } // del switch estilo
827
        }
828 3833 caballero
        public static void DrawHandlers(Graphics2D g, AffineTransform at,
829
                        Handler[] handlers) {
830 3748 caballero
831 3833 caballero
                        for (int i = 0; i < handlers.length; i++) {
832
                                Point2D point = handlers[i].getPoint();
833
                                at.transform(point, point);
834 3886 caballero
                                g.setColor(Color.orange);
835
                                g.fillRect((int) (point.getX() - 3), (int) (point.getY() - 3), 7, 7);
836
                                g.setColor(Color.darkGray);
837 3833 caballero
                                g.drawRect((int) (point.getX() - 5), (int) (point.getY() - 5), 10, 10);
838 6013 fjp
                                g.drawString( "" + i, (int) (point.getX() - 5), (int) (point.getY() - 5));
839 3833 caballero
                        }
840
                }
841 4118 caballero
842
        public static void DrawVertex(Graphics2D g, AffineTransform at, Handler handlers) {
843
                //for (int i = 0; i < handlers.length; i++) {
844
                        Point2D point = handlers.getPoint();
845
                        at.transform(point, point);
846
                        g.setColor(Color.red);
847
                        g.drawLine((int)point.getX()-1,(int)point.getY()-10,(int)point.getX()-1,(int)point.getY()+10);
848
                        g.drawLine((int)point.getX()+1,(int)point.getY()-10,(int)point.getX()+1,(int)point.getY()+10);
849
                        g.drawLine((int)point.getX()-10,(int)point.getY()-1,(int)point.getX()+10,(int)point.getY()-1);
850
                        g.drawLine((int)point.getX()-10,(int)point.getY()+1,(int)point.getX()+10,(int)point.getY()+1);
851
                //}
852
        }
853 452 fjp
}