Statistics
| Revision:

svn-gvsig-desktop / tags / J2ME_compat_v1_2_Build_1209 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / symbols / MarkerFillSymbol.java @ 19509

History | View | Annotate | Download (13.1 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

    
42
/* CVS MESSAGES:
43
*
44
* $Id: MarkerFillSymbol.java 16176 2007-11-08 16:07:26Z jdominguez $
45
* $Log$
46
* Revision 1.19  2007-09-21 12:25:32  jaume
47
* cancellation support extended down to the IGeometry and ISymbol level
48
*
49
* Revision 1.18  2007/09/20 11:53:11  jvidal
50
* bug solved
51
*
52
* Revision 1.16  2007/08/09 10:39:41  jaume
53
* first round of found bugs fixed
54
*
55
* Revision 1.15  2007/08/08 12:04:05  jvidal
56
* javadoc
57
*
58
* Revision 1.14  2007/08/03 09:22:09  jaume
59
* refactored class names
60
*
61
* Revision 1.13  2007/08/02 11:13:50  jaume
62
* char encoding fix
63
*
64
* Revision 1.12  2007/08/01 11:45:59  jaume
65
* passing general tests (drawing test yet missing)
66
*
67
* Revision 1.11  2007/07/23 06:52:25  jaume
68
* default selection color refactored, moved to MapContext
69
*
70
* Revision 1.10  2007/05/28 15:36:42  jaume
71
* *** empty log message ***
72
*
73
* Revision 1.9  2007/05/08 08:47:40  jaume
74
* *** empty log message ***
75
*
76
* Revision 1.8  2007/03/28 16:48:14  jaume
77
* *** empty log message ***
78
*
79
* Revision 1.7  2007/03/26 14:25:17  jaume
80
* implements IPrintable
81
*
82
* Revision 1.6  2007/03/21 17:36:22  jaume
83
* *** empty log message ***
84
*
85
* Revision 1.5  2007/03/13 16:58:36  jaume
86
* Added QuantityByCategory (Multivariable legend) and some bugfixes in symbols
87
*
88
* Revision 1.4  2007/03/09 11:20:57  jaume
89
* Advanced symbology (start committing)
90
*
91
* Revision 1.2.2.4  2007/02/16 10:54:12  jaume
92
* multilayer splitted to multilayerline, multilayermarker,and  multilayerfill
93
*
94
* Revision 1.2.2.3  2007/02/15 16:23:44  jaume
95
* *** empty log message ***
96
*
97
* Revision 1.2.2.2  2007/02/12 15:15:20  jaume
98
* refactored interval legend and added graduated symbol legend
99
*
100
* Revision 1.2.2.1  2007/02/09 07:47:05  jaume
101
* Isymbol moved
102
*
103
* Revision 1.2  2007/01/10 16:39:41  jaume
104
* ISymbol now belongs to com.iver.cit.gvsig.fmap.core.symbols package
105
*
106
* Revision 1.1  2007/01/10 16:31:36  jaume
107
* *** empty log message ***
108
*
109
* Revision 1.10  2006/11/09 18:39:05  jaume
110
* *** empty log message ***
111
*
112
* Revision 1.9  2006/11/09 10:22:50  jaume
113
* *** empty log message ***
114
*
115
* Revision 1.8  2006/11/08 13:05:51  jaume
116
* *** empty log message ***
117
*
118
* Revision 1.7  2006/11/08 10:56:47  jaume
119
* *** empty log message ***
120
*
121
* Revision 1.6  2006/11/07 08:52:30  jaume
122
* *** empty log message ***
123
*
124
* Revision 1.5  2006/11/06 17:08:45  jaume
125
* *** empty log message ***
126
*
127
* Revision 1.4  2006/11/06 16:06:52  jaume
128
* *** empty log message ***
129
*
130
* Revision 1.3  2006/11/06 07:33:54  jaume
131
* javadoc, source style
132
*
133
* Revision 1.2  2006/10/31 16:16:34  jaume
134
* *** empty log message ***
135
*
136
* Revision 1.1  2006/10/30 19:30:35  jaume
137
* *** empty log message ***
138
*
139
*
140
*/
141
package com.iver.cit.gvsig.fmap.core.symbols;
142

    
143
import java.awt.Color;
144
import java.awt.Graphics2D;
145
import java.awt.Paint;
146
import java.awt.Rectangle;
147
import java.awt.RenderingHints;
148
import java.awt.Shape;
149
import java.awt.TexturePaint;
150
import java.awt.geom.AffineTransform;
151
import java.awt.geom.Point2D;
152
import java.awt.image.BufferedImage;
153
import java.util.Random;
154

    
155
import javax.print.attribute.PrintRequestAttributeSet;
156

    
157
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
158
import com.iver.cit.gvsig.fmap.MapContext;
159
import com.iver.cit.gvsig.fmap.core.FPoint2D;
160
import com.iver.cit.gvsig.fmap.core.FShape;
161
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
162
import com.iver.cit.gvsig.fmap.core.styles.IMarkerFillPropertiesStyle;
163
import com.iver.cit.gvsig.fmap.core.styles.SimpleMarkerFillPropertiesStyle;
164
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
165
import com.iver.utiles.StringUtilities;
166
import com.iver.utiles.XMLEntity;
167
import com.iver.utiles.swing.threads.Cancellable;
168
import com.vividsolutions.jts.geom.Geometry;
169

    
170
/**
171
 * Allows to define a marker symbol of any type as a path image to be used for a filled of a
172
 * polygon's padding
173
 *
174
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
175
 */
176
public class MarkerFillSymbol extends AbstractFillSymbol {
177
        public static final int RANDOM_FILL = 3;
178
        public static final int GRID_FILL = 1;
179
        public static final int SINGLE_CENTERED_SYMBOL = 2;
180
        private MarkerFillSymbol selectionSymbol;
181
        private IMarkerFillPropertiesStyle markerFillProperties = new SimpleMarkerFillPropertiesStyle();
182
        private IMarkerSymbol markerSymbol = SymbologyFactory.createDefaultMarkerSymbol();
183

    
184

    
185

    
186

    
187
        public ISymbol getSymbolForSelection() {
188
                if (selectionSymbol == null) {
189
                        selectionSymbol = (MarkerFillSymbol) SymbologyFactory.createSymbolFromXML(getXMLEntity(), null);
190
                        selectionSymbol.setFillColor(MapContext.getSelectionColor());
191
                }
192

    
193
                return selectionSymbol;
194
        }
195

    
196
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) {
197
                switch (markerFillProperties.getFillStyle()) {
198
                case SINGLE_CENTERED_SYMBOL:
199
                        // case a single marker is used into a polygon shapetype
200
                        Geometry geom = FConverter.java2d_to_jts(shp);
201
                        com.vividsolutions.jts.geom.Point centroid = geom.getCentroid();
202
                        FPoint2D p = new FPoint2D(new Point2D.Double(
203
                                        centroid.getX()+markerFillProperties.getXOffset(),
204
                                        centroid.getY()+markerFillProperties.getYOffset()));
205
                        markerSymbol.draw(g, affineTransform, p, null);
206
                        break;
207
                case GRID_FILL:
208
                        // case a grid fill is used
209
                        {
210
                        g.setClip(shp);
211
                        int size = (int) markerSymbol.getSize();
212
                        Rectangle rProv = new Rectangle();
213
                        rProv.setFrame(0, 0, size, size);
214
                        Paint resulPatternFill = null;
215

    
216
                        BufferedImage sample = null;
217
                        sample = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
218
                        Graphics2D gAux = sample.createGraphics();
219

    
220
                        double xSeparation = markerFillProperties.getXSeparation(); // TODO apply CartographicSupport
221
                        double ySeparation = markerFillProperties.getYSeparation(); // TODO apply CartographicSupport
222
                        double xOffset = markerFillProperties.getXOffset();
223
                        double yOffset = markerFillProperties.getYOffset();
224

    
225

    
226
                        markerSymbol.drawInsideRectangle(gAux, new AffineTransform(), rProv);
227
                        rProv.setRect(0, 0,
228
                                        rProv.getWidth() + xSeparation,
229
                                        rProv.getHeight() + ySeparation);
230

    
231
                        BufferedImage bi = new BufferedImage(rProv.width, rProv.height, BufferedImage.TYPE_INT_ARGB);
232
                        gAux = bi.createGraphics();
233
                        gAux.drawImage(sample, null, (int) (xSeparation*0.5), (int) (ySeparation*0.5));
234

    
235
                        resulPatternFill = new TexturePaint(bi,rProv);
236

    
237
                        g.setColor(null);
238
                        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
239
                                RenderingHints.VALUE_ANTIALIAS_ON);
240

    
241
                        g.translate(xOffset, yOffset);
242
                        g.setPaint(resulPatternFill);
243
                        g.fill(shp);
244
                        g.translate(-xOffset, -yOffset);
245
                        g.setClip(null);
246

    
247
                        }
248
                        break;
249
                case RANDOM_FILL:
250
                        {
251

    
252
                        double s = markerSymbol.getSize();
253
                        Rectangle r = shp.getBounds();
254
                        int drawCount = (int) (Math.min(r.getWidth(), r.getHeight())/s);
255
                        Random random = new Random();
256

    
257
                        int minx = r.x;
258
                        int miny = r.y;
259
                        int width = r.width;
260
                        int height = r.height;
261

    
262
                        r = new Rectangle();
263
                        g.setClip(shp);
264

    
265
                        for (int i = 0; (cancel==null || !cancel.isCanceled()) && i < drawCount; i++) {
266
                                int x = (int) Math.abs(random.nextDouble() * width);
267
                                int y = (int) Math.abs(random.nextDouble() * height);
268
                                x = x + minx;
269
                                y = y + miny;
270
                                markerSymbol.draw(g, new AffineTransform(), new FPoint2D(x, y), cancel);
271

    
272
                        }
273
                        g.setClip(null);
274
                        }
275
                        break;
276
                }
277
        }
278

    
279
        public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform, Shape shp) {
280
                // TODO Auto-generated method stub
281
                throw new Error("Not yet implemented!");
282
        }
283

    
284
        public XMLEntity getXMLEntity() {
285
                XMLEntity xml = new XMLEntity();
286
                xml.putProperty("className", getClassName());
287
                xml.putProperty("isShapeVisible", isShapeVisible());
288
                // color (necessite)
289
                if (getFillColor() !=null)
290
                        xml.putProperty("color", StringUtilities.color2String(getFillColor()));
291
                xml.putProperty("desc", getDescription());
292

    
293
                xml.addChild(markerSymbol.getXMLEntity());
294
                xml.addChild(markerFillProperties.getXMLEntity());
295
                return xml;
296
        }
297

    
298
        public int getSymbolType() {
299
                return FShape.POLYGON;
300
        }
301

    
302
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r) {
303
                markerFillProperties.setSampleSymbol(markerSymbol);
304
                switch (markerFillProperties.getFillStyle()) {
305
                case SINGLE_CENTERED_SYMBOL:
306
                        FPoint2D p = new FPoint2D(r.getCenterX(), r.getCenterY());
307
                        markerSymbol.draw(g, null, p, null);
308
                        break;
309
                case GRID_FILL:
310
                {
311
                        g.setClip(r);
312
                        int size = (int) markerSymbol.getSize();
313
                        if (size <= 0 ) size = 1;
314
                        Rectangle rProv = new Rectangle();
315
                        rProv.setFrame(0, 0, size, size);
316
                        Paint resulPatternFill = null;
317

    
318
                        BufferedImage sample = null;
319
                        sample = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
320
                        Graphics2D gAux = sample.createGraphics();
321

    
322
                        double xSeparation = markerFillProperties.getXSeparation(); // TODO apply CartographicSupport
323
                        double ySeparation = markerFillProperties.getYSeparation(); // TODO apply CartographicSupport
324
                        double xOffset = markerFillProperties.getXOffset();
325
                        double yOffset = markerFillProperties.getYOffset();
326

    
327
                        markerSymbol.drawInsideRectangle(gAux, new AffineTransform(), rProv);
328

    
329
                        rProv.setRect(0, 0,
330
                                        rProv.getWidth() + xSeparation,
331
                                        rProv.getHeight() + ySeparation);
332

    
333
                        BufferedImage bi = new BufferedImage(rProv.width, rProv.height, BufferedImage.TYPE_INT_ARGB);
334
                        gAux = bi.createGraphics();
335
                        gAux.drawImage(sample, null, (int) (xSeparation*0.5), (int) (ySeparation*0.5));
336

    
337

    
338
                        resulPatternFill = new TexturePaint(bi,rProv);
339
                        g.setColor(null);
340
                        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
341
                                        RenderingHints.VALUE_ANTIALIAS_ON);
342

    
343
                        g.translate(xOffset, yOffset);
344
                        g.setPaint(resulPatternFill);
345
                        g.fill(r);
346
                        g.translate(-xOffset, -yOffset);
347
                        g.setClip(null);
348
                }
349
                        break;
350
                case RANDOM_FILL:
351
                        g.setClip(r);
352
                        int x = r.x;
353
                        int y = r.y;
354
                        int width = r.width;
355
                        int height= r.height;
356
                        g.setBackground(null);
357

    
358
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.2), (y+height*0.8)), null);
359
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.634), (y+height*0.3)), null);
360
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.26), (y+height*0.35)), null);
361
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.45), (y+height*0.98)), null);
362
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.9), (y+height*0.54)), null);
363
                        markerSymbol.draw(g, null, new FPoint2D((x+width*1.1), (y+height*0.7)), null);
364
                        g.setClip(null);
365
                        break;
366
                }
367
        }
368

    
369

    
370
        public String getClassName() {
371
                return getClass().getName();
372
        }
373

    
374
        public void setXMLEntity(XMLEntity xml) {
375
                setDescription(xml.getStringProperty("desc"));
376
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
377

    
378
                markerSymbol = (AbstractMarkerSymbol) SymbologyFactory.
379
                                                        createSymbolFromXML(xml.getChild(0), null);
380
                markerFillProperties = (SimpleMarkerFillPropertiesStyle) SymbologyFactory.
381
                                                        createStyleFromXML(xml.getChild(1), null);
382
        }
383

    
384
        public void setMarker(IMarkerSymbol marker) {
385
                this.markerSymbol = marker;
386
        }
387

    
388
        public IMarkerSymbol getMarker() {
389
                return markerSymbol;
390
        }
391

    
392
        public Color getFillColor(){
393
                return markerSymbol.getColor();
394
        }
395

    
396
        public void setFillColor (Color color) {
397
                markerSymbol.setColor(color);
398
        }
399
        
400
        public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties) throws ReadDriverException {
401
                // TODO Implement it
402
                throw new Error("Not yet implemented!");
403

    
404
        }
405
        /**
406
         * Sets the markerfillproperties to be used by the class
407
         *
408
         * @param markerFillStyle,IMarkerFillPropertiesStyle
409
         */
410
        public void setMarkerFillProperties(IMarkerFillPropertiesStyle markerFillStyle) {
411
                this.markerFillProperties = markerFillStyle;
412
        }
413

    
414
        /**
415
         * Returns the markerfillproperties that are used by the class
416
         *
417
         * @return markerFillProperties,IMarkerFillPropertiesStyle
418
         */
419
        public IMarkerFillPropertiesStyle getMarkerFillProperties() {
420
                return markerFillProperties;
421
        }
422

    
423
}