Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / symbols / MarkerFillSymbol.java @ 12148

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

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

    
131
import javax.print.attribute.PrintRequestAttributeSet;
132

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

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

    
156

    
157

    
158

    
159
        public ISymbol getSymbolForSelection() {
160
                if (selectionSymbol == null) {
161
                        selectionSymbol = (MarkerFillSymbol) SymbologyFactory.createSymbolFromXML(getXMLEntity(), null);
162
                        selectionSymbol.setFillColor(FSymbol.getSelectionColor());
163
                }
164

    
165
                return selectionSymbol;
166
        }
167

    
168
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
169
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
170
                                RenderingHints.VALUE_ANTIALIAS_ON);
171

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

    
186
                        int size = (int) markerSymbol.getSize();
187
                        Rectangle rProv = new Rectangle();
188
                        rProv.setFrame(0, 0, size, size);
189
                        Paint resulPatternFill = null;
190

    
191
                        BufferedImage sample = null;
192
                        sample = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
193
                        Graphics2D gAux = sample.createGraphics();
194

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

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

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

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

    
222
                        int minx = r.x;
223
                        int miny = r.y;
224
                        int width = r.width;
225
                        int height = r.height;
226

    
227
                        r = new Rectangle();
228
                        g.setClip(shp);
229

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

    
237
                        }
238
                        g.setClip(null);
239
                        }
240
                        break;
241
                }
242
        }
243

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

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

    
259
                xml.addChild(markerSymbol.getXMLEntity());
260
                xml.addChild(markerFillProperties.getXMLEntity());
261
                return xml;
262
        }
263

    
264
        public int getSymbolType() {
265
                return FShape.POLYGON;
266
        }
267

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

    
283
                        BufferedImage sample = null;
284
                        sample = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
285
                        Graphics2D gAux = sample.createGraphics();
286

    
287
                        double xSeparation = markerFillProperties.getXSeparation(); // TODO apply CartographicSupport
288
                        double ySeparation = markerFillProperties.getYSeparation(); // TODO apply CartographicSupport
289
                        markerSymbol.drawInsideRectangle(gAux, new AffineTransform(), rProv);
290

    
291
                        rProv.setRect(0, 0,
292
                                        rProv.getWidth() + xSeparation,
293
                                        rProv.getHeight() + ySeparation);
294

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

    
299

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

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

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

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

    
342
        public void setXMLEntity(XMLEntity xml) {
343
                setDescription(xml.getStringProperty("desc"));
344
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
345
//                fillStyle = xml.getIntProperty("fillStyle");
346

    
347
                markerSymbol = (AbstractMarkerSymbol) SymbologyFactory.
348
                                                        createSymbolFromXML(xml.getChild(0), null);
349
                markerFillProperties = (MarkerFillProperties) SymbologyFactory.
350
                                                        createStyleFromXML(xml.getChild(1), null);
351
        }
352

    
353
        public void setMarker(IMarkerSymbol marker) {
354
                this.markerSymbol = marker;
355
        }
356

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

    
361
        }
362

    
363
        public void setFillProperties(IMarkerFillProperties markerFillStyle) {
364
                this.markerFillProperties = markerFillStyle;
365
        }
366

    
367
        public IMarkerFillProperties getFillProperties() {
368
                return markerFillProperties;
369
        }
370

    
371
}