Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1009 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / CADGrid.java @ 12649

History | View | Annotate | Download (4.66 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 com.iver.cit.gvsig.gui.cad;
42

    
43
import com.iver.cit.gvsig.fmap.ViewPort;
44

    
45
import java.awt.Color;
46
import java.awt.Graphics;
47
import java.awt.geom.Point2D;
48
import java.awt.geom.Rectangle2D;
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 CADGrid {
58
        private boolean grid = false;
59
        private double gridSizeX = 1000;
60
        private double gridSizeY = 1000;
61
        private ViewPort viewport;
62
        private boolean adjustGrid;
63

    
64
        /**
65
         * Inserta el viewPort.
66
         *
67
         * @param vp
68
         */
69
        public void setViewPort(ViewPort vp) {
70
                viewport = vp;
71

    
72
//                if (gridSize == 0) {
73
//                        gridSize = viewport.toMapDistance(25);
74
//                }
75
        }
76

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

    
106
        /**
107
         * Dibuja el grid sobre el graphics que se pasa como par?metro
108
         *
109
         * @param g Graphics sobre el que dibujar el grid.
110
         */
111
        public void drawGrid(Graphics g) {
112
                if (!grid) {
113
                        return;
114
                }
115
                if (viewport.fromMapDistance(gridSizeX) > 3
116
                                && viewport.fromMapDistance(gridSizeY) > 3) {
117
                        g.setColor(Color.lightGray);
118

    
119
                        Rectangle2D extent = viewport.getAdjustedExtent();
120
                        Point2D auxp = new Point2D.Double(0, 0);
121

    
122
                        for (double i = extent.getMinX(); i < (extent.getMaxX() + gridSizeX); i += gridSizeX) {
123
                                for (double j = extent.getMinY(); j < (extent.getMaxY() + gridSizeY); j += gridSizeY) {
124
                                        Point2D po = new Point2D.Double(i, j);
125
                                        Point2D point = viewport.fromMapPoint(po);
126
                                        double x = ((po.getX() + gridSizeX) % gridSizeX)
127
                                                        - ((auxp.getX()) % gridSizeX);
128
                                        double y = ((po.getY() + gridSizeY) % gridSizeY)
129
                                                        - ((auxp.getY()) % gridSizeY);
130
                                        x = (point.getX() - viewport.fromMapDistance(x));
131
                                        y = (point.getY() + viewport.fromMapDistance(y));
132

    
133
                                        g.drawRect((int) x, (int) y, 1, 1);
134

    
135
                                }
136
                        }
137
                }
138
        }
139

    
140
        /**
141
         * Inserta un boolean que indica si se utiliza o no el grid y de esta forma
142
         * dibujarse.
143
         *
144
         * @param b boolean
145
         */
146
        public void setShowGrid(boolean b) {
147
                grid = b;
148
        }
149

    
150
        /**
151
         * Devuelve true si se usa el grid.
152
         *
153
         * @return True si se usa el grid.
154
         */
155
        public boolean isShowGrid() {
156
                return grid;
157
        }
158

    
159
        /**
160
         * Inserta un boolean que indica si se ajusta o no al grid y de esta forma
161
         * dibujarse.
162
         *
163
         * @param b boolean
164
         */
165
        public void setAdjustGrid(boolean b) {
166
                adjustGrid = b;
167
        }
168

    
169
        /**
170
         * Devuelve true si se ha de ajustar al grid.
171
         *
172
         * @return True si se est? ajustando al grid.
173
         */
174
        public boolean isAdjustGrid() {
175
                return adjustGrid;
176
        }
177

    
178
        public double getGridSizeX() {
179
                return gridSizeX;
180
        }
181
        public double getGridSizeY() {
182
                return gridSizeY;
183
        }
184
        public void setGridSizeX(double gridSize) {
185
                this.gridSizeX = gridSize;
186
        }
187
        public void setGridSizeY(double gridSize) {
188
                this.gridSizeY = gridSize;
189
        }
190
}