root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / symbols / PictureLineSymbol.java @ 12657
History | View | Annotate | Download (7.76 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 |
package com.iver.cit.gvsig.fmap.core.symbols; |
42 |
|
43 |
import java.awt.BasicStroke; |
44 |
import java.awt.Graphics2D; |
45 |
import java.awt.Shape; |
46 |
import java.awt.geom.AffineTransform; |
47 |
import java.awt.geom.PathIterator; |
48 |
import java.awt.geom.Point2D; |
49 |
import java.awt.image.BufferedImage; |
50 |
import java.io.File; |
51 |
import java.io.IOException; |
52 |
import java.net.URL; |
53 |
|
54 |
import javax.imageio.ImageIO; |
55 |
import javax.print.attribute.PrintRequestAttributeSet; |
56 |
|
57 |
import org.apache.log4j.Logger; |
58 |
|
59 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
60 |
import com.iver.cit.gvsig.fmap.Messages; |
61 |
import com.iver.cit.gvsig.fmap.core.FPolyline2D; |
62 |
import com.iver.cit.gvsig.fmap.core.FShape; |
63 |
import com.iver.cit.gvsig.fmap.core.SymbologyFactory; |
64 |
import com.iver.utiles.XMLEntity; |
65 |
|
66 |
/**
|
67 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
68 |
*/
|
69 |
public class PictureLineSymbol extends AbstractLineSymbol { |
70 |
transient private BufferedImage img; |
71 |
transient private BufferedImage selImg; |
72 |
transient private PictureLineSymbol selectionSym; |
73 |
private String selImagePath; |
74 |
private double width; |
75 |
private String imagePath; |
76 |
private boolean selected; |
77 |
private double xScale = 1; |
78 |
private double yScale = 1; |
79 |
|
80 |
public PictureLineSymbol() {
|
81 |
super();
|
82 |
} |
83 |
|
84 |
public PictureLineSymbol(URL imageURL, URL selImageURL) throws IOException { |
85 |
this(new File(imageURL.getFile()), selImageURL !=null ? new File(selImageURL.getFile()) : null); |
86 |
} |
87 |
|
88 |
public PictureLineSymbol(File imageFile, File selImageFile) throws IOException { |
89 |
setImage(imageFile); |
90 |
if (selImageFile!=null) |
91 |
setSelImage(selImageFile); |
92 |
} |
93 |
|
94 |
public void setImage(File imageFile) throws IOException{ |
95 |
img = ImageIO.read(imageFile);
|
96 |
imagePath = imageFile.getAbsolutePath(); |
97 |
// imageOutline = null;
|
98 |
} |
99 |
|
100 |
public void setSelImage(File imageFile) throws IOException{ |
101 |
if (imageFile!=null && imageFile.exists()) { |
102 |
selImg = ImageIO.read(imageFile);
|
103 |
selImagePath = imageFile.getAbsolutePath(); |
104 |
} else {
|
105 |
selImg = img; |
106 |
selImagePath = imagePath; |
107 |
} |
108 |
|
109 |
// imageOutline = null;
|
110 |
} |
111 |
|
112 |
public void setLineWidth(double width) { |
113 |
this.width = width;
|
114 |
|
115 |
} |
116 |
|
117 |
public double getLineWidth() { |
118 |
return width;
|
119 |
} |
120 |
|
121 |
public ISymbol getSymbolForSelection() {
|
122 |
if (selectionSym == null) { |
123 |
selectionSym = (PictureLineSymbol) SymbologyFactory.createSymbolFromXML(getXMLEntity(), getDescription()); |
124 |
selectionSym.selected=true;
|
125 |
selectionSym.selectionSym = selectionSym; // avoid too much lazy creations
|
126 |
|
127 |
} |
128 |
return selectionSym;
|
129 |
|
130 |
} |
131 |
|
132 |
private BufferedImage getImg() throws IOException { |
133 |
if (img == null) { |
134 |
img = ImageIO.read(new File(imagePath)); |
135 |
// imageOutline = null;
|
136 |
} |
137 |
return img;
|
138 |
} |
139 |
private BufferedImage getSelImg() throws IOException { |
140 |
if (selImg == null) { |
141 |
if (selImagePath!= null) { |
142 |
selImg = ImageIO.read(new File(selImagePath)); |
143 |
} else return getImg(); |
144 |
// imageOutline = null;
|
145 |
} |
146 |
return selImg;
|
147 |
} |
148 |
|
149 |
public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) { |
150 |
|
151 |
g.draw(new BasicStroke((float) width).createStrokedShape(shp)); |
152 |
|
153 |
try{
|
154 |
BufferedImage img = selected ? getSelImg() : getImg();
|
155 |
if (xScale<=0 && yScale<=0) |
156 |
return;
|
157 |
AffineTransform scaleInstance = AffineTransform.getScaleInstance(xScale, yScale); |
158 |
|
159 |
final double halfImageHeight = (img.getHeight() * yScale * 0.5); |
160 |
PathIterator iterator = ((FPolyline2D) shp).getPathIterator(null, 0.8); |
161 |
double[] theData = new double[6]; |
162 |
Point2D firstPoint = null, startPoint = null, endPoint = null; |
163 |
if (!iterator.isDone()) {
|
164 |
if ( iterator.currentSegment(theData) != PathIterator.SEG_CLOSE) { |
165 |
firstPoint = new Point2D.Double(theData[0], theData[1]); |
166 |
} |
167 |
} |
168 |
while (!iterator.isDone()) {
|
169 |
|
170 |
int theType = iterator.currentSegment(theData);
|
171 |
switch (theType) {
|
172 |
case PathIterator.SEG_MOVETO: |
173 |
startPoint = new Point2D.Double(theData[0], theData[1]); |
174 |
|
175 |
endPoint = null;
|
176 |
iterator.next(); |
177 |
|
178 |
continue;
|
179 |
|
180 |
case PathIterator.SEG_LINETO: |
181 |
case PathIterator.SEG_QUADTO: |
182 |
case PathIterator.SEG_CUBICTO: |
183 |
endPoint = new Point2D.Double(theData[0], theData[1]); |
184 |
|
185 |
break;
|
186 |
case PathIterator.SEG_CLOSE: |
187 |
endPoint = startPoint; |
188 |
startPoint = firstPoint; |
189 |
break;
|
190 |
} |
191 |
|
192 |
double theta = ((endPoint.getX() - startPoint.getX()) / (endPoint.getY() - startPoint.getY()));
|
193 |
// int x = (int) startPoint.getX();
|
194 |
// int y = (int) startPoint.getY() - halfImageHeight;
|
195 |
// g.translate(x, y);
|
196 |
// g.rotate(theta);
|
197 |
//
|
198 |
|
199 |
double x = startPoint.getX();
|
200 |
double y = startPoint.getY();
|
201 |
|
202 |
g.translate(x, y); |
203 |
g.rotate(theta); |
204 |
g.drawImage(img, scaleInstance, null);
|
205 |
|
206 |
g.rotate(-theta); |
207 |
g.translate(-x, -y); |
208 |
|
209 |
startPoint = endPoint; |
210 |
iterator.next(); |
211 |
|
212 |
} |
213 |
|
214 |
} catch (IOException e) { |
215 |
Logger.getLogger(this.getClass()).error(Messages.getString("failed_to_read_image_file"), e); |
216 |
} |
217 |
g.setClip(null);
|
218 |
} |
219 |
|
220 |
public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform, |
221 |
Shape shp) {
|
222 |
// TODO Implement it
|
223 |
throw new Error("Not yet implemented!"); |
224 |
|
225 |
} |
226 |
|
227 |
public XMLEntity getXMLEntity() {
|
228 |
XMLEntity xml = new XMLEntity();
|
229 |
xml.putProperty("className", getClassName());
|
230 |
xml.putProperty("isShapeVisible", isShapeVisible());
|
231 |
xml.putProperty("desc", getDescription());
|
232 |
xml.putProperty("imagePath", imagePath);
|
233 |
xml.putProperty("selImagePath", selImagePath);
|
234 |
xml.putProperty("lineWidth", getLineWidth());
|
235 |
xml.putProperty("xScale", getXScale());
|
236 |
xml.putProperty("yScale", getYScale());
|
237 |
|
238 |
// measure unit
|
239 |
xml.putProperty("unit", getUnit());
|
240 |
|
241 |
// reference system
|
242 |
xml.putProperty("referenceSystem", getReferenceSystem());
|
243 |
|
244 |
return xml;
|
245 |
|
246 |
} |
247 |
|
248 |
public void setXMLEntity(XMLEntity xml) { |
249 |
setDescription(xml.getStringProperty("desc"));
|
250 |
setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
|
251 |
imagePath = xml.getStringProperty("imagePath");
|
252 |
selImagePath = xml.getStringProperty("selImagePath");
|
253 |
setLineWidth(xml.getDoubleProperty("lineWidth"));
|
254 |
setXScale(xml.getDoubleProperty("xScale"));
|
255 |
setYScale(xml.getDoubleProperty("yScale"));
|
256 |
setReferenceSystem(xml.getIntProperty("referenceSystem"));
|
257 |
setUnit(xml.getIntProperty("unit"));
|
258 |
|
259 |
} |
260 |
|
261 |
public void setYScale(double yScale) { |
262 |
this.yScale = yScale;
|
263 |
|
264 |
} |
265 |
|
266 |
public void setXScale(double xScale) { |
267 |
this.xScale = xScale;
|
268 |
} |
269 |
|
270 |
public String getClassName() { |
271 |
return getClass().getName();
|
272 |
} |
273 |
|
274 |
public void print(Graphics2D g, AffineTransform at, FShape shape, |
275 |
PrintRequestAttributeSet properties) throws ReadDriverException { |
276 |
// TODO Implement it
|
277 |
throw new Error("Not yet implemented!"); |
278 |
|
279 |
} |
280 |
|
281 |
public String getSelImagePath() { |
282 |
return selImagePath;
|
283 |
} |
284 |
|
285 |
public String getImagePath() { |
286 |
return imagePath;
|
287 |
} |
288 |
|
289 |
public double getXScale() { |
290 |
return xScale;
|
291 |
} |
292 |
|
293 |
public double getYScale() { |
294 |
return yScale;
|
295 |
} |
296 |
} |