root / branches / pilotoDWG / applications / appgvSIG / src / com / iver / cit / gvsig / gui / cad / CadGrid.java @ 1639
History | View | Annotate | Download (4.01 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 |
gridSize=(int)viewport.toMapDistance(20); |
71 |
} |
72 |
|
73 |
/**
|
74 |
* Ajusta un punto de la imagen que se pasa como par?metro al handler m?s
|
75 |
* cercano si se encuentra lo suficientemente cerca y devuelve la
|
76 |
* distancia del punto original al punto ajustado
|
77 |
*
|
78 |
* @param point
|
79 |
*
|
80 |
* @return Distancia del punto que se pasa como par?metro al punto ajustado
|
81 |
*/
|
82 |
public double adjustToGrid(Point point) { |
83 |
if (grid && adjustGrid) {
|
84 |
Point2D auxp = new Point2D.Double(0, 0); |
85 |
Point2D po = viewport.toMapPoint(point);
|
86 |
Rectangle2D extent = viewport.getAdjustedExtent();
|
87 |
double x = ((po.getX() + gridSize) % gridSize) -
|
88 |
((auxp.getX()) % gridSize); |
89 |
double y = ((po.getY() + gridSize) % gridSize) -
|
90 |
((auxp.getY()) % gridSize); |
91 |
Point2D p = new Point(point); |
92 |
point.x = (int) (p.getX() - viewport.fromMapDistance(x));
|
93 |
point.y = (int) (p.getY() + viewport.fromMapDistance(y));
|
94 |
|
95 |
return p.distance(point);
|
96 |
} |
97 |
|
98 |
return Double.MAX_VALUE; |
99 |
} |
100 |
|
101 |
/**
|
102 |
* Dibuja el grid sobre el graphics que se pasa como par?metro
|
103 |
*
|
104 |
* @param g Graphics sobre el que dibujar el grid.
|
105 |
*/
|
106 |
public void drawGrid(Graphics g) { |
107 |
if (!grid) {
|
108 |
return;
|
109 |
} |
110 |
|
111 |
Rectangle2D extent = viewport.getAdjustedExtent();
|
112 |
Point2D auxp = new Point2D.Double(0, 0); |
113 |
|
114 |
for (int i = (int) extent.getMinX(); i < (extent.getMaxX() + gridSize); |
115 |
i += gridSize) { |
116 |
for (int j = (int) extent.getMinY(); |
117 |
j < (extent.getMaxY() + gridSize); j += gridSize) { |
118 |
Point2D po = new Point2D.Double(i, j); |
119 |
Point2D point = viewport.fromMapPoint(po);
|
120 |
double x = ((po.getX() + gridSize) % gridSize) -
|
121 |
((auxp.getX()) % gridSize); |
122 |
double y = ((po.getY() + gridSize) % gridSize) -
|
123 |
((auxp.getY()) % gridSize); |
124 |
x = (int) (point.getX() - viewport.fromMapDistance(x));
|
125 |
y = (int) (point.getY() + viewport.fromMapDistance(y));
|
126 |
if (viewport.fromMapDistance(gridSize)>3){ |
127 |
g.drawOval((int) x, (int) y, 1, 1); |
128 |
} |
129 |
} |
130 |
} |
131 |
} |
132 |
|
133 |
/**
|
134 |
* Inserta un boolean que indica si se utiliza o no el grid y de esta forma
|
135 |
* dibujarse.
|
136 |
*
|
137 |
* @param b boolean
|
138 |
*/
|
139 |
public void setUseGrid(boolean b) { |
140 |
grid = b; |
141 |
} |
142 |
|
143 |
/**
|
144 |
* Inserta un boolean que indica si se ajusta o no al grid y de esta forma
|
145 |
* dibujarse.
|
146 |
*
|
147 |
* @param b boolean
|
148 |
*/
|
149 |
public void setAdjustGrid(boolean b) { |
150 |
adjustGrid = b; |
151 |
} |
152 |
} |