Statistics
| Revision:

svn-gvsig-desktop / branches / gvSIG_CAD_Layout_version / applications / appgvSIG / src / com / iver / cit / gvsig / gui / cad / CadGrid.java @ 1763

History | View | Annotate | Download (4.72 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 java.awt.Color;
44
import java.awt.Graphics;
45
import java.awt.geom.AffineTransform;
46
import java.awt.geom.Point2D;
47
import java.awt.geom.Rectangle2D;
48

    
49

    
50
/**
51
 * Clase encargada de gestionar las diferentes operaciones que se realizan
52
 * sobre el grid.
53
 *
54
 * @author Vicente Caballero Navarro
55
 */
56
public class CadGrid extends com.iver.cit.gvsig.fmap.CadGrid {
57
        private boolean grid = false;
58
        private double gridSizeX = 1;
59
        private double gridSizeY = 1;
60
        private AffineTransform at;
61
        private boolean adjustGrid;
62
        private CADToolAdapter cta;
63

    
64
        /**
65
         * Crea un nuevo CadGrid.
66
         *
67
         * @param cta DOCUMENT ME!
68
         */
69
        public CadGrid(CADToolAdapter cta) {
70
                this.cta = cta;
71
        }
72

    
73
        /**
74
         * Inserta el viewPort.
75
         *
76
         * @param at
77
         */
78
        public void setAT(AffineTransform at) {
79
                this.at = at;
80
        }
81

    
82
        /**
83
         * DOCUMENT ME!
84
         *
85
         * @param d DOCUMENT ME!
86
         */
87
        public void setGridSizeX(double d) {
88
                gridSizeX = d; //(int)cta.fromDistance(d);
89
        }
90

    
91
        /**
92
         * DOCUMENT ME!
93
         *
94
         * @param d DOCUMENT ME!
95
         */
96
        public void setGridSizeY(double d) {
97
                gridSizeY = d; //(int)cta.fromDistance(d);
98
        }
99

    
100
        /**
101
         * DOCUMENT ME!
102
         *
103
         * @return DOCUMENT ME!
104
         */
105
        public double getGridSizeX() {
106
                return gridSizeX;
107
        }
108

    
109
        /**
110
         * DOCUMENT ME!
111
         *
112
         * @return DOCUMENT ME!
113
         */
114
        public double getGridSizeY() {
115
                return gridSizeY;
116
        }
117

    
118
        /**
119
         * Ajusta un punto de la imagen que se pasa como  par?metro al handler m?s
120
         * cercano si se encuentra lo suficientemente  cerca y devuelve la
121
         * distancia del punto original al punto ajustado
122
         *
123
         * @param point
124
         *
125
         * @return Distancia del punto que se pasa como par?metro al punto ajustado
126
         */
127
        public double adjustToGrid(Point2D point) {
128
                if (adjustGrid) {
129
                        Point2D auxp = new Point2D.Double(0, 0);
130
                        double x = ((point.getX() + gridSizeX) % gridSizeX) -
131
                                ((auxp.getX()) % gridSizeX);
132
                        double y = ((point.getY() + gridSizeY) % gridSizeY) -
133
                                ((auxp.getY()) % gridSizeY);
134
                        Point2D p = (Point2D) point.clone();
135

    
136
                        if (x > (gridSizeX / 2)) {
137
                                x = x - gridSizeX;
138
                        }
139

    
140
                        if (y > (gridSizeY / 2)) {
141
                                y = y - gridSizeY;
142
                        }
143

    
144
                        point.setLocation((point.getX() - x), (point.getY() - y));
145

    
146
                        return p.distance(point);
147
                }
148

    
149
                return Double.MAX_VALUE;
150
        }
151

    
152
        /**
153
         * Dibuja el grid sobre el graphics que se pasa como par?metro
154
         *
155
         * @param g Graphics sobre el que dibujar el grid.
156
         */
157
        public void drawGrid(Graphics g) {
158
                if (!grid) {
159
                        return;
160
                }
161

    
162
                g.setColor(Color.lightGray);
163

    
164
                Rectangle2D extent = cta.getExtent();
165
                Point2D auxp = new Point2D.Double(0, 0);
166

    
167
                for (int i = (int) extent.getMinX(); i < (extent.getMaxX());
168
                                i += gridSizeX) {
169
                        for (int j = (int) extent.getMinY(); j < (extent.getMaxY());
170
                                        j += gridSizeY) {
171
                                Point2D po = new Point2D.Double(i, j);
172
                                Point2D point = cta.fromPoint(po); //viewport.fromMapPoint(po);
173
                                double x = ((po.getX() + gridSizeX) % gridSizeX) -
174
                                        ((auxp.getX()) % gridSizeX);
175
                                double y = ((po.getY() + gridSizeY) % gridSizeY) -
176
                                        ((auxp.getY()) % gridSizeY);
177
                                x = (int) (point.getX() - cta.fromDistance(x));
178
                                y = (int) (point.getY() + cta.fromDistance(y));
179

    
180
                                if ((cta.fromDistance(gridSizeX) > 5) ||
181
                                                (cta.fromDistance(gridSizeY) > 5)) {
182
                                        g.drawOval((int) x, (int) y, 1, 1);
183
                                }
184
                        }
185
                }
186
        }
187

    
188
        /**
189
         * Inserta un boolean que indica si se utiliza o no el grid y de esta forma
190
         * dibujarse.
191
         *
192
         * @param b boolean
193
         */
194
        public void setUseGrid(boolean b) {
195
                grid = b;
196
        }
197

    
198
        /**
199
         * Inserta un boolean que indica si se ajusta o no al grid y de esta forma
200
         * dibujarse.
201
         *
202
         * @param b boolean
203
         */
204
        public void setAdjustGrid(boolean b) {
205
                adjustGrid = b;
206
        }
207
}