Statistics
| Revision:

root / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / symbols / DotDensityFillSymbol.java @ 25036

History | View | Annotate | Download (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: DotDensityFillSymbol.java 13953 2007-09-21 12:26:04Z jaume $
45
* $Log$
46
* Revision 1.8  2007-09-21 12:25:32  jaume
47
* cancellation support extended down to the IGeometry and ISymbol level
48
*
49
* Revision 1.7  2007/09/18 14:50:31  caballero
50
* Leyendas sobre el Layout
51
*
52
* Revision 1.6  2007/03/26 14:24:24  jaume
53
* IPrintable refactored
54
*
55
* Revision 1.5  2007/03/09 11:20:56  jaume
56
* Advanced symbology (start committing)
57
*
58
* Revision 1.3.2.4  2007/02/21 16:09:02  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.3.2.3  2007/02/16 10:54:12  jaume
62
* multilayer splitted to multilayerline, multilayermarker,and  multilayerfill
63
*
64
* Revision 1.3.2.2  2007/02/15 16:23:44  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.3.2.1  2007/02/09 07:47:04  jaume
68
* Isymbol moved
69
*
70
* Revision 1.3  2007/01/12 10:08:26  jaume
71
* *** empty log message ***
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.4  2006/11/14 11:10:27  jaume
80
* *** empty log message ***
81
*
82
* Revision 1.3  2006/11/13 09:15:23  jaume
83
* javadoc and some clean-up
84
*
85
* Revision 1.2  2006/11/09 18:39:05  jaume
86
* *** empty log message ***
87
*
88
* Revision 1.1  2006/11/09 10:22:50  jaume
89
* *** empty log message ***
90
*
91
*
92
*/
93
package org.gvsig.symbology.fmap.symbols;
94

    
95
import java.awt.Color;
96
import java.awt.Graphics2D;
97
import java.awt.Rectangle;
98
import java.awt.geom.AffineTransform;
99
import java.util.Random;
100

    
101
import javax.print.attribute.PrintRequestAttributeSet;
102

    
103
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
104
import com.iver.cit.gvsig.fmap.core.FShape;
105
import com.iver.cit.gvsig.fmap.core.symbols.AbstractFillSymbol;
106
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
107
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException;
108
import com.iver.utiles.StringUtilities;
109
import com.iver.utiles.XMLEntity;
110
import com.iver.utiles.swing.threads.Cancellable;
111

    
112
/**
113
 * <p>
114
 * Symbol that draws a set of points within a polygon. The amount of points is
115
 * defined by the field dotCount.<br>
116
 * </p>
117
 * <p>
118
 * This symbol only draws the points. The outline and the fill of the polygon is
119
 * handled by a SimpleFillSymboll where a DotDensityFillSymbol should be
120
 * embedded.<br>
121
 * </p>
122
 * @author jaume dominguez faus - jaume.dominguez@iver.es
123
 *
124
 */
125
public class DotDensityFillSymbol extends AbstractFillSymbol {
126
        private int  dotCount;
127
        private double dotSize;
128
        private double dotSpacing;
129
        private Color dotColor;
130
        private boolean fixedPlacement;
131

    
132
        public DotDensityFillSymbol() {
133
                super();
134
        }
135

    
136
        public ISymbol getSymbolForSelection() {
137
                return this; // the selection color is applied in the SimpleFillSymbol
138
        }
139

    
140
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) {
141
                int maxIntentos = 35;
142
                int width = shp.getBounds().width;
143
                int height = shp.getBounds().height;
144
                int minx = shp.getBounds().x;
145
                int miny = shp.getBounds().y;
146
                Random random = new Random();
147
                g.setClip(shp);
148
                g.setColor(getDotColor());
149
                g.setBackground(null);
150
                int size = (int) dotSize;
151
                for (int i = 0; (cancel==null || !cancel.isCanceled()) && i < dotCount; i++) {
152
                        int x,y;
153
                        int intentos = 0;
154
                        /* Introducimos este bucle para procurar que los puntos
155
                         * queden dentro del shape. Le ponemos adem?s un
156
                         * numero m?ximo de intentos para evitar las posibilidad de
157
                         * un bucle infinito o excesivamente reiterativo.
158
                        */
159
                        do{
160
                                x = (int) Math.abs(random.nextDouble() * width);
161
                                y = (int) Math.abs(random.nextDouble() * height);
162
                                x = x + minx;
163
                                y = y + miny;
164
                                intentos++;
165
                        } while (intentos<maxIntentos && !shp.contains(x, y));
166
                        g.fillRect(x, y, size, size);
167
                }
168
                g.setClip(null);
169
        }
170

    
171

    
172
        public XMLEntity getXMLEntity() {
173
                XMLEntity xml = new XMLEntity();
174
                xml.putProperty("className", getClassName());
175

    
176
                // color
177
                Color c = getDotColor();
178
                if (c!= null)
179
                        xml.putProperty("color", StringUtilities.color2String(getDotColor()));
180

    
181
                // description
182
                xml.putProperty("desc", getDescription());
183

    
184
                // is shape visible
185
                xml.putProperty("isShapeVisible", isShapeVisible());
186

    
187
                // dot count
188
                xml.putProperty("dotCount", dotCount);
189

    
190
                // dot size
191
                xml.putProperty("dotSize", dotSize);
192

    
193
                // dot spacing
194
                xml.putProperty("dotSpacing", dotSpacing);
195

    
196
                xml.putProperty("referenceSystem", getReferenceSystem());
197
                xml.putProperty("unit", getUnit());
198

    
199
                return xml;
200
        }
201

    
202
        public int getSymbolType() {
203
                return FShape.POLYGON;
204
        }
205

    
206
        public void drawInsideRectangle(Graphics2D g,
207
                        AffineTransform scaleInstance, Rectangle r) throws SymbolDrawingException {
208
                int x = r.x;
209
                int y = r.y;
210
                int width = r.width;
211
                int height= r.height;
212
                int size = height / 5;
213
                g.setColor(getDotColor());
214
                g.setBackground(null);
215
                g.fillRect((int) (x+width*0.2), (int) (y+height*0.2), size, size);
216
                g.fillRect((int) (x+width*0.25), (int) (y+height*0.7), size, size);
217
                g.fillRect((int) (x+width*0.35), (int) (y+height*0.5), size, size);
218
                g.fillRect((int) (x+width*0.6), (int) (y+height*0.1), size, size);
219
                g.fillRect((int) (x+width*0.7), (int) (y+height*0.8), size, size);
220
                g.fillRect((int) (x+width*0.8), (int) (y+height*0.3), size, size);
221
                g.fillRect((int) (x+width*0.9), (int) (y+height*0.6), size, size);
222
        }
223

    
224
        public String getClassName() {
225
                return getClass().getName();
226
        }
227

    
228
        public void setXMLEntity(XMLEntity xml) {
229
                // color
230
                if (xml.contains("color"))
231
                        setDotColor(StringUtilities.string2Color(xml.getStringProperty("color")));
232

    
233
                // description
234
                setDescription(xml.getStringProperty("desc"));
235

    
236
                // is shape visible
237
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
238

    
239
                // dot count
240
                dotCount = xml.getIntProperty("dotCount");
241

    
242
                // dot size
243
                dotSize = xml.getDoubleProperty("dotSize");
244

    
245
                // dot spacing
246
                dotSpacing = xml.getDoubleProperty("dotSpacing");
247
        }
248

    
249
        /**
250
         * @return
251
         * @uml.property  name="dotCount"
252
         */
253
        public int getDotCount() {
254
                return dotCount;
255
        }
256

    
257
        /**
258
         * @param dotCount
259
         * @uml.property  name="dotCount"
260
         */
261
        public void setDotCount(int dotCount) {
262
                this.dotCount = dotCount;
263
        }
264

    
265
        /**
266
         * @return
267
         * @uml.property  name="dotSize"
268
         */
269
        public double getDotSize() {
270
                return dotSize;
271
        }
272

    
273
        /**
274
         * @param dotSize
275
         * @uml.property  name="dotSize"
276
         */
277
        public void setDotSize(double dotSize) {
278
                this.dotSize = dotSize;
279
        }
280

    
281
        /**
282
         * @return
283
         * @uml.property  name="dotSpacing"
284
         */
285
        public double getDotSpacing() {
286
                return dotSpacing;
287
        }
288

    
289
        /**
290
         * @param dotSpacing
291
         * @uml.property  name="dotSpacing"
292
         */
293
        public void setDotSpacing(double dotSpacing) {
294
                this.dotSpacing = dotSpacing;
295
        }
296

    
297
        public Color getDotColor() {
298
                return dotColor;
299
        }
300

    
301
        public void setDotColor(Color dotColor) {
302
                this.dotColor = dotColor;
303
        }
304

    
305
        public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties) throws ReadDriverException {
306
                // TODO Implement it
307
                throw new Error("Not yet implemented!");
308

    
309
        }
310

    
311
}