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 @ 793

History | View | Annotate | Download (10.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: 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.mapcontext.rendering.symbols.ISymbol;
107
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
108
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl.AbstractFillSymbol;
109
import org.gvsig.tools.ToolsLocator;
110
import org.gvsig.tools.dynobject.DynStruct;
111
import org.gvsig.tools.persistence.PersistenceManager;
112
import org.gvsig.tools.persistence.PersistentState;
113
import org.gvsig.tools.persistence.exception.PersistenceException;
114
import org.gvsig.tools.task.Cancellable;
115
import org.gvsig.tools.util.Callable;
116
import org.slf4j.Logger;
117
import org.slf4j.LoggerFactory;
118

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

    
138
        private int  dotCount;
139
        private double dotSize;
140
        private double dotSpacing;
141
        private Color dotColor = Color.BLACK;
142

    
143
        private boolean fixedPlacement;
144
        private PrintRequestAttributeSet properties;
145

    
146
        public DotDensityFillSymbol() {
147
                super();
148
        }
149

    
150
        public ISymbol getSymbolForSelection() {
151
                return this; // the selection color is applied in the SimpleFillSymbol
152
        }
153

    
154
    public void draw(Graphics2D g, AffineTransform atr,
155
        Geometry theGeom, Feature f, Cancellable cancel) {
156

    
157
        try {
158
            Geometry clo_geo = theGeom.cloneGeometry();
159
            clo_geo.transform(atr);
160

    
161
            int maxIntentos = 35;
162
            int width = clo_geo.getBounds().width;
163
            int height = clo_geo.getBounds().height;
164
            int minx = clo_geo.getBounds().x;
165
            int miny = clo_geo.getBounds().y;
166
            Random random = new Random();
167
            g.setClip(clo_geo.getShape());
168
            g.setColor(getDotColor());
169
            g.setBackground(null);
170
            int size = (int) dotSize;
171
            for (int i = 0; (cancel==null || !cancel.isCanceled()) && i < dotCount; i++) {
172
                    int x,y;
173
                    int intentos = 0;
174
                    /* Introducimos este bucle para procurar que los puntos
175
                     * queden dentro del shape. Le ponemos adem?s un
176
                     * numero m?ximo de intentos para evitar las posibilidad de
177
                     * un bucle infinito o excesivamente reiterativo.
178
                    */
179
                    do{
180
                            x = (int) Math.abs(random.nextDouble() * width);
181
                            y = (int) Math.abs(random.nextDouble() * height);
182
                            x = x + minx;
183
                            y = y + miny;
184
                            intentos++;
185
                    } while (intentos<maxIntentos && !clo_geo.contains(x, y));
186
                    g.fillRect(x, y, size, size);
187
            }
188
            g.setClip(null);
189
        } catch (Exception e) {
190
            logger.warn("An error has been produced drawing density points for : "+theGeom.toString());
191
        }
192
        }
193

    
194
    public void saveToState(PersistentState state) throws PersistenceException {
195

    
196
        super.saveToState(state);
197

    
198
        // color
199
        Color c = getDotColor();
200
        if (c == null) {
201
            c = Color.BLACK;
202
        }
203

    
204
        state.set("dotColor", c);
205
        state.set("dotCount", dotCount);
206
        state.set("dotSize", dotSize);
207
        state.set("dotSpacing", dotSpacing);
208
    }
209

    
210

    
211
        public int getSymbolType() {
212
                return Geometry.TYPES.SURFACE;
213
        }
214

    
215
    public void drawInsideRectangle(Graphics2D g,
216
        AffineTransform scaleInstance,
217
        Rectangle r,
218
        PrintAttributes properties) throws SymbolDrawingException {
219

    
220
                int x = r.x;
221
                int y = r.y;
222
                int width = r.width;
223
                int height= r.height;
224
                int size = height / 5;
225
                g.setColor(getDotColor());
226
                g.setBackground(null);
227
                g.fillRect((int) (x+width*0.2), (int) (y+height*0.2), size, size);
228
                g.fillRect((int) (x+width*0.25), (int) (y+height*0.7), size, size);
229
                g.fillRect((int) (x+width*0.35), (int) (y+height*0.5), size, size);
230
                g.fillRect((int) (x+width*0.6), (int) (y+height*0.1), size, size);
231
                g.fillRect((int) (x+width*0.7), (int) (y+height*0.8), size, size);
232
                g.fillRect((int) (x+width*0.8), (int) (y+height*0.3), size, size);
233
                g.fillRect((int) (x+width*0.9), (int) (y+height*0.6), size, size);
234
        }
235

    
236
    public void loadFromState(PersistentState state)
237
        throws PersistenceException {
238

    
239
        super.loadFromState(state);
240

    
241
        Object obj_col = state.get("dotColor");
242
        this.setDotColor((Color) obj_col);
243

    
244
        this.setDotCount(state.getInt("dotCount"));
245
        this.setDotSize(state.getDouble("dotSize"));
246
        this.setDotSpacing(state.getDouble("dotSpacing"));
247
    }
248

    
249

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

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

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

    
274
        public void setDotSize(double dotSize) {
275
                this.dotSize = dotSize;
276
        }
277

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

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

    
294
        public Color getDotColor() {
295
                return dotColor;
296
        }
297

    
298
        public void setDotColor(Color dotColor) {
299
                this.dotColor = dotColor;
300
        }
301

    
302
    /* (non-Javadoc)
303
     * @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)
304
     */
305
    public void print(Graphics2D g, AffineTransform at, Geometry shape,
306
        PrintAttributes prop) {
307

    
308
        // this.properties.=prop;
309
        draw(g, at, shape, null, null);
310
        // this.properties=null;
311

    
312
    }
313

    
314
    public static class RegisterPersistence implements Callable {
315

    
316
        public Object call() throws Exception {
317
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
318
            if( manager.getDefinition(
319
                DOT_DENSITY_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
320
                DynStruct definition = manager.addDefinition(
321
                    DotDensityFillSymbol.class,
322
                        DOT_DENSITY_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME,
323
                        DOT_DENSITY_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME +
324
                        " Persistence definition",
325
                        null,
326
                        null
327
                );
328

    
329
                // Extend the Symbol base definition
330
                definition.extend(manager.getDefinition(
331
                    AbstractFillSymbol.FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME));
332

    
333
                definition.addDynFieldObject("dotColor").setMandatory(true)
334
                .setClassOfValue(Color.class);
335
                definition.addDynFieldInt("dotCount").setMandatory(true);
336
                definition.addDynFieldDouble("dotSize").setMandatory(true);
337
                definition.addDynFieldDouble("dotSpacing").setMandatory(true);
338
            }
339
            return Boolean.TRUE;
340
        }
341

    
342
    }
343

    
344

    
345
}