Statistics
| Revision:

root / org.gvsig.legend.dotdensity.app.mainplugin / trunk / org.gvsig.legend.dotdensity.app.mainplugin / src / main / java / org / gvsig / symbology / fmap / symbols / DotDensityFillSymbol.java @ 824

History | View | Annotate | Download (10.7 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 org.gvsig.compat.print.PrintAttributes;
104
import org.gvsig.fmap.dal.feature.Feature;
105
import org.gvsig.fmap.geom.Geometry;
106
import org.gvsig.fmap.geom.GeometryLocator;
107
import org.gvsig.fmap.geom.GeometryManager;
108
import org.gvsig.fmap.geom.primitive.Point;
109
import org.gvsig.fmap.geom.primitive.Polygon;
110
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
111
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
112
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl.AbstractFillSymbol;
113
import org.gvsig.tools.ToolsLocator;
114
import org.gvsig.tools.dynobject.DynStruct;
115
import org.gvsig.tools.persistence.PersistenceManager;
116
import org.gvsig.tools.persistence.PersistentState;
117
import org.gvsig.tools.persistence.exception.PersistenceException;
118
import org.gvsig.tools.task.Cancellable;
119
import org.gvsig.tools.util.Callable;
120
import org.slf4j.Logger;
121
import org.slf4j.LoggerFactory;
122

    
123
/**
124
 * <p>
125
 * Symbol that draws a set of points within a polygon. The amount of points is
126
 * defined by the field dotCount.<br>
127
 * </p>
128
 * <p>
129
 * This symbol only draws the points. The outline and the fill of the polygon is
130
 * handled by a SimpleFillSymboll where a DotDensityFillSymbol should be
131
 * embedded.<br>
132
 * </p>
133
 * @author jaume dominguez faus - jaume.dominguez@iver.es
134
 *
135
 */
136
public class DotDensityFillSymbol extends AbstractFillSymbol {
137
    private static final Logger logger = LoggerFactory.getLogger(DotDensityFillSymbol.class);
138
    public static final String
139
    DOT_DENSITY_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME =
140
        "DOT_DENSITY_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME";
141

    
142
        private int  dotCount;
143
        private double dotSize;
144
        private double dotSpacing;
145
        private Color dotColor = Color.BLACK;
146

    
147
        private boolean fixedPlacement;
148
        private PrintRequestAttributeSet properties;
149
        private GeometryManager geoManager=GeometryLocator.getGeometryManager();
150

    
151
        public DotDensityFillSymbol() {
152
                super();
153
        }
154

    
155
        public ISymbol getSymbolForSelection() {
156
                return this; // the selection color is applied in the SimpleFillSymbol
157
        }
158

    
159
    public void draw(Graphics2D g, AffineTransform atr,
160
        Geometry theGeom, Feature f, Cancellable cancel) {
161

    
162
        try {
163
            Geometry clo_geo = theGeom.cloneGeometry();
164
            clo_geo.transform(atr);
165

    
166
            Point interiorPoint=geoManager.createPoint(0, 0, Geometry.SUBTYPES.GEOM2D);
167
            int maxIntentos = 35;
168
            int width = clo_geo.getBounds().width;
169
            int height = clo_geo.getBounds().height;
170
            int minx = clo_geo.getBounds().x;
171
            int miny = clo_geo.getBounds().y;
172
            Random random = new Random();
173
            g.setClip(clo_geo.getShape());
174
            g.setColor(getDotColor());
175
            g.setBackground(null);
176

    
177
            int size = (int) dotSize;
178
            for (int i = 0; (cancel==null || !cancel.isCanceled()) && i < dotCount; i++) {
179
                int x,y;
180
                int intentos = 0;
181
                /* Introducimos este bucle para procurar que los puntos
182
                 * queden dentro del shape. Le ponemos adem?s un
183
                 * numero m?ximo de intentos para evitar las posibilidad de
184
                 * un bucle infinito o excesivamente reiterativo.
185
                 */
186
                do{
187
                    x = (int) Math.abs(random.nextDouble() * width);
188
                    y = (int) Math.abs(random.nextDouble() * height);
189
                    x = x + minx;
190
                    y = y + miny;
191
                    intentos++;
192
                    interiorPoint.setX(x);
193
                    interiorPoint.setY(y);
194
                }  while (intentos<maxIntentos && !clo_geo.contains(interiorPoint));
195
                g.fillRect(x, y, size, size);
196
            }
197
            g.setClip(null);
198

    
199
        } catch (Exception e) {
200
            logger.warn("An error has been produced drawing density points for : "+theGeom.toString());
201
        }
202
        }
203

    
204
    public void saveToState(PersistentState state) throws PersistenceException {
205

    
206
        super.saveToState(state);
207

    
208
        // color
209
        Color c = getDotColor();
210
        if (c == null) {
211
            c = Color.BLACK;
212
        }
213

    
214
        state.set("dotColor", c);
215
        state.set("dotCount", dotCount);
216
        state.set("dotSize", dotSize);
217
        state.set("dotSpacing", dotSpacing);
218
    }
219

    
220

    
221
        public int getSymbolType() {
222
                return Geometry.TYPES.SURFACE;
223
        }
224

    
225
    public void drawInsideRectangle(Graphics2D g,
226
        AffineTransform scaleInstance,
227
        Rectangle r,
228
        PrintAttributes properties) throws SymbolDrawingException {
229

    
230
                int x = r.x;
231
                int y = r.y;
232
                int width = r.width;
233
                int height= r.height;
234
                int size = height / 5;
235
                g.setColor(getDotColor());
236
                g.setBackground(null);
237
                g.fillRect((int) (x+width*0.2), (int) (y+height*0.2), size, size);
238
                g.fillRect((int) (x+width*0.25), (int) (y+height*0.7), size, size);
239
                g.fillRect((int) (x+width*0.35), (int) (y+height*0.5), size, size);
240
                g.fillRect((int) (x+width*0.6), (int) (y+height*0.1), size, size);
241
                g.fillRect((int) (x+width*0.7), (int) (y+height*0.8), size, size);
242
                g.fillRect((int) (x+width*0.8), (int) (y+height*0.3), size, size);
243
                g.fillRect((int) (x+width*0.9), (int) (y+height*0.6), size, size);
244
        }
245

    
246
    public void loadFromState(PersistentState state)
247
        throws PersistenceException {
248

    
249
        super.loadFromState(state);
250

    
251
        Object obj_col = state.get("dotColor");
252
        this.setDotColor((Color) obj_col);
253

    
254
        this.setDotCount(state.getInt("dotCount"));
255
        this.setDotSize(state.getDouble("dotSize"));
256
        this.setDotSpacing(state.getDouble("dotSpacing"));
257
    }
258

    
259

    
260
        /**
261
         * @return
262
         * @uml.property  name="dotCount"
263
         */
264
        public int getDotCount() {
265
                return dotCount;
266
        }
267

    
268
        /**
269
         * @param dotCount
270
         * @uml.property  name="dotCount"
271
         */
272
        public void setDotCount(int dotCount) {
273
                this.dotCount = dotCount;
274
        }
275

    
276
        /**
277
         * @return
278
         * @uml.property  name="dotSize"
279
         */
280
        public double getDotSize() {
281
                return dotSize;
282
        }
283

    
284
        public void setDotSize(double dotSize) {
285
                this.dotSize = dotSize;
286
        }
287

    
288
        /**
289
         * @return
290
         * @uml.property  name="dotSpacing"
291
         */
292
        public double getDotSpacing() {
293
                return dotSpacing;
294
        }
295

    
296
        /**
297
         * @param dotSpacing
298
         * @uml.property  name="dotSpacing"
299
         */
300
        public void setDotSpacing(double dotSpacing) {
301
                this.dotSpacing = dotSpacing;
302
        }
303

    
304
        public Color getDotColor() {
305
                return dotColor;
306
        }
307

    
308
        public void setDotColor(Color dotColor) {
309
                this.dotColor = dotColor;
310
        }
311

    
312
    /* (non-Javadoc)
313
     * @see org.gvsig.fmap.mapcontext.rendering.symbols.IPrintable#print(java.awt.Graphics2D, java.awt.geom.AffineTransform, org.gvsig.fmap.geom.Geometry, org.gvsig.compat.print.PrintAttributes)
314
     */
315
    public void print(Graphics2D g, AffineTransform at, Geometry shape,
316
        PrintAttributes prop) {
317

    
318
        // this.properties.=prop;
319
        draw(g, at, shape, null, null);
320
        // this.properties=null;
321

    
322
    }
323

    
324
    public static class RegisterPersistence implements Callable {
325

    
326
        public Object call() throws Exception {
327
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
328
            if( manager.getDefinition(
329
                DOT_DENSITY_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
330
                DynStruct definition = manager.addDefinition(
331
                    DotDensityFillSymbol.class,
332
                        DOT_DENSITY_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME,
333
                        DOT_DENSITY_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME +
334
                        " Persistence definition",
335
                        null,
336
                        null
337
                );
338

    
339
                // Extend the Symbol base definition
340
                definition.extend(manager.getDefinition(
341
                    AbstractFillSymbol.FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME));
342

    
343
                definition.addDynFieldObject("dotColor").setMandatory(true)
344
                .setClassOfValue(Color.class);
345
                definition.addDynFieldInt("dotCount").setMandatory(true);
346
                definition.addDynFieldDouble("dotSize").setMandatory(true);
347
                definition.addDynFieldDouble("dotSpacing").setMandatory(true);
348
            }
349
            return Boolean.TRUE;
350
        }
351

    
352
    }
353

    
354

    
355
}