root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / ViewPort.java @ 416
History | View | Annotate | Download (7.7 KB)
1 |
/* Generated by Together */
|
---|---|
2 |
|
3 |
package com.iver.cit.gvsig.fmap; |
4 |
|
5 |
import java.awt.Color; |
6 |
import java.awt.Dimension; |
7 |
import java.awt.Point; |
8 |
import java.awt.geom.AffineTransform; |
9 |
import java.awt.geom.NoninvertibleTransformException; |
10 |
import java.awt.geom.Point2D; |
11 |
import java.awt.geom.Rectangle2D; |
12 |
import java.util.ArrayList; |
13 |
|
14 |
import org.cresques.cts.IProjection; |
15 |
|
16 |
public class ViewPort { |
17 |
public static int KILOMETROS = 1; |
18 |
public static int METROS = 2; |
19 |
public static int MILLAS = 3; |
20 |
public static int YARDAS = 4; |
21 |
public static int PIES = 5; |
22 |
public static int PULGADAS = 6; |
23 |
|
24 |
/**
|
25 |
* Resoluci?n (Puntos por pulgada) de la vista actual.
|
26 |
* Se necesita para los c?lculos de escala geogr?fica.
|
27 |
*/
|
28 |
private static int dpi = java.awt.Toolkit.getDefaultToolkit().getScreenResolution(); |
29 |
|
30 |
private Rectangle2D extent; |
31 |
private Rectangle2D adjustedExtent; |
32 |
private ExtentHistory extents = new ExtentHistory(); |
33 |
private Dimension imageSize; |
34 |
private AffineTransform trans = new AffineTransform(); |
35 |
private int distanceUnits = METROS; |
36 |
private int mapUnits = METROS; |
37 |
|
38 |
private ArrayList extentListeners=new ArrayList(); |
39 |
private Point2D offset = new Point2D.Double(0, 0); |
40 |
private Rectangle2D clip; |
41 |
private Color backColor = Color.WHITE; |
42 |
|
43 |
private IProjection proj;
|
44 |
|
45 |
private double dist1pixel; |
46 |
private double dist3pixel; |
47 |
|
48 |
|
49 |
private double scale; |
50 |
|
51 |
public boolean addExtentListener(ExtentListener arg0) { |
52 |
return extentListeners.add(arg0);
|
53 |
} |
54 |
|
55 |
public boolean removeExtentListener(ExtentListener arg0) { |
56 |
return extentListeners.remove(arg0);
|
57 |
} |
58 |
|
59 |
public int fromMapDistance(double d) { |
60 |
Point2D.Double pWorld = new Point2D.Double(1, 1); |
61 |
Point2D.Double pScreen = new Point2D.Double(); |
62 |
|
63 |
double nuevoX;
|
64 |
double nuevoY;
|
65 |
double cX;
|
66 |
double cY;
|
67 |
|
68 |
try {
|
69 |
trans.deltaTransform(pWorld, pScreen); |
70 |
} catch (Exception e) { |
71 |
System.err.print(e.getMessage());
|
72 |
} |
73 |
|
74 |
return (int) (d * pScreen.x); |
75 |
} |
76 |
|
77 |
public Point2D fromMapPoint(double x, double y) { |
78 |
Point2D.Double pWorld = new Point2D.Double(x, y); |
79 |
Point2D.Double pScreen = new Point2D.Double(); |
80 |
|
81 |
double nuevoX;
|
82 |
double nuevoY;
|
83 |
double cX;
|
84 |
double cY;
|
85 |
|
86 |
try {
|
87 |
trans.transform(pWorld, pScreen); |
88 |
} catch (Exception e) { |
89 |
System.err.print(e.getMessage());
|
90 |
} |
91 |
|
92 |
return pScreen;
|
93 |
} |
94 |
|
95 |
public Point2D toMapPoint(int x, int y) { |
96 |
Point pScreen = new Point(x, y); |
97 |
return toMapPoint(pScreen);
|
98 |
} |
99 |
|
100 |
public double toMapDistance(int d) { |
101 |
double dist = d / trans.getScaleX();
|
102 |
return dist;
|
103 |
} |
104 |
|
105 |
public Point2D toMapPoint(Point2D pScreen) { |
106 |
Point2D.Double pWorld = new Point2D.Double(); |
107 |
AffineTransform at;
|
108 |
|
109 |
try {
|
110 |
at = trans.createInverse(); |
111 |
at.transform(pScreen, pWorld); |
112 |
} catch (NoninvertibleTransformException e) { |
113 |
throw new RuntimeException(e); |
114 |
} |
115 |
|
116 |
return pWorld;
|
117 |
} |
118 |
|
119 |
public void setPreviousExtent() { |
120 |
} |
121 |
|
122 |
public Rectangle2D getExtent() { |
123 |
return extent;
|
124 |
} |
125 |
|
126 |
public void setExtent(Rectangle2D r) { |
127 |
|
128 |
//Esto comprueba que el extent no es de anchura o altura = "0"
|
129 |
//y si es as? lo redimensiona.
|
130 |
if (r.getWidth()==0 || r.getHeight()==0){ |
131 |
extent=new Rectangle2D.Double(r.getMinX()-0.1,r.getMinY()-0.1,r.getWidth()+0.2,r.getHeight()+0.2); |
132 |
}else{
|
133 |
extent = r; |
134 |
} |
135 |
//TODO calcular la escala sin usar setScale
|
136 |
|
137 |
//Calcula la transformaci?n af?n
|
138 |
calculateAffineTransform(); |
139 |
} |
140 |
|
141 |
public void setScale(double scale) { |
142 |
this.scale = scale;
|
143 |
|
144 |
//TODO calcular el extent sin usar setExtent
|
145 |
|
146 |
//Calcula la transformaci?n af?n
|
147 |
calculateAffineTransform(); |
148 |
} |
149 |
|
150 |
/**
|
151 |
* Devuelve la escala. Debe estar siempre actualizada y
|
152 |
* no calcularse nunca aqu? pues se utiliza en el dibujado
|
153 |
* para cada geometr?a
|
154 |
* @return
|
155 |
*/
|
156 |
public double getScale() { |
157 |
return proj.getScale(extent.getMinX(), extent.getMaxX(), imageSize.getWidth(), dpi);
|
158 |
} |
159 |
|
160 |
/**
|
161 |
* @return
|
162 |
*/
|
163 |
public AffineTransform getAffineTransform() { |
164 |
return trans;
|
165 |
} |
166 |
/**
|
167 |
* @return Returns the imageSize.
|
168 |
*/
|
169 |
public Dimension getImageSize() { |
170 |
return imageSize;
|
171 |
} |
172 |
/**
|
173 |
* @param imageSize The imageSize to set.
|
174 |
*/
|
175 |
public void setImageSize(Dimension imageSize) { |
176 |
this.imageSize = imageSize;
|
177 |
calculateAffineTransform(); |
178 |
} |
179 |
|
180 |
/**
|
181 |
*
|
182 |
*/
|
183 |
private void calculateAffineTransform() { |
184 |
if ((imageSize == null) || (extent == null)) return; |
185 |
|
186 |
AffineTransform escalado = new AffineTransform(); |
187 |
AffineTransform translacion = new AffineTransform(); |
188 |
|
189 |
double escalaX;
|
190 |
double escalaY;
|
191 |
|
192 |
escalaX = imageSize.getWidth() / extent.getWidth(); |
193 |
escalaY = imageSize.getHeight() / extent.getHeight(); |
194 |
|
195 |
adjustedExtent = new Rectangle2D.Double(); |
196 |
if (escalaX < escalaY) {
|
197 |
scale = escalaX; |
198 |
adjustedExtent.setRect(extent.getX(), extent.getY(), extent.getWidth(), imageSize.getHeight() / scale); |
199 |
} else {
|
200 |
scale = escalaY; |
201 |
adjustedExtent.setRect(extent.getX(), extent.getY(), imageSize.getWidth() / scale, extent.getHeight()); |
202 |
} |
203 |
|
204 |
translacion.setToTranslation(-adjustedExtent.getX(), -adjustedExtent.getY() - |
205 |
adjustedExtent.getHeight()); |
206 |
escalado.setToScale(scale, -scale); |
207 |
|
208 |
AffineTransform offsetTrans = new AffineTransform(); |
209 |
offsetTrans.setToTranslation(offset.getX(), offset.getY()); |
210 |
|
211 |
trans.setToIdentity(); |
212 |
trans.concatenate(offsetTrans); |
213 |
trans.concatenate(escalado); |
214 |
|
215 |
trans.concatenate(translacion); |
216 |
|
217 |
|
218 |
// Calculamos las distancias de 1 pixel y 3 pixel con esa transformaci?n
|
219 |
// de coordenadas, de forma que est?n precalculadas para cuando las necesitemos
|
220 |
AffineTransform at;
|
221 |
try {
|
222 |
at = trans.createInverse(); |
223 |
java.awt.Point pPixel = new java.awt.Point(1, 1); |
224 |
Point2D.Float pProv = new Point2D.Float(); |
225 |
at.deltaTransform(pPixel, pProv); |
226 |
|
227 |
dist1pixel = pProv.x; |
228 |
dist3pixel = 3 * pProv.x;
|
229 |
|
230 |
} catch (NoninvertibleTransformException e) { |
231 |
e.printStackTrace(); |
232 |
} |
233 |
|
234 |
} |
235 |
public void setOffset(Point2D p){ |
236 |
offset=p; |
237 |
} |
238 |
public void setBackColor(Color c) { |
239 |
backColor = c; |
240 |
} |
241 |
|
242 |
public Color getBackColor() { |
243 |
return backColor;
|
244 |
} |
245 |
/**
|
246 |
* @return Returns the adjustedExtent.
|
247 |
*/
|
248 |
public Rectangle2D getAdjustedExtent() { |
249 |
return adjustedExtent;
|
250 |
} |
251 |
/**
|
252 |
* @return Returns the distanceUnits.
|
253 |
*/
|
254 |
public int getDistanceUnits() { |
255 |
return distanceUnits;
|
256 |
} |
257 |
/**
|
258 |
* @param distanceUnits The distanceUnits to set.
|
259 |
*/
|
260 |
public void setDistanceUnits(int distanceUnits) { |
261 |
this.distanceUnits = distanceUnits;
|
262 |
} |
263 |
/**
|
264 |
* @return Returns the mapUnits.
|
265 |
*/
|
266 |
public int getMapUnits() { |
267 |
return mapUnits;
|
268 |
} |
269 |
/**
|
270 |
* @param mapUnits The mapUnits to set.
|
271 |
*/
|
272 |
public void setMapUnits(int mapUnits) { |
273 |
this.mapUnits = mapUnits;
|
274 |
} |
275 |
|
276 |
public int getImageWidth(){ |
277 |
return imageSize.width;
|
278 |
} |
279 |
|
280 |
public int getImageHeight(){ |
281 |
return imageSize.height;
|
282 |
} |
283 |
public double getDist1pixel() { |
284 |
return dist1pixel;
|
285 |
} |
286 |
public void setDist1pixel(double dist1pixel) { |
287 |
this.dist1pixel = dist1pixel;
|
288 |
} |
289 |
public double getDist3pixel() { |
290 |
return dist3pixel;
|
291 |
} |
292 |
public void setDist3pixel(double dist3pixel) { |
293 |
this.dist3pixel = dist3pixel;
|
294 |
} |
295 |
/**
|
296 |
* @return Returns the extents.
|
297 |
*/
|
298 |
public ExtentHistory getExtents() {
|
299 |
return extents;
|
300 |
} |
301 |
|
302 |
/**
|
303 |
*
|
304 |
*/
|
305 |
public void setExtentPrev() { |
306 |
setExtent(extents.removePrev()); |
307 |
} |
308 |
/**
|
309 |
* @return Returns the proj.
|
310 |
*/
|
311 |
public IProjection getProjection() {
|
312 |
return proj;
|
313 |
} |
314 |
/**
|
315 |
* @param proj The proj to set.
|
316 |
*/
|
317 |
public void setProjection(IProjection proj) { |
318 |
this.proj = proj;
|
319 |
} |
320 |
} |