Statistics
| Revision:

root / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / symbols / GradientFillSymbol.java @ 22457

History | View | Annotate | Download (8.89 KB)

1 20768 jdominguez
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 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
package org.gvsig.symbology.fmap.symbols;
42
43
import java.awt.BasicStroke;
44
import java.awt.Color;
45
import java.awt.Graphics2D;
46
import java.awt.Rectangle;
47
import java.awt.Shape;
48
import java.awt.geom.AffineTransform;
49
import java.awt.geom.Ellipse2D;
50
import java.awt.geom.Line2D;
51
import java.awt.geom.Rectangle2D;
52
53
import javax.print.attribute.PrintRequestAttributeSet;
54
55
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
56
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
57
import com.iver.cit.gvsig.fmap.core.FShape;
58
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
59
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
60
import com.iver.cit.gvsig.fmap.core.symbols.AbstractFillSymbol;
61
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
62
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
63
import com.iver.cit.gvsig.fmap.core.symbols.SimpleFillSymbol;
64
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException;
65
import com.iver.utiles.StringUtilities;
66
import com.iver.utiles.XMLEntity;
67
import com.iver.utiles.swing.threads.Cancellable;
68
69
/**
70
 *
71
 * Allows the user to fill a polygon with a gradient color.This gradient
72
 * can be painted with 4 different methods (linal, rectangular, circular and
73
 * buffered) and, for each one will be possible to modify its angle, percentage
74
 * and intervals.
75
 *
76
 * @author pepe vidal salvador - jose.vidal.salvador@iver.es
77
 */
78
public class GradientFillSymbol  extends AbstractFillSymbol {
79
80
        private GradientFillSymbol gradsym;
81
        private SimpleFillSymbol sfs = new SimpleFillSymbol();
82
83
        private double angle;
84
        private double percentage = 100;
85
        private int intervals;
86 22457 jdominguez
        private Color[] gradientcolor=null;
87 20768 jdominguez
88
        private int style;
89
90
91
        public ISymbol getSymbolForSelection() {
92
93
                if (gradsym==null)gradsym=new GradientFillSymbol();
94
                return gradsym;
95
96
        }
97
98
99
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) {
100
101
                Rectangle2D bounds = shp.getBounds();
102
                double radius = Math.abs(Math.max(bounds.getHeight(), bounds.getWidth()));
103
                double centerX = bounds.getCenterX();
104
                double centerY = bounds.getCenterY();
105
106
                setFillColor(gradientcolor[0]);
107
                g.fill(shp);
108
109
                g.setClip(shp);
110
111
                if (radius <= 1) {
112
                        g.setColor(gradientcolor[0]);
113
                        g.drawLine((int) centerX, (int) centerY, (int) centerX, (int) centerY);
114
                        return;
115
                }
116
117
                /*Creation of a new shape depending on the style of the gradient*/
118
                Shape myShape = null;
119
                if (style==0) {//Style=buffered
120
                        myShape = shp;
121
                } else if (style==1) {//Style=lineal
122
                        myShape = new Line2D.Double(bounds.getMinX(), bounds.getMaxY(), bounds.getMaxX(), bounds.getMaxY());
123
                } else        if (style==2) {//Style=circular
124
                        myShape = new Ellipse2D.Double(bounds.getMinX(), bounds.getMinY(), radius, radius);
125
                } else if (style==3) {//Style=rectangular
126
                        myShape = shp.getBounds();
127
                }
128
129
                if (intervals == 0) intervals = 1;
130
131
                /*The variable separation will be used to specify the width of the line*/
132
                int separation=(int) Math.abs(Math.min(bounds.getHeight(), bounds.getWidth()))/(int)intervals;
133
                /*If the style is linal the separation will be double*/
134
                if (style==1)separation*=2;
135
136
                /*If the user wants to apply a rotation*/
137
                boolean bRotate = angle != 0;
138
139
                /*All the intervals are painted*/
140
                for (int i = intervals; (cancel==null || !cancel.isCanceled()) && i>0 ; i--) {
141
                        BasicStroke stroke = new BasicStroke((float) (separation*i*percentage*0.01)+1);
142
143
                        FShape fShape = new FPolygon2D(
144
                                        new GeneralPathX(stroke.createStrokedShape(myShape)));
145
146
                        double cenX,cenY;
147
                        cenX = fShape.getBounds().getCenterX();
148
                        cenY = fShape.getBounds().getCenterY();
149
150
                        if (bRotate) {
151
152
                                g.translate(cenX, cenY);
153
                                g.rotate(angle);
154
155
                                fShape.transform(AffineTransform.getTranslateInstance(-cenX,-cenY));
156
157
                        }
158
                        sfs.setFillColor(gradientcolor[i-1]);
159
                        sfs.draw(g, affineTransform, fShape, cancel);
160
161
162
                        if (bRotate) {
163
164
                                g.rotate(-angle);
165
                                g.translate(-cenX, -cenY);
166
                        }
167
                }
168
169
                g.setClip(null);
170
171
                /*If an outline exists it is painted*/
172
                ILineSymbol outLineSymbol = getOutline();
173
                if (outLineSymbol != null && hasOutline())
174
                        outLineSymbol.draw(g, affineTransform, shp, null);
175
        }
176
177
        public XMLEntity getXMLEntity() {
178
179
                XMLEntity xml = new XMLEntity();
180
181
                xml.putProperty("className", getClassName());
182
                xml.putProperty("desc", getDescription());
183
                xml.putProperty("isShapeVisible", isShapeVisible());
184
                xml.putProperty("angle", angle);
185
                xml.putProperty("intervals",intervals);
186
                xml.putProperty("percentage", percentage);
187
                xml.putProperty("style", style);
188
                xml.putProperty("referenceSystem", getReferenceSystem());
189
                xml.putProperty("unit", getUnit());
190
191
                String[] strColors = new String[gradientcolor.length];
192
                for (int i = 0; i < strColors.length; i++) {
193
                        strColors[i] = StringUtilities.color2String(gradientcolor[i]);
194
                }
195
                xml.putProperty("gradientcolor", strColors);
196
197
                Color c2 = getFillColor();
198
                if (c2!=null)
199
                        xml.putProperty("color", StringUtilities.color2String(getFillColor()));
200
201
                ILineSymbol outline = getOutline();
202
203
                if (outline!=null) {
204
                        XMLEntity xmlOutline = outline.getXMLEntity();
205
                        xmlOutline.putProperty("id", "outline");
206
                        xml.addChild(xmlOutline);
207
                }
208
209
                xml.putProperty("hasOutline", hasOutline());
210
211
                return xml;
212
        }
213
214
        public void setXMLEntity(XMLEntity xml) {
215
216
                if (xml.contains("color"))
217
                        setFillColor(StringUtilities.string2Color(xml.getStringProperty("color")));
218
219
220
                setDescription(xml.getStringProperty("desc"));
221
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
222
                setAngle(xml.getDoubleProperty("angle"));
223
                setStyle(xml.getIntProperty("style"));
224
                setIntervals(xml.getIntProperty("intervals"));
225
                setPercentage(xml.getDoubleProperty("percentage"));
226
227
                String[] strColors = xml.getStringArrayProperty("gradientcolor");
228
229
                Color[] cc = new Color[strColors.length];
230
                for (int i = 0; i < cc.length; i++) {
231
                        cc[i] = StringUtilities.string2Color(strColors[i]);
232
                }
233
                setGradientColor(cc);
234
235
236
237
                XMLEntity xmlOutline = xml.firstChild("id", "outline");
238
                if (xmlOutline != null) {
239
                        setOutline((ILineSymbol) SymbologyFactory.
240
                                        createSymbolFromXML(xmlOutline, "outline"));
241
                }
242
243
                if (xml.contains("unit")) { // remove this line when done
244
245
                // measure unit (for outline)
246
                setUnit(xml.getIntProperty("unit"));
247
248
                // reference system (for outline)
249
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
250
                }
251
                //has Outline
252
                if(xml.contains("hasOutline"))
253
                        setHasOutline(xml.getBooleanProperty("hasOutline"));
254
        }
255
256
257
258
259
        public int getSymbolType() {
260
                return FShape.POLYGON;
261
        }
262
263
        public void drawInsideRectangle(Graphics2D g,AffineTransform scaleInstance, Rectangle r) throws SymbolDrawingException {
264
265
                draw(g, null, new FPolygon2D(new GeneralPathX(r)), null);
266
267
        }
268
269
270
        public String getClassName() {
271
                return getClass().getName();
272
        }
273
274
        public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties) throws ReadDriverException {
275
                // TODO Auto-generated method stub
276
                // TODO implement it!
277
                throw new Error("Not yet implemented!");
278
        }
279
280
        /**
281
         * Defines the angle for the rotation of the image when it is added to create the
282
         * padding
283
         *
284
         * @return angle
285
         */
286
        public double getAngle() {
287
                return angle;
288
        }
289
        /**
290
         * Sets the angle for the rotation of the image when it is added to create the padding
291
         * @param angle
292
         */
293
        public void setAngle(double angle) {
294
                this.angle = angle;
295
        }
296
297
        public double getPercentage() {
298
                return percentage;
299
        }
300
301
        public void setPercentage(double percentage) {
302
                this.percentage= percentage;
303
        }
304
305
        public int getIntervals() {
306
                return intervals;
307
        }
308
309
        public void setIntervals(int intervals) {
310
                this.intervals= intervals;
311
        }
312
313
        public int getStyle(){
314
315
                return style;
316
317
        }
318
        public void setStyle(int style) {
319
320
                this.style=style;
321
        }
322
323
        public Color[] getGradientColor(){
324
325
                return gradientcolor;
326
        }
327
328
        public void setGradientColor(Color[] gradientcolor) {
329
                this.gradientcolor=gradientcolor;
330
        }
331
332
}