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