Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / symbols / MarkerFillSymbol.java @ 13881

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

    
140
import java.awt.Graphics2D;
141
import java.awt.Paint;
142
import java.awt.Rectangle;
143
import java.awt.RenderingHints;
144
import java.awt.Shape;
145
import java.awt.TexturePaint;
146
import java.awt.geom.AffineTransform;
147
import java.awt.geom.Point2D;
148
import java.awt.image.BufferedImage;
149
import java.util.Random;
150

    
151
import javax.print.attribute.PrintRequestAttributeSet;
152

    
153
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
154
import com.iver.cit.gvsig.fmap.MapContext;
155
import com.iver.cit.gvsig.fmap.core.FPoint2D;
156
import com.iver.cit.gvsig.fmap.core.FShape;
157
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
158
import com.iver.cit.gvsig.fmap.core.styles.IMarkerFillPropertiesStyle;
159
import com.iver.cit.gvsig.fmap.core.styles.SimpleMarkerFillPropertiesStyle;
160
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
161
import com.iver.utiles.StringUtilities;
162
import com.iver.utiles.XMLEntity;
163
import com.vividsolutions.jts.geom.Geometry;
164

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

    
179

    
180

    
181

    
182
        public ISymbol getSymbolForSelection() {
183
                if (selectionSymbol == null) {
184
                        selectionSymbol = (MarkerFillSymbol) SymbologyFactory.createSymbolFromXML(getXMLEntity(), null);
185
                        selectionSymbol.setFillColor(MapContext.getSelectionColor());
186
                }
187

    
188
                return selectionSymbol;
189
        }
190

    
191
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
192
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
193
                                RenderingHints.VALUE_ANTIALIAS_ON);
194

    
195
                switch (markerFillProperties.getFillStyle()) {
196
                case SINGLE_CENTERED_SYMBOL:
197
                        // case a single marker is used into a polygon shapetype
198
                        Geometry geom = FConverter.java2d_to_jts(shp);
199
                        com.vividsolutions.jts.geom.Point centroid = geom.getCentroid();
200
                        FPoint2D p = new FPoint2D(new Point2D.Double(
201
                                        centroid.getX()+markerFillProperties.getXOffset(),
202
                                        centroid.getY()+markerFillProperties.getYOffset()));
203
                        markerSymbol.draw(g, affineTransform, p);
204
                        break;
205
                case GRID_FILL:
206
                        // case a grid fill is used
207
                        {
208

    
209
                        int size = (int) markerSymbol.getSize();
210
                        Rectangle rProv = new Rectangle();
211
                        rProv.setFrame(0, 0, size, size);
212
                        Paint resulPatternFill = null;
213

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

    
218
                        double xSeparation = markerFillProperties.getXSeparation(); // TODO apply CartographicSupport
219
                        double ySeparation = markerFillProperties.getYSeparation(); // TODO apply CartographicSupport
220
                        markerSymbol.drawInsideRectangle(gAux, new AffineTransform(), rProv);
221
                        rProv.setRect(0, 0,
222
                                        rProv.getWidth() + xSeparation,
223
                                        rProv.getHeight() + ySeparation);
224

    
225
                        BufferedImage bi = new BufferedImage(rProv.width, rProv.height, BufferedImage.TYPE_INT_ARGB);
226
                        gAux = bi.createGraphics();
227
                        gAux.drawImage(sample, null, (int) (xSeparation*0.5), (int) (ySeparation*0.5));
228

    
229
                        resulPatternFill = new TexturePaint(bi,rProv);
230
                        g.setColor(null);
231
                        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
232
                                RenderingHints.VALUE_ANTIALIAS_ON);
233
                        g.setPaint(resulPatternFill);
234
                        g.fill(shp);
235

    
236
                        }
237
                        break;
238
                case RANDOM_FILL:
239
                        {
240
                        double s = markerSymbol.getSize();
241
                        Rectangle r = shp.getBounds();
242
                        int drawCount = (int) (Math.min(r.getWidth(), r.getHeight())/s);
243
                        Random random = new Random();
244

    
245
                        int minx = r.x;
246
                        int miny = r.y;
247
                        int width = r.width;
248
                        int height = r.height;
249

    
250
                        r = new Rectangle();
251
                        g.setClip(shp);
252

    
253
                        for (int i = 0; i < drawCount; i++) {
254
                                int x = (int) Math.abs(random.nextDouble() * width);
255
                                int y = (int) Math.abs(random.nextDouble() * height);
256
                                x = x + minx;
257
                                y = y + miny;
258
                                markerSymbol.draw(g, new AffineTransform(), new FPoint2D(x, y));
259

    
260
                        }
261
                        g.setClip(null);
262
                        }
263
                        break;
264
                }
265
        }
266

    
267
        public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform, Shape shp) {
268
                // TODO Auto-generated method stub
269
                throw new Error("Not yet implemented!");
270
        }
271

    
272
        public XMLEntity getXMLEntity() {
273
                XMLEntity xml = new XMLEntity();
274
                xml.putProperty("className", getClassName());
275
                xml.putProperty("isShapeVisible", isShapeVisible());
276
                // color (necessite)
277
                if (getFillColor() !=null)
278
                        xml.putProperty("color", StringUtilities.color2String(getFillColor()));
279
                xml.putProperty("desc", getDescription());
280

    
281
                xml.addChild(markerSymbol.getXMLEntity());
282
                xml.addChild(markerFillProperties.getXMLEntity());
283
                return xml;
284
        }
285

    
286
        public int getSymbolType() {
287
                return FShape.POLYGON;
288
        }
289

    
290
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r) {
291
                markerFillProperties.setSampleSymbol(markerSymbol);
292
                switch (markerFillProperties.getFillStyle()) {
293
                case SINGLE_CENTERED_SYMBOL:
294
                        FPoint2D p = new FPoint2D(r.getCenterX(), r.getCenterY());
295
                        markerSymbol.draw(g, null, p);
296
                        break;
297
                case GRID_FILL:
298
                {
299
                        int size = (int) markerSymbol.getSize();
300
                        if (size <= 0 ) size = 1;
301
                        Rectangle rProv = new Rectangle();
302
                        rProv.setFrame(0, 0, size, size);
303
                        Paint resulPatternFill = null;
304

    
305
                        BufferedImage sample = null;
306
                        sample = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
307
                        Graphics2D gAux = sample.createGraphics();
308

    
309
                        double xSeparation = markerFillProperties.getXSeparation(); // TODO apply CartographicSupport
310
                        double ySeparation = markerFillProperties.getYSeparation(); // TODO apply CartographicSupport
311
                        markerSymbol.drawInsideRectangle(gAux, new AffineTransform(), rProv);
312

    
313
                        rProv.setRect(0, 0,
314
                                        rProv.getWidth() + xSeparation,
315
                                        rProv.getHeight() + ySeparation);
316

    
317
                        BufferedImage bi = new BufferedImage(rProv.width, rProv.height, BufferedImage.TYPE_INT_ARGB);
318
                        gAux = bi.createGraphics();
319
                        gAux.drawImage(sample, null, (int) (xSeparation*0.5), (int) (ySeparation*0.5));
320

    
321

    
322
                        resulPatternFill = new TexturePaint(bi,rProv);
323
                        g.setColor(null);
324
                        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
325
                                        RenderingHints.VALUE_ANTIALIAS_ON);
326
                        g.setPaint(resulPatternFill);
327
                        g.fill(r);
328
                }
329
                        break;
330
                case RANDOM_FILL:
331
                        int x = r.x;
332
                        int y = r.y;
333
                        int width = r.width;
334
                        int height= r.height;
335
                        g.setBackground(null);
336

    
337
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.2), (y+height*0.8)));
338
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.634), (y+height*0.3)));
339
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.26), (y+height*0.35)));
340
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.45), (y+height*0.98)));
341
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.9), (y+height*0.54)));
342
                        markerSymbol.draw(g, null, new FPoint2D((x+width*1.1), (y+height*0.7)));
343
                        break;
344
                }
345
        }
346

    
347

    
348
        public String getClassName() {
349
                return getClass().getName();
350
        }
351

    
352
        public void setXMLEntity(XMLEntity xml) {
353
                setDescription(xml.getStringProperty("desc"));
354
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
355

    
356
                markerSymbol = (AbstractMarkerSymbol) SymbologyFactory.
357
                                                        createSymbolFromXML(xml.getChild(0), null);
358
                markerFillProperties = (SimpleMarkerFillPropertiesStyle) SymbologyFactory.
359
                                                        createStyleFromXML(xml.getChild(1), null);
360
        }
361

    
362
        public void setMarker(IMarkerSymbol marker) {
363
                this.markerSymbol = marker;
364
        }
365
        
366
        public IMarkerSymbol getMarker() {
367
                return markerSymbol;
368
        }
369

    
370
        public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties) throws ReadDriverException {
371
                // TODO Implement it
372
                throw new Error("Not yet implemented!");
373

    
374
        }
375
        /**
376
         * Sets the markerfillproperties to be used by the class
377
         *
378
         * @param markerFillStyle,IMarkerFillPropertiesStyle
379
         */
380
        public void setMarkerFillProperties(IMarkerFillPropertiesStyle markerFillStyle) {
381
                this.markerFillProperties = markerFillStyle;
382
        }
383

    
384
        /**
385
         * Returns the markerfillproperties that are used by the class
386
         *
387
         * @return markerFillProperties,IMarkerFillPropertiesStyle
388
         */
389
        public IMarkerFillPropertiesStyle getMarkerFillProperties() {
390
                return markerFillProperties;
391
        }
392

    
393
}