Statistics
| Revision:

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

History | View | Annotate | Download (4.65 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
import com.iver.cit.gvsig.gui.layout.LayoutCADToolAdapter;
45

    
46
import java.awt.Graphics;
47
import java.awt.Point;
48
import java.awt.geom.AffineTransform;
49
import java.awt.geom.Point2D;
50
import java.awt.geom.Rectangle2D;
51

    
52

    
53
/**
54
 * Clase encargada de gestionar las diferentes operaciones que se realizan
55
 * sobre el grid.
56
 *
57
 * @author Vicente Caballero Navarro
58
 */
59
public class CadGrid {
60
        private boolean grid = true;
61
        private double gridSizeX=1;
62
        private double gridSizeY=1;
63
        private AffineTransform at;
64
        private boolean adjustGrid;
65
        private CADToolAdapter cta;
66
        
67
        public CadGrid(CADToolAdapter cta){
68
                this.cta=cta;
69
        }
70
        /**
71
         * Inserta el viewPort.
72
         *
73
         * @param vp
74
         */
75
        public void setAT(AffineTransform at) {
76
                this.at = at;
77
        }
78
        public void setGridSizeX(double d){
79
                gridSizeX=d;//(int)cta.fromDistance(d);
80
        }
81
        public void setGridSizeY(double d){
82
                gridSizeY=d;//(int)cta.fromDistance(d);
83
        }
84
        public double getGridSizeX(){
85
                return gridSizeX;
86
        }
87
        public double getGridSizeY(){
88
                return gridSizeY;
89
        }
90
        /**
91
         * Ajusta un punto de la imagen que se pasa como  par?metro al handler m?s
92
         * cercano si se encuentra lo suficientemente  cerca y devuelve la
93
         * distancia del punto original al punto ajustado
94
         *
95
         * @param point
96
         *
97
         * @return Distancia del punto que se pasa como par?metro al punto ajustado
98
         */
99
        public double adjustToGrid(Point point) {
100
                if (grid && adjustGrid) {
101
                        Point2D auxp = new Point2D.Double(0, 0);
102
                        Point2D po = cta.toPoint(point);//viewport.toMapPoint(point);
103
                        Rectangle2D extent = cta.getExtent();//viewport.getAdjustedExtent();
104
                        double x = ((po.getX() + gridSizeX) % gridSizeX) -
105
                                ((auxp.getX()) % gridSizeX);
106
                        double y = ((po.getY() + gridSizeY) % gridSizeY) -
107
                                ((auxp.getY()) % gridSizeY);
108
                        Point2D p = new Point(point);
109
                        point.x = (int) (p.getX() - cta.fromDistance(x));//viewport.fromMapDistance(x));
110
                        if (cta instanceof LayoutCADToolAdapter){
111
                                point.y = (int) (p.getY() - cta.fromDistance(y));//viewport.fromMapDistance(y));
112
                        }else{
113
                                point.y = (int) (p.getY() + cta.fromDistance(y));
114
                        }
115
                        
116
                        return p.distance(point);
117
                }
118

    
119
                return Double.MAX_VALUE;
120
        }
121

    
122
        /**
123
         * Dibuja el grid sobre el graphics que se pasa como par?metro
124
         *
125
         * @param g Graphics sobre el que dibujar el grid.
126
         */
127
        public void drawGrid(Graphics g) {
128
                if (!grid) {
129
                        return;
130
                }
131

    
132
                Rectangle2D extent = cta.getExtent();
133
                Point2D auxp = new Point2D.Double(0, 0);
134

    
135
                for (int i = (int) extent.getMinX(); i < (extent.getMaxX());
136
                                i += gridSizeX) {
137
                        for (int j = (int) extent.getMinY();
138
                                        j < (extent.getMaxY() ); j += gridSizeY) {
139
                                Point2D po = new Point2D.Double(i, j);
140
                                Point2D point = cta.fromPoint(po);//viewport.fromMapPoint(po);
141
                                double x = ((po.getX() + gridSizeX) % gridSizeX) -
142
                                        ((auxp.getX()) % gridSizeX);
143
                                double y = ((po.getY() + gridSizeY) % gridSizeY) -
144
                                        ((auxp.getY()) % gridSizeY);
145
                                x = (int) (point.getX() - cta.fromDistance(x));
146
                                y = (int) (point.getY() + cta.fromDistance(y));
147
                                if (cta.fromDistance(gridSizeX)>5 && cta.fromDistance(gridSizeY)>5){
148
                                        g.drawOval((int) x, (int) y, 1, 1);
149
                                }
150
                        }
151
                }
152
        }
153

    
154
        /**
155
         * Inserta un boolean que indica si se utiliza o no el grid y de esta forma
156
         * dibujarse.
157
         *
158
         * @param b boolean
159
         */
160
        public void setUseGrid(boolean b) {
161
                grid = b;
162
        }
163

    
164
        /**
165
         * Inserta un boolean que indica si se ajusta o no al grid y de esta forma
166
         * dibujarse.
167
         *
168
         * @param b boolean
169
         */
170
        public void setAdjustGrid(boolean b) {
171
                adjustGrid = b;
172
        }
173
}