svn-gvsig-desktop / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / symbols / GradientFillSymbol.java @ 18863
History | View | Annotate | Download (9.22 KB)
1 | 18763 | 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 | |||
87 | private Color[] gradientcolor=null; |
||
88 | private int indexgradientcolor; |
||
89 | |||
90 | private int style; |
||
91 | |||
92 | |||
93 | public ISymbol getSymbolForSelection() {
|
||
94 | |||
95 | if (gradsym==null)gradsym=new GradientFillSymbol(); |
||
96 | return gradsym;
|
||
97 | |||
98 | } |
||
99 | |||
100 | |||
101 | public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) { |
||
102 | |||
103 | Rectangle2D bounds = shp.getBounds();
|
||
104 | double radius = Math.abs(Math.max(bounds.getHeight(), bounds.getWidth())); |
||
105 | double centerX = bounds.getCenterX();
|
||
106 | double centerY = bounds.getCenterY();
|
||
107 | |||
108 | setFillColor(gradientcolor[0]);
|
||
109 | g.fill(shp); |
||
110 | |||
111 | g.setClip(shp); |
||
112 | |||
113 | if (radius <= 1) { |
||
114 | g.setColor(gradientcolor[0]);
|
||
115 | g.drawLine((int) centerX, (int) centerY, (int) centerX, (int) centerY); |
||
116 | return;
|
||
117 | } |
||
118 | |||
119 | /*Creation of a new shape depending on the style of the gradient*/
|
||
120 | Shape myShape = null; |
||
121 | if (style==0) {//Style=buffered |
||
122 | myShape = shp; |
||
123 | } else if (style==1) {//Style=lineal |
||
124 | myShape = new Line2D.Double(bounds.getMinX(), bounds.getMaxY(), bounds.getMaxX(), bounds.getMaxY()); |
||
125 | } else if (style==2) {//Style=circular |
||
126 | myShape = new Ellipse2D.Double(bounds.getMinX(), bounds.getMinY(), radius, radius); |
||
127 | } else if (style==3) {//Style=rectangular |
||
128 | myShape = shp.getBounds(); |
||
129 | } |
||
130 | |||
131 | if (intervals == 0) intervals = 1; |
||
132 | |||
133 | /*The variable separation will be used to specify the width of the line*/
|
||
134 | int separation=(int) Math.abs(Math.min(bounds.getHeight(), bounds.getWidth()))/(int)intervals; |
||
135 | /*If the style is linal the separation will be double*/
|
||
136 | if (style==1)separation*=2; |
||
137 | |||
138 | /*If the user wants to apply a rotation*/
|
||
139 | boolean bRotate = angle != 0; |
||
140 | |||
141 | /*All the intervals are painted*/
|
||
142 | for (int i = intervals; (cancel==null || !cancel.isCanceled()) && i>0 ; i--) { |
||
143 | BasicStroke stroke = new BasicStroke((float) (separation*i*percentage*0.01)+1); |
||
144 | |||
145 | FShape fShape = new FPolygon2D(
|
||
146 | new GeneralPathX(stroke.createStrokedShape(myShape)));
|
||
147 | |||
148 | double cenX,cenY;
|
||
149 | cenX = fShape.getBounds().getCenterX(); |
||
150 | cenY = fShape.getBounds().getCenterY(); |
||
151 | |||
152 | if (bRotate) {
|
||
153 | |||
154 | g.translate(cenX, cenY); |
||
155 | g.rotate(angle); |
||
156 | |||
157 | fShape.transform(AffineTransform.getTranslateInstance(-cenX,-cenY));
|
||
158 | |||
159 | } |
||
160 | sfs.setFillColor(gradientcolor[i-1]);
|
||
161 | sfs.draw(g, affineTransform, fShape, cancel); |
||
162 | |||
163 | |||
164 | if (bRotate) {
|
||
165 | |||
166 | g.rotate(-angle); |
||
167 | g.translate(-cenX, -cenY); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | g.setClip(null);
|
||
172 | |||
173 | /*If an outline exists it is painted*/
|
||
174 | ILineSymbol outLineSymbol = getOutline(); |
||
175 | if (outLineSymbol != null && hasOutline()) |
||
176 | outLineSymbol.draw(g, affineTransform, shp, null);
|
||
177 | } |
||
178 | |||
179 | public XMLEntity getXMLEntity() {
|
||
180 | |||
181 | XMLEntity xml = new XMLEntity();
|
||
182 | |||
183 | xml.putProperty("className", getClassName());
|
||
184 | xml.putProperty("desc", getDescription());
|
||
185 | xml.putProperty("isShapeVisible", isShapeVisible());
|
||
186 | xml.putProperty("angle", angle);
|
||
187 | xml.putProperty("intervals",intervals);
|
||
188 | xml.putProperty("percentage", percentage);
|
||
189 | xml.putProperty("style", style);
|
||
190 | xml.putProperty("indexgradientcolor",indexgradientcolor);
|
||
191 | xml.putProperty("referenceSystem", getReferenceSystem());
|
||
192 | xml.putProperty("unit", getUnit());
|
||
193 | |||
194 | |||
195 | String[] strColors = new String[gradientcolor.length]; |
||
196 | for (int i = 0; i < strColors.length; i++) { |
||
197 | strColors[i] = StringUtilities.color2String(gradientcolor[i]); |
||
198 | } |
||
199 | xml.putProperty("gradientcolor", strColors);
|
||
200 | |||
201 | Color c2 = getFillColor();
|
||
202 | if (c2!=null) |
||
203 | xml.putProperty("color", StringUtilities.color2String(getFillColor()));
|
||
204 | |||
205 | ILineSymbol outline = getOutline(); |
||
206 | |||
207 | if (outline!=null) { |
||
208 | XMLEntity xmlOutline = outline.getXMLEntity(); |
||
209 | xmlOutline.putProperty("id", "outline"); |
||
210 | xml.addChild(xmlOutline); |
||
211 | } |
||
212 | |||
213 | xml.putProperty("hasOutline", hasOutline());
|
||
214 | |||
215 | return xml;
|
||
216 | } |
||
217 | |||
218 | public void setXMLEntity(XMLEntity xml) { |
||
219 | |||
220 | if (xml.contains("color")) |
||
221 | setFillColor(StringUtilities.string2Color(xml.getStringProperty("color")));
|
||
222 | |||
223 | |||
224 | setDescription(xml.getStringProperty("desc"));
|
||
225 | setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
|
||
226 | setAngle(xml.getDoubleProperty("angle"));
|
||
227 | setStyle(xml.getIntProperty("style"));
|
||
228 | setIntervals(xml.getIntProperty("intervals"));
|
||
229 | setPercentage(xml.getDoubleProperty("percentage"));
|
||
230 | setindexgradientcolor(xml.getIntProperty("indexgradientcolor"));
|
||
231 | |||
232 | String[] strColors = xml.getStringArrayProperty("gradientcolor"); |
||
233 | |||
234 | Color[] cc = new Color[strColors.length]; |
||
235 | for (int i = 0; i < cc.length; i++) { |
||
236 | cc[i] = StringUtilities.string2Color(strColors[i]); |
||
237 | } |
||
238 | setGradientColor(cc); |
||
239 | |||
240 | |||
241 | |||
242 | XMLEntity xmlOutline = xml.firstChild("id", "outline"); |
||
243 | if (xmlOutline != null) { |
||
244 | setOutline((ILineSymbol) SymbologyFactory. |
||
245 | createSymbolFromXML(xmlOutline, "outline"));
|
||
246 | } |
||
247 | |||
248 | if (xml.contains("unit")) { // remove this line when done |
||
249 | |||
250 | // measure unit (for outline)
|
||
251 | setUnit(xml.getIntProperty("unit"));
|
||
252 | |||
253 | // reference system (for outline)
|
||
254 | setReferenceSystem(xml.getIntProperty("referenceSystem"));
|
||
255 | } |
||
256 | //has Outline
|
||
257 | if(xml.contains("hasOutline")) |
||
258 | setHasOutline(xml.getBooleanProperty("hasOutline"));
|
||
259 | } |
||
260 | |||
261 | |||
262 | |||
263 | |||
264 | public int getSymbolType() { |
||
265 | return FShape.POLYGON;
|
||
266 | } |
||
267 | |||
268 | public void drawInsideRectangle(Graphics2D g,AffineTransform scaleInstance, Rectangle r) throws SymbolDrawingException { |
||
269 | |||
270 | draw(g, null, new FPolygon2D(new GeneralPathX(r)), null); |
||
271 | |||
272 | } |
||
273 | |||
274 | |||
275 | public String getClassName() { |
||
276 | return getClass().getName();
|
||
277 | } |
||
278 | |||
279 | public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties) throws ReadDriverException { |
||
280 | // TODO Auto-generated method stub
|
||
281 | // TODO implement it!
|
||
282 | throw new Error("Not yet implemented!"); |
||
283 | } |
||
284 | |||
285 | /**
|
||
286 | * Defines the angle for the rotation of the image when it is added to create the
|
||
287 | * padding
|
||
288 | *
|
||
289 | * @return angle
|
||
290 | */
|
||
291 | public double getAngle() { |
||
292 | return angle;
|
||
293 | } |
||
294 | /**
|
||
295 | * Sets the angle for the rotation of the image when it is added to create the padding
|
||
296 | * @param angle
|
||
297 | */
|
||
298 | public void setAngle(double angle) { |
||
299 | this.angle = angle;
|
||
300 | } |
||
301 | |||
302 | public double getPercentage() { |
||
303 | return percentage;
|
||
304 | } |
||
305 | |||
306 | public void setPercentage(double percentage) { |
||
307 | this.percentage= percentage;
|
||
308 | } |
||
309 | |||
310 | public int getIntervals() { |
||
311 | return intervals;
|
||
312 | } |
||
313 | |||
314 | public void setIntervals(int intervals) { |
||
315 | this.intervals= intervals;
|
||
316 | } |
||
317 | |||
318 | public int getStyle(){ |
||
319 | |||
320 | return style;
|
||
321 | |||
322 | } |
||
323 | public void setStyle(int style) { |
||
324 | |||
325 | this.style=style;
|
||
326 | } |
||
327 | |||
328 | public Color[] getGradientColor(){ |
||
329 | |||
330 | return gradientcolor;
|
||
331 | } |
||
332 | |||
333 | public void setGradientColor(Color[] gradientcolor) { |
||
334 | |||
335 | this.gradientcolor=gradientcolor;
|
||
336 | } |
||
337 | |||
338 | public void setindexgradientcolor(int colorindex) { |
||
339 | |||
340 | this.indexgradientcolor=colorindex;
|
||
341 | } |
||
342 | |||
343 | public int getindexgradientcolor() { |
||
344 | |||
345 | return indexgradientcolor;
|
||
346 | |||
347 | } |
||
348 | |||
349 | |||
350 | } |