Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / ViewPort.java @ 285

History | View | Annotate | Download (7.09 KB)

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