Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / tools / grid / Grid.java @ 30349

History | View | Annotate | Download (5.37 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 org.gvsig.fmap.mapcontrol.tools.grid;
42

    
43
import java.awt.Color;
44
import java.awt.geom.Point2D;
45

    
46
import org.gvsig.fmap.geom.primitive.Envelope;
47
import org.gvsig.fmap.mapcontext.ViewPort;
48
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
49

    
50

    
51
/**
52
 * Clase encargada de gestionar las diferentes operaciones que se realizan
53
 * sobre el grid.
54
 *
55
 * @author Vicente Caballero Navarro
56
 */
57
public class Grid {
58
        public static boolean DefaultShowGrid = false;
59
        public static double DefaultGridSizeX = 1000;
60
        public static double DefaultGridSizeY = 1000;
61
        public static boolean DefaultAdjustGrid=false;
62

    
63
        public static boolean SHOWGRID = false;
64
        public static double GRIDSIZEX = 1000;
65
        public static double GRIDSIZEY = 1000;
66
        public static boolean ADJUSTGRID=false;
67

    
68
        private boolean grid = SHOWGRID;
69
        private double gridSizeX = GRIDSIZEX;
70
        private double gridSizeY = GRIDSIZEY;
71
        private ViewPort viewport=null;
72
        private boolean adjustGrid=ADJUSTGRID;
73

    
74
        /**
75
         * Inserta el viewPort.
76
         *
77
         * @param vp
78
         */
79
        public void setViewPort(ViewPort vp) {
80
                viewport = vp;
81

    
82
//                if (gridSize == 0) {
83
//                        gridSize = viewport.toMapDistance(25);
84
//                }
85
        }
86

    
87
        /**
88
         * Ajusta un punto de la imagen que se pasa como  par?metro al handler m?s
89
         * cercano si se encuentra lo suficientemente  cerca y devuelve la
90
         * distancia del punto original al punto ajustado
91
         *
92
         * @param point
93
         *
94
         * @return Distancia del punto que se pasa como par?metro al punto ajustado
95
         */
96
        public double adjustToGrid(Point2D point) {
97
                if (adjustGrid) {
98
                        Point2D auxp = new Point2D.Double(0, 0);
99
                        double x = ((point.getX() + gridSizeX) % gridSizeX) -
100
                                ((auxp.getX()) % gridSizeX);
101
                        double y = ((point.getY() + gridSizeY) % gridSizeY) -
102
                                ((auxp.getY()) % gridSizeY);
103
                        Point2D p = (Point2D) point.clone();
104
                        if (x>gridSizeX/2){
105
                                x=x-gridSizeX;
106
                        }
107
                        if (y>gridSizeY/2){
108
                                y=y-gridSizeY;
109
                        }
110
                        point.setLocation((point.getX() - x), (point.getY() - y));
111
                        return p.distance(point);
112
                }
113
                return Double.MAX_VALUE;
114
        }
115

    
116
        /**
117
         * Dibuja el grid sobre el graphics que se pasa como par?metro
118
         *
119
         * @param g Graphics sobre el que dibujar el grid.
120
         */
121
        public void drawGrid(MapControlDrawer mapControlDrawer) {
122
                if (!grid) {
123
                        return;
124
                }
125
                if (viewport.fromMapDistance(gridSizeX) > 3
126
                                && viewport.fromMapDistance(gridSizeY) > 3) {
127
                        mapControlDrawer.setColor(Color.lightGray);
128

    
129
                        Envelope extent = viewport.getAdjustedExtent();
130
                        Point2D auxp = new Point2D.Double(0, 0);
131
                        if (extent==null)
132
                                return;
133
                        for (double i = extent.getMinimum(0); i < (extent.getMaximum(0) + gridSizeX); i += gridSizeX) {
134
                                for (double j = extent.getMinimum(0); j < (extent.getMaximum(1) + gridSizeY); j += gridSizeY) {
135
                                        Point2D po = new Point2D.Double(i, j);
136
                                        Point2D point = viewport.fromMapPoint(po);
137
                                        double x = ((po.getX() + gridSizeX) % gridSizeX)
138
                                                        - ((auxp.getX()) % gridSizeX);
139
                                        double y = ((po.getY() + gridSizeY) % gridSizeY)
140
                                                        - ((auxp.getY()) % gridSizeY);
141
                                        x = (point.getX() - viewport.fromMapDistance(x));
142
                                        y = (point.getY() + viewport.fromMapDistance(y));
143

    
144
                                        mapControlDrawer.drawRect((int) x, (int) y, 1, 1);
145

    
146
                                }
147
                        }
148
                }
149
        }
150

    
151
        /**
152
         * Inserta un boolean que indica si se utiliza o no el grid y de esta forma
153
         * dibujarse.
154
         *
155
         * @param b boolean
156
         */
157
        public void setShowGrid(boolean b) {
158
                grid = b;
159
        }
160

    
161
        /**
162
         * Devuelve true si se usa el grid.
163
         *
164
         * @return True si se usa el grid.
165
         */
166
        public boolean isShowGrid() {
167
                return grid;
168
        }
169

    
170
        /**
171
         * Inserta un boolean que indica si se ajusta o no al grid y de esta forma
172
         * dibujarse.
173
         *
174
         * @param b boolean
175
         */
176
        public void setAdjustGrid(boolean b) {
177
                adjustGrid = b;
178
        }
179

    
180
        /**
181
         * Devuelve true si se ha de ajustar al grid.
182
         *
183
         * @return True si se est? ajustando al grid.
184
         */
185
        public boolean isAdjustGrid() {
186
                return adjustGrid;
187
        }
188

    
189
        public double getGridSizeX() {
190
                return gridSizeX;
191
        }
192
        public double getGridSizeY() {
193
                return gridSizeY;
194
        }
195
        public void setGridSizeX(double gridSize) {
196
                this.gridSizeX = gridSize;
197
        }
198
        public void setGridSizeY(double gridSize) {
199
                this.gridSizeY = gridSize;
200
        }
201
}