Statistics
| Revision:

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

History | View | Annotate | Download (11.3 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 12728 2007-07-23 06:58:42Z jaume $
45
* $Log$
46
* Revision 1.11  2007-07-23 06:52:25  jaume
47
* default selection color refactored, moved to MapContext
48
*
49
* Revision 1.10  2007/05/28 15:36:42  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.9  2007/05/08 08:47:40  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.8  2007/03/28 16:48:14  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.7  2007/03/26 14:25:17  jaume
59
* implements IPrintable
60
*
61
* Revision 1.6  2007/03/21 17:36:22  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.5  2007/03/13 16:58:36  jaume
65
* Added QuantityByCategory (Multivariable legend) and some bugfixes in symbols
66
*
67
* Revision 1.4  2007/03/09 11:20:57  jaume
68
* Advanced symbology (start committing)
69
*
70
* Revision 1.2.2.4  2007/02/16 10:54:12  jaume
71
* multilayer splitted to multilayerline, multilayermarker,and  multilayerfill
72
*
73
* Revision 1.2.2.3  2007/02/15 16:23:44  jaume
74
* *** empty log message ***
75
*
76
* Revision 1.2.2.2  2007/02/12 15:15:20  jaume
77
* refactored interval legend and added graduated symbol legend
78
*
79
* Revision 1.2.2.1  2007/02/09 07:47:05  jaume
80
* Isymbol moved
81
*
82
* Revision 1.2  2007/01/10 16:39:41  jaume
83
* ISymbol now belongs to com.iver.cit.gvsig.fmap.core.symbols package
84
*
85
* Revision 1.1  2007/01/10 16:31:36  jaume
86
* *** empty log message ***
87
*
88
* Revision 1.10  2006/11/09 18:39:05  jaume
89
* *** empty log message ***
90
*
91
* Revision 1.9  2006/11/09 10:22:50  jaume
92
* *** empty log message ***
93
*
94
* Revision 1.8  2006/11/08 13:05:51  jaume
95
* *** empty log message ***
96
*
97
* Revision 1.7  2006/11/08 10:56:47  jaume
98
* *** empty log message ***
99
*
100
* Revision 1.6  2006/11/07 08:52:30  jaume
101
* *** empty log message ***
102
*
103
* Revision 1.5  2006/11/06 17:08:45  jaume
104
* *** empty log message ***
105
*
106
* Revision 1.4  2006/11/06 16:06:52  jaume
107
* *** empty log message ***
108
*
109
* Revision 1.3  2006/11/06 07:33:54  jaume
110
* javadoc, source style
111
*
112
* Revision 1.2  2006/10/31 16:16:34  jaume
113
* *** empty log message ***
114
*
115
* Revision 1.1  2006/10/30 19:30:35  jaume
116
* *** empty log message ***
117
*
118
*
119
*/
120
package com.iver.cit.gvsig.fmap.core.symbols;
121

    
122
import java.awt.Graphics2D;
123
import java.awt.Paint;
124
import java.awt.Rectangle;
125
import java.awt.RenderingHints;
126
import java.awt.Shape;
127
import java.awt.TexturePaint;
128
import java.awt.geom.AffineTransform;
129
import java.awt.geom.Point2D;
130
import java.awt.image.BufferedImage;
131
import java.util.ArrayList;
132
import java.util.Random;
133

    
134
import javax.print.attribute.PrintRequestAttributeSet;
135

    
136
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
137
import com.iver.cit.gvsig.fmap.MapContext;
138
import com.iver.cit.gvsig.fmap.core.FPoint2D;
139
import com.iver.cit.gvsig.fmap.core.FShape;
140
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
141
import com.iver.cit.gvsig.fmap.core.styles.IMarkerFillProperties;
142
import com.iver.cit.gvsig.fmap.core.styles.MarkerFillProperties;
143
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
144
import com.iver.utiles.StringUtilities;
145
import com.iver.utiles.XMLEntity;
146
import com.vividsolutions.jts.geom.Geometry;
147

    
148
/**
149
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
150
 */
151
public class MarkerFillSymbol extends AbstractFillSymbol {
152
        public static final int RANDOM_FILL = 3;
153
        public static final int GRID_FILL = 1;
154
        public static final int SINGLE_CENTERED_SYMBOL = 2;
155
        private MarkerFillSymbol selectionSymbol;
156
        private IMarkerFillProperties markerFillProperties = new MarkerFillProperties();
157
        private IMarkerSymbol markerSymbol = new SimpleMarkerSymbol();
158

    
159

    
160

    
161

    
162
        public ISymbol getSymbolForSelection() {
163
                if (selectionSymbol == null) {
164
                        selectionSymbol = (MarkerFillSymbol) SymbologyFactory.createSymbolFromXML(getXMLEntity(), null);
165
                        selectionSymbol.setFillColor(MapContext.getSelectionColor());
166
                }
167

    
168
                return selectionSymbol;
169
        }
170

    
171
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
172
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
173
                                RenderingHints.VALUE_ANTIALIAS_ON);
174

    
175
                switch (markerFillProperties.getFillStyle()) {
176
                case SINGLE_CENTERED_SYMBOL:
177
                        // case a single marker is used into a polygon shapetype
178
                        Geometry geom = FConverter.java2d_to_jts(shp);
179
                        com.vividsolutions.jts.geom.Point centroid = geom.getCentroid();
180
                        FPoint2D p = new FPoint2D(new Point2D.Double(
181
                                        centroid.getX()+markerFillProperties.getXOffset(),
182
                                        centroid.getY()+markerFillProperties.getYOffset()));
183
                        markerSymbol.draw(g, affineTransform, p);
184
                        break;
185
                case GRID_FILL:
186
                        // case a grid fill is used
187
                        {
188

    
189
                        int size = (int) markerSymbol.getSize();
190
                        Rectangle rProv = new Rectangle();
191
                        rProv.setFrame(0, 0, size, size);
192
                        Paint resulPatternFill = null;
193

    
194
                        BufferedImage sample = null;
195
                        sample = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
196
                        Graphics2D gAux = sample.createGraphics();
197

    
198
                        double xSeparation = markerFillProperties.getXSeparation(); // TODO apply CartographicSupport
199
                        double ySeparation = markerFillProperties.getYSeparation(); // TODO apply CartographicSupport
200
                        markerSymbol.drawInsideRectangle(gAux, new AffineTransform(), rProv);
201
                        rProv.setRect(0, 0,
202
                                        rProv.getWidth() + xSeparation,
203
                                        rProv.getHeight() + ySeparation);
204

    
205
                        BufferedImage bi = new BufferedImage(rProv.width, rProv.height, BufferedImage.TYPE_INT_ARGB);
206
                        gAux = bi.createGraphics();
207
                        gAux.drawImage(sample, null, (int) (xSeparation*0.5), (int) (ySeparation*0.5));
208

    
209
                        resulPatternFill = new TexturePaint(bi,rProv);
210
                        g.setColor(null);
211
                        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
212
                                RenderingHints.VALUE_ANTIALIAS_ON);
213
                        g.setPaint(resulPatternFill);
214
                        g.fill(shp);
215

    
216
                        }
217
                        break;
218
                case RANDOM_FILL:
219
                        {
220
                        double s = markerSymbol.getSize();
221
                        Rectangle r = shp.getBounds();
222
                        int drawCount = (int) (Math.min(r.getWidth(), r.getHeight())/s);
223
                        Random random = new Random();
224

    
225
                        int minx = r.x;
226
                        int miny = r.y;
227
                        int width = r.width;
228
                        int height = r.height;
229

    
230
                        r = new Rectangle();
231
                        g.setClip(shp);
232

    
233
                        for (int i = 0; i < drawCount; i++) {
234
                                int x = (int) Math.abs(random.nextDouble() * width);
235
                                int y = (int) Math.abs(random.nextDouble() * height);
236
                                x = x + minx;
237
                                y = y + miny;
238
                                markerSymbol.draw(g, new AffineTransform(), new FPoint2D(x, y));
239

    
240
                        }
241
                        g.setClip(null);
242
                        }
243
                        break;
244
                }
245
        }
246

    
247
        public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform, Shape shp) {
248
                // TODO Auto-generated method stub
249
                throw new Error("Not yet implemented!");
250
        }
251

    
252
        public XMLEntity getXMLEntity() {
253
                XMLEntity xml = new XMLEntity();
254
                xml.putProperty("className", getClassName());
255
                xml.putProperty("isShapeVisible", isShapeVisible());
256
                // color (necessite)
257
                if (getFillColor() !=null)
258
                        xml.putProperty("color", StringUtilities.color2String(getFillColor()));
259
                xml.putProperty("desc", getDescription());
260
//                xml.putProperty("fillStyle", fillStyle);
261

    
262
                xml.addChild(markerSymbol.getXMLEntity());
263
                xml.addChild(markerFillProperties.getXMLEntity());
264
                return xml;
265
        }
266

    
267
        public int getSymbolType() {
268
                return FShape.POLYGON;
269
        }
270

    
271
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r) {
272
                markerFillProperties.setSampleSymbol(markerSymbol);
273
                switch (markerFillProperties.getFillStyle()) {
274
                case SINGLE_CENTERED_SYMBOL:
275
                        FPoint2D p = new FPoint2D(r.getCenterX(), r.getCenterY());
276
                        markerSymbol.draw(g, null, p);
277
                        break;
278
                case GRID_FILL:
279
                {
280
                        int size = (int) markerSymbol.getSize();
281
                        if (size == 0 ) size = 1;
282
                        Rectangle rProv = new Rectangle();
283
                        rProv.setFrame(0, 0, size, size);
284
                        Paint resulPatternFill = null;
285

    
286
                        BufferedImage sample = null;
287
                        sample = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
288
                        Graphics2D gAux = sample.createGraphics();
289

    
290
                        double xSeparation = markerFillProperties.getXSeparation(); // TODO apply CartographicSupport
291
                        double ySeparation = markerFillProperties.getYSeparation(); // TODO apply CartographicSupport
292
                        markerSymbol.drawInsideRectangle(gAux, new AffineTransform(), rProv);
293

    
294
                        rProv.setRect(0, 0,
295
                                        rProv.getWidth() + xSeparation,
296
                                        rProv.getHeight() + ySeparation);
297

    
298
                        BufferedImage bi = new BufferedImage(rProv.width, rProv.height, BufferedImage.TYPE_INT_ARGB);
299
                        gAux = bi.createGraphics();
300
                        gAux.drawImage(sample, null, (int) (xSeparation*0.5), (int) (ySeparation*0.5));
301

    
302

    
303
                        resulPatternFill = new TexturePaint(bi,rProv);
304
                        g.setColor(null);
305
                        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
306
                                        RenderingHints.VALUE_ANTIALIAS_ON);
307
                        g.setPaint(resulPatternFill);
308
                        g.fill(r);
309
                }
310
                        break;
311
                case RANDOM_FILL:
312
                        int x = r.x;
313
                        int y = r.y;
314
                        int width = r.width;
315
                        int height= r.height;
316
                        g.setBackground(null);
317

    
318
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.2), (y+height*0.8)));
319
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.634), (y+height*0.3)));
320
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.26), (y+height*0.35)));
321
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.45), (y+height*0.98)));
322
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.9), (y+height*0.54)));
323
                        markerSymbol.draw(g, null, new FPoint2D((x+width*1.1), (y+height*0.7)));
324
                        break;
325
                }
326
        }
327

    
328
        private Point2D[] ramdonizePoints(Rectangle r) {
329
                ArrayList points = new ArrayList();
330
                        throw new Error("Not yet implemented!");
331
//                return (Point2D[]) points.toArray(new Point2D[0]);
332
        }
333

    
334
//        public int getFillStyle() {
335
//                return fillStyle;
336
//        }
337
//
338
//        public void setFillStyle(int fillStyle) {
339
//                this.fillStyle = fillStyle;
340
//        }
341
        public String getClassName() {
342
                return getClass().getName();
343
        }
344

    
345
        public void setXMLEntity(XMLEntity xml) {
346
                setDescription(xml.getStringProperty("desc"));
347
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
348
//                fillStyle = xml.getIntProperty("fillStyle");
349

    
350
                markerSymbol = (AbstractMarkerSymbol) SymbologyFactory.
351
                                                        createSymbolFromXML(xml.getChild(0), null);
352
                markerFillProperties = (MarkerFillProperties) SymbologyFactory.
353
                                                        createStyleFromXML(xml.getChild(1), null);
354
        }
355

    
356
        public void setMarker(IMarkerSymbol marker) {
357
                this.markerSymbol = marker;
358
        }
359

    
360
        public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties) throws ReadDriverException {
361
                // TODO Implement it
362
                throw new Error("Not yet implemented!");
363

    
364
        }
365

    
366
        public void setFillProperties(IMarkerFillProperties markerFillStyle) {
367
                this.markerFillProperties = markerFillStyle;
368
        }
369

    
370
        public IMarkerFillProperties getFillProperties() {
371
                return markerFillProperties;
372
        }
373

    
374
}