|
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.Graphics2D;
|
|
44 |
import java.awt.Paint;
|
|
45 |
import java.awt.Rectangle;
|
|
46 |
import java.awt.Shape;
|
|
47 |
import java.awt.TexturePaint;
|
|
48 |
import java.awt.geom.AffineTransform;
|
|
49 |
import java.awt.image.BufferedImage;
|
|
50 |
import java.io.File;
|
|
51 |
import java.io.IOException;
|
|
52 |
|
|
53 |
import javax.imageio.ImageIO;
|
|
54 |
import javax.print.attribute.PrintRequestAttributeSet;
|
|
55 |
|
|
56 |
import org.apache.log4j.Logger;
|
|
57 |
|
|
58 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
|
|
59 |
import com.iver.cit.gvsig.fmap.Messages;
|
|
60 |
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
|
|
61 |
import com.iver.cit.gvsig.fmap.core.FShape;
|
|
62 |
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
|
|
63 |
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
|
|
64 |
import com.iver.cit.gvsig.fmap.core.styles.IMarkerFillProperties;
|
|
65 |
import com.iver.cit.gvsig.fmap.core.styles.MarkerFillProperties;
|
|
66 |
import com.iver.utiles.XMLEntity;
|
|
67 |
|
|
68 |
/**
|
|
69 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
|
70 |
*/
|
|
71 |
public class PictureFillSymbol extends AbstractFillSymbol {
|
|
72 |
private double angle = 0;
|
|
73 |
private double xScale = 1;
|
|
74 |
private double yScale = 1;
|
|
75 |
|
|
76 |
private IMarkerFillProperties markerFillProperties = new MarkerFillProperties();
|
|
77 |
private String imagePath;
|
|
78 |
private BufferedImage img;
|
|
79 |
|
|
80 |
public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
|
|
81 |
g.setClip(shp);
|
|
82 |
try{
|
|
83 |
if (xScale<=0 && yScale<=0)
|
|
84 |
return;
|
|
85 |
|
|
86 |
BufferedImage img = getImg();
|
|
87 |
AffineTransform scaleInstance = AffineTransform.
|
|
88 |
getScaleInstance(xScale, yScale);
|
|
89 |
|
|
90 |
final double imageWidth = img.getWidth() * xScale;
|
|
91 |
final double imageHeight = img.getHeight() * yScale;
|
|
92 |
|
|
93 |
if (imageWidth==0 || imageHeight==0) return;
|
|
94 |
|
|
95 |
Rectangle rProv = new Rectangle();
|
|
96 |
rProv.setFrame(0, 0, imageWidth, imageHeight);
|
|
97 |
Paint resulPatternFill = null;
|
|
98 |
|
|
99 |
Graphics2D gAux = img.createGraphics();
|
|
100 |
|
|
101 |
double xSeparation = markerFillProperties.getXSeparation(); // TODO apply CartographicSupport
|
|
102 |
double ySeparation = markerFillProperties.getYSeparation(); // TODO apply CartographicSupport
|
|
103 |
double xOffset = markerFillProperties.getXOffset();// TODO apply CartographicSupport
|
|
104 |
double yOffset = markerFillProperties.getYOffset();// TODO apply CartographicSupport
|
|
105 |
|
|
106 |
rProv.setRect(0, 0,
|
|
107 |
rProv.getWidth() + xSeparation,
|
|
108 |
rProv.getHeight() + ySeparation);
|
|
109 |
|
|
110 |
BufferedImage bi = new BufferedImage(rProv.width, rProv.height, BufferedImage.TYPE_INT_ARGB);
|
|
111 |
gAux = bi.createGraphics();
|
|
112 |
AffineTransform tx = AffineTransform.getScaleInstance(xScale, yScale);
|
|
113 |
tx.translate(xSeparation*0.5, ySeparation*0.5);
|
|
114 |
gAux.rotate(angle);
|
|
115 |
gAux.drawImage(img, tx, null);
|
|
116 |
|
|
117 |
resulPatternFill = new TexturePaint(bi,rProv);
|
|
118 |
g.translate(xOffset, yOffset);
|
|
119 |
g.setPaint(resulPatternFill);
|
|
120 |
g.fill(shp);
|
|
121 |
|
|
122 |
} catch (IOException e) {
|
|
123 |
Logger.getLogger(this.getClass()).error(Messages.getString("failed_to_read_image_file"), e);
|
|
124 |
}
|
|
125 |
g.setClip(null);
|
|
126 |
if (getOutline()!=null) {
|
|
127 |
getOutline().draw(g, affineTransform, shp);
|
|
128 |
}
|
|
129 |
}
|
|
130 |
|
|
131 |
private BufferedImage getImg() throws IOException {
|
|
132 |
if (img == null) {
|
|
133 |
img = ImageIO.read(new File(imagePath));
|
|
134 |
}
|
|
135 |
return img;
|
|
136 |
}
|
|
137 |
|
|
138 |
public void setImage(File imageFile) throws IOException{
|
|
139 |
img = ImageIO.read(imageFile);
|
|
140 |
imagePath = imageFile.getAbsolutePath();
|
|
141 |
}
|
|
142 |
|
|
143 |
public void drawInsideRectangle(Graphics2D g,
|
|
144 |
AffineTransform scaleInstance, Rectangle r) {
|
|
145 |
draw(g, null, new FPolygon2D(new GeneralPathX(r)));
|
|
146 |
}
|
|
147 |
|
|
148 |
public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform,
|
|
149 |
Shape shp) {
|
|
150 |
// TODO Auto-generated method stub
|
|
151 |
throw new Error("Not yet implemented!");
|
|
152 |
}
|
|
153 |
|
|
154 |
public ISymbol getSymbolForSelection() {
|
|
155 |
// TODO Auto-generated method stub
|
|
156 |
throw new Error("Not yet implemented!");
|
|
157 |
}
|
|
158 |
|
|
159 |
public int getSymbolType() {
|
|
160 |
return FShape.POLYGON;
|
|
161 |
}
|
|
162 |
|
|
163 |
public XMLEntity getXMLEntity() {
|
|
164 |
XMLEntity xml = new XMLEntity();
|
|
165 |
xml.putProperty("className", getClassName());
|
|
166 |
xml.putProperty("angle", angle);
|
|
167 |
xml.putProperty("scaleX", xScale);
|
|
168 |
xml.putProperty("scaleY", yScale);
|
|
169 |
xml.putProperty("imagePath", imagePath);
|
|
170 |
XMLEntity fillPropertiesXML = markerFillProperties.getXMLEntity();
|
|
171 |
fillPropertiesXML.putProperty("id", "fillProperties");
|
|
172 |
xml.addChild(fillPropertiesXML);
|
|
173 |
if (getOutline()!=null) {
|
|
174 |
XMLEntity outlineXML = getOutline().getXMLEntity();
|
|
175 |
outlineXML.putProperty("id", "outline symbol");
|
|
176 |
}
|
|
177 |
return xml;
|
|
178 |
}
|
|
179 |
|
|
180 |
public void setXMLEntity(XMLEntity xml) {
|
|
181 |
File f = new File(xml.getStringProperty("imagePath"));
|
|
182 |
try {
|
|
183 |
setImage(f);
|
|
184 |
setAngle(xml.getDoubleProperty("angle"));
|
|
185 |
setXScale(xml.getDoubleProperty("scaleX"));
|
|
186 |
setYScale(xml.getDoubleProperty("scaleY"));
|
|
187 |
|
|
188 |
markerFillProperties = (IMarkerFillProperties) SymbologyFactory.
|
|
189 |
createStyleFromXML(
|
|
190 |
xml.firstChild("id", "fillProperties"), "fill properties");
|
|
191 |
|
|
192 |
XMLEntity outlineXML = xml.firstChild("id", "outline symbol");
|
|
193 |
if (outlineXML != null) {
|
|
194 |
setOutline((ILineSymbol) SymbologyFactory.createSymbolFromXML(outlineXML, "outline"));
|
|
195 |
}
|
|
196 |
}catch (IOException e) {
|
|
197 |
Logger.getLogger(this.getClass()).error(Messages.getString("failed_to_read_image_file"), e);
|
|
198 |
}
|
|
199 |
|
|
200 |
}
|
|
201 |
|
|
202 |
public String getClassName() {
|
|
203 |
return getClass().getName();
|
|
204 |
}
|
|
205 |
|
|
206 |
public void print(Graphics2D g, AffineTransform at, FShape shape,
|
|
207 |
PrintRequestAttributeSet properties) throws ReadDriverException {
|
|
208 |
// TODO Auto-generated method stub
|
|
209 |
throw new Error("Not yet implemented!");
|
|
210 |
}
|
|
211 |
|
|
212 |
public IMarkerFillProperties getMarkerFillProperties() {
|
|
213 |
return markerFillProperties;
|
|
214 |
}
|
|
215 |
|
|
216 |
public void setMarkerFillProperties(IMarkerFillProperties prop) {
|
|
217 |
this.markerFillProperties = prop;
|
|
218 |
}
|
|
219 |
|
|
220 |
public double getAngle() {
|
|
221 |
return angle;
|
|
222 |
}
|
|
223 |
|
|
224 |
public void setAngle(double angle) {
|
|
225 |
this.angle = angle;
|
|
226 |
}
|
|
227 |
|
|
228 |
public double getXScale() {
|
|
229 |
return xScale;
|
|
230 |
}
|
|
231 |
|
|
232 |
public void setXScale(double xScale) {
|
|
233 |
this.xScale = xScale;
|
|
234 |
}
|
|
235 |
|
|
236 |
public double getYScale() {
|
|
237 |
return yScale;
|
|
238 |
}
|
|
239 |
|
|
240 |
public void setYScale(double yScale) {
|
|
241 |
this.yScale = yScale;
|
|
242 |
}
|
|
243 |
|
|
244 |
public String getImagePath() {
|
|
245 |
return imagePath;
|
|
246 |
}
|
|
247 |
|
|
248 |
}
|
0 |
249 |
|