Statistics
| Revision:

svn-gvsig-desktop / branches / FMap_piloto_CAD_Layout_version / libraries / libFMap / src / com / iver / cit / gvsig / fmap / CadGrid.java @ 1762

History | View | Annotate | Download (4.31 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.fmap;
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 int gridSize = 0;
60
        private ViewPort viewport;
61
        private boolean adjustGrid;
62

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

    
71
                if (gridSize == 0) {
72
                        gridSize = (int) viewport.toMapDistance(20);
73
                }
74
        }
75

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

    
105
        /**
106
         * Dibuja el grid sobre el graphics que se pasa como par?metro
107
         *
108
         * @param g Graphics sobre el que dibujar el grid.
109
         */
110
        public void drawGrid(Graphics g) {
111
                if (!grid) {
112
                        return;
113
                }
114

    
115
                g.setColor(Color.lightGray);
116

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

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

    
133
                                if (viewport.fromMapDistance(gridSize) > 3) {
134
                                        g.drawRect((int) x, (int) y, 1, 1);
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 setUseGrid(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 getUseGrid() {
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 getAdjustGrig() {
175
                return adjustGrid;
176
        }
177
}