Statistics
| Revision:

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

History | View | Annotate | Download (3.9 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.Graphics;
46
import java.awt.Point;
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 = 1000;
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

    
72
        /**
73
         * Ajusta un punto de la imagen que se pasa como  par?metro al handler m?s
74
         * cercano si se encuentra lo suficientemente  cerca y devuelve la
75
         * distancia del punto original al punto ajustado
76
         *
77
         * @param point
78
         *
79
         * @return Distancia del punto que se pasa como par?metro al punto ajustado
80
         */
81
        public double adjustToGrid(Point point) {
82
                if (grid) {
83
                        Point2D auxp = new Point2D.Double(0, 0);
84
                        Point2D po = viewport.toMapPoint(point);
85
                        Rectangle2D extent = viewport.getAdjustedExtent();
86
                        double x = ((po.getX() + gridSize) % gridSize) -
87
                                ((auxp.getX()) % gridSize);
88
                        double y = ((po.getY() + gridSize) % gridSize) -
89
                                ((auxp.getY()) % gridSize);
90
                        Point2D p = new Point(point);
91
                        point.x = (int) (p.getX() - viewport.fromMapDistance(x));
92
                        point.y = (int) (p.getY() + viewport.fromMapDistance(y));
93

    
94
                        return p.distance(point);
95
                }
96

    
97
                return Double.MAX_VALUE;
98
        }
99

    
100
        /**
101
         * Dibuja el grid sobre el graphics que se pasa como par?metro
102
         *
103
         * @param g Graphics sobre el que dibujar el grid.
104
         */
105
        public void drawGrid(Graphics g) {
106
                if (!grid) {
107
                        return;
108
                }
109

    
110
                Rectangle2D extent = viewport.getAdjustedExtent();
111
                Point2D auxp = new Point2D.Double(0, 0);
112

    
113
                for (int i = (int) extent.getMinX(); i < (extent.getMaxX() + gridSize);
114
                                i += gridSize) {
115
                        for (int j = (int) extent.getMinY();
116
                                        j < (extent.getMaxY() + gridSize); j += gridSize) {
117
                                Point2D po = new Point2D.Double(i, j);
118
                                Point2D point = viewport.fromMapPoint(po);
119
                                double x = ((po.getX() + gridSize) % gridSize) -
120
                                        ((auxp.getX()) % gridSize);
121
                                double y = ((po.getY() + gridSize) % gridSize) -
122
                                        ((auxp.getY()) % gridSize);
123
                                x = (int) (point.getX() - viewport.fromMapDistance(x));
124
                                y = (int) (point.getY() + viewport.fromMapDistance(y));
125
                                g.drawOval((int) x, (int) y, 1, 1);
126
                        }
127
                }
128
        }
129

    
130
        /**
131
         * Inserta un boolean que indica si se utiliza o no el grid y de esta forma
132
         * dibujarse.
133
         *
134
         * @param b boolean
135
         */
136
        public void setUseGrid(boolean b) {
137
                grid = b;
138
        }
139

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