Statistics
| Revision:

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

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

    
113
import java.awt.Graphics2D;
114
import java.awt.Paint;
115
import java.awt.Rectangle;
116
import java.awt.RenderingHints;
117
import java.awt.Shape;
118
import java.awt.TexturePaint;
119
import java.awt.geom.AffineTransform;
120
import java.awt.geom.Point2D;
121
import java.awt.geom.Rectangle2D;
122
import java.awt.image.BufferedImage;
123
import java.util.ArrayList;
124
import java.util.Random;
125

    
126
import javax.print.attribute.PrintRequestAttributeSet;
127

    
128
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
129
import com.iver.cit.gvsig.fmap.core.FPoint2D;
130
import com.iver.cit.gvsig.fmap.core.FShape;
131
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
132
import com.iver.cit.gvsig.fmap.core.styles.MarkerFillStyle;
133
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
134
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
135
import com.iver.utiles.StringUtilities;
136
import com.iver.utiles.XMLEntity;
137
import com.vividsolutions.jts.geom.Geometry;
138

    
139
/**
140
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
141
 */
142
public class MarkerFillSymbol extends AbstractFillSymbol {
143
        private double rotation;
144
        private IMarkerSymbol markerSymbol = new SimpleMarkerSymbol();
145
        private double xOffset = 0;
146
        private double yOffset = 0;
147
        private double xSeparation = 20;
148
        private double ySeparation = 20;
149
        private MarkerFillStyle markerFillStyle = new MarkerFillStyle();
150
        private MarkerFillSymbol selectionSymbol;
151

    
152
        public int getFillStyle() {
153
                return markerFillStyle.getFillStyle();
154
        }
155

    
156
        public void setFillStyle(int fillStyle) {
157
                markerFillStyle.setFillStyle(fillStyle);
158
        }
159

    
160
        public IMarkerSymbol getMarker() {
161
                return markerSymbol;
162
        }
163

    
164
        public double getRotation() {
165
                return rotation;
166
        }
167

    
168
        public void setRotation(double rotation) {
169
                this.rotation = rotation;
170
        }
171

    
172
        public double getXOffset() {
173
                return xOffset;
174
        }
175

    
176
        public void setXOffset(double offset) {
177
                xOffset = offset;
178
        }
179

    
180
        public double getXSeparation() {
181
                return xSeparation;
182
        }
183

    
184
        public void setXSeparation(double separation) {
185
                xSeparation = separation;
186
        }
187

    
188
        public double getYOffset() {
189
                return yOffset;
190
        }
191

    
192
        public void setYOffset(double offset) {
193
                yOffset = offset;
194
        }
195

    
196
        public double getYSeparation() {
197
                return ySeparation;
198
        }
199

    
200
        public void setYSeparation(double separation) {
201
                ySeparation = separation;
202
        }
203

    
204
        public ISymbol getSymbolForSelection() {
205
                if (selectionSymbol == null) {
206
                        selectionSymbol = (MarkerFillSymbol) SymbologyFactory.createSymbolFromXML(getXMLEntity(), null);
207
                        selectionSymbol.setFillColor(FSymbol.getSelectionColor());
208
                }
209

    
210
                return selectionSymbol;
211
        }
212

    
213
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
214
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
215
                                RenderingHints.VALUE_ANTIALIAS_ON);
216

    
217
                switch (markerFillStyle.getFillStyle()) {
218
                case MarkerFillStyle.SINGLE_CENTERED_SYMBOL:
219
                        // case a single marker is used into a polygon shapetype
220
                        Geometry geom = FConverter.java2d_to_jts(shp);
221
                        com.vividsolutions.jts.geom.Point centroid = geom.getCentroid();
222
                        FPoint2D p = new FPoint2D(new Point2D.Double(centroid.getX()+getXOffset(), centroid.getY()+getYOffset()));
223
                        markerSymbol.draw(g, affineTransform, p);
224
                        break;
225
                case MarkerFillStyle.GRID_FILL:
226
                        // case a grid fill is used
227
                        {
228

    
229
                        int s = (int) markerSymbol.getSize();
230
                        Rectangle rProv = new Rectangle();
231
                        rProv.setFrame(0, 0, s, s);
232
                        Paint resulPatternFill = null;
233
                        BufferedImage bi = null;
234
                        bi= new BufferedImage(s, s, BufferedImage.TYPE_INT_ARGB);
235
                        Graphics2D gAux = bi.createGraphics();
236
                        markerSymbol.drawInsideRectangle(gAux, new AffineTransform(), rProv);
237
                        resulPatternFill = new TexturePaint(bi,rProv);
238
                        g.setColor(null);
239
                        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
240
                                RenderingHints.VALUE_ANTIALIAS_ON);
241
                        g.setPaint(resulPatternFill);
242
                        g.fill(shp);
243

    
244
                        }
245
                        break;
246
                case MarkerFillStyle.RANDOM_FILL:
247
                        {
248
                        double s = markerSymbol.getSize();
249
                        Rectangle r = shp.getBounds();
250
                        int drawCount = (int) (Math.min(r.getWidth(), r.getHeight())/s);
251
                        Random random = new Random();
252

    
253
                        int minx = r.x;
254
                        int miny = r.y;
255
                        int width = r.width;
256
                        int height = r.height;
257

    
258
                        r = new Rectangle();
259
                        g.setClip(shp);
260

    
261
                        for (int i = 0; i < drawCount; i++) {
262
                                int x = (int) Math.abs(random.nextDouble() * width);
263
                                int y = (int) Math.abs(random.nextDouble() * height);
264
                                x = x + minx;
265
                                y = y + miny;
266
                                markerSymbol.draw(g, new AffineTransform(), new FPoint2D(x, y));
267

    
268
                        }
269
                        g.setClip(null);
270
                        }
271
                        break;
272
                }
273
        }
274

    
275
        public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform, Shape shp) {
276
                // TODO Auto-generated method stub
277
                throw new Error("Not yet implemented!");
278
        }
279

    
280
        public XMLEntity getXMLEntity() {
281
                XMLEntity xml = new XMLEntity();
282
                xml.putProperty("className", getClassName());
283
                xml.putProperty("isShapeVisible", isShapeVisible());
284
                // color (necessite)
285
                if (getFillColor() !=null)
286
                        xml.putProperty("color", StringUtilities.color2String(getFillColor()));
287
                xml.putProperty("desc", getDescription());
288
                xml.putProperty("rotation", rotation);
289
                xml.putProperty("xOffset", xOffset);
290
                xml.putProperty("yOffset", yOffset);
291
                xml.putProperty("xSeparation", xSeparation);
292
                xml.putProperty("ySeparation", ySeparation);
293

    
294
                xml.addChild(markerSymbol.getXMLEntity());
295
                xml.addChild(markerFillStyle.getXMLEntity());
296
                return xml;
297
        }
298

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

    
303
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r) {
304
                markerFillStyle.setSampleSymbol(markerSymbol);
305
                markerFillStyle.drawInsideRectangle(g, r);
306
        }
307

    
308
        private Point2D[] ramdonizePoints(Rectangle r) {
309
                ArrayList points = new ArrayList();
310
                        throw new Error("Not yet implemented!");
311
//                return (Point2D[]) points.toArray(new Point2D[0]);
312
        }
313

    
314
        public String getClassName() {
315
                return getClass().getName();
316
        }
317

    
318
        public void setXMLEntity(XMLEntity xml) {
319
                setDescription(xml.getStringProperty("desc"));
320
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
321
                rotation = xml.getDoubleProperty("rotation");
322
                xOffset = xml.getDoubleProperty("xOffset");
323
                yOffset = xml.getDoubleProperty("yOffset");
324
                xSeparation = xml.getDoubleProperty("xSeparation");
325
                ySeparation = xml.getDoubleProperty("ySeparation");
326
                markerSymbol = (AbstractMarkerSymbol) SymbologyFactory.
327
                                                        createSymbolFromXML(xml.getChild(0), null);
328
                markerFillStyle = (MarkerFillStyle) SymbologyFactory.
329
                                                        createStyleFromXML(xml.getChild(1), null);
330
        }
331

    
332
        public void setMarker(IMarkerSymbol marker) {
333
                this.markerSymbol = marker;
334
        }
335

    
336
        public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties) throws ReadDriverException {
337
                // TODO Implement it
338
                throw new Error("Not yet implemented!");
339

    
340
        }
341

    
342
}