Statistics
| Revision:

gvsig-raster / org.gvsig.raster.georeferencing / trunk / org.gvsig.raster.georeferencing / org.gvsig.raster.georeferencing.swing / org.gvsig.raster.georeferencing.swing.impl / src / main / java / org / gvsig / raster / georeferencing / swing / impl / tool / ZoomRectangleTool.java @ 1717

History | View | Annotate | Download (5.26 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.georeferencing.swing.impl.tool;
23

    
24
import java.awt.Color;
25
import java.awt.Graphics;
26
import java.awt.event.MouseEvent;
27
import java.awt.event.MouseListener;
28
import java.awt.event.MouseMotionListener;
29
import java.awt.geom.Point2D;
30
import java.awt.geom.Rectangle2D;
31

    
32
import org.gvsig.raster.georeferencing.swing.impl.view.CanvasZone;
33
import org.gvsig.raster.georeferencing.swing.view.ToolEvent;
34
import org.gvsig.raster.georeferencing.swing.view.ToolListener;
35

    
36
/**
37
 * Herramienta de selecci?n de zoom sobre la vista.
38
 * 
39
 * 17/01/2008
40
 * @author Nacho Brodin (nachobrodin@gmail.com)
41
 */
42
public class ZoomRectangleTool extends BaseViewTool implements MouseListener, MouseMotionListener {
43
    private Point2D                  initPoint = null;
44
    private double                   x = 0, y = 0, w = 0, h = 0;
45
    private Rectangle2D              result = null;
46

    
47

    
48
        /**
49
         * Constructor. Asigna el canvas e inicializa los listeners.
50
         * @param canvas
51
         */
52
        public ZoomRectangleTool(CanvasZone canvas, ToolListener listener) {
53
                super(canvas, listener);
54
                canvas.addMouseListener(this);
55
                canvas.addMouseMotionListener(this);
56
        }
57
        
58
        /*
59
         * (non-Javadoc)
60
         * @see org.gvsig.rastertools.georeferencing.ui.zoom.IViewTool#draw(java.awt.image.BufferedImage, java.awt.geom.Rectangle2D)
61
         */
62
        public void draw(Graphics g) {
63
                if(initPoint == null || w == 0 || h == 0)
64
                        return;
65
                g.setColor(Color.RED);
66
                g.drawRect((int)x, (int)y, (int)w, (int)h);
67
        }
68

    
69
        /*
70
         * (non-Javadoc)
71
         * @see org.gvsig.rastertools.georeferencing.ui.zoom.IViewTool#getResult()
72
         */
73
        public Object getResult() {
74
                return result;
75
        }
76

    
77
        /**
78
         * Selecciona el punto inicial del cuadro del que se quiere el zoom
79
         */
80
        public void mousePressed(MouseEvent e) {
81
                if(isActive()) {
82
                        initPoint = e.getPoint();
83
                        //Especificamos un tama?o m?nimo por si no se llega a hacer dragged
84
                        x = initPoint.getX();
85
                        y = initPoint.getY();
86
                        w = 30;
87
                        h = 10;
88
                        result = null;
89
                }
90
        }
91
        
92
        /**
93
         * Asigna el flag que activa y desactiva la herramienta 
94
         * @param active true para activarla y false para desactivarla
95
         */
96
        public void setActive(boolean active) {
97
                this.active = active;
98
                if(active)
99
                        onTool(new ToolEvent(this));
100
                else
101
                        offTool(new ToolEvent(this));
102
        }
103
        
104
        /**
105
         * Dibujado del cuadro con el ?rea a hacer zoom.
106
         */
107
        public void mouseDragged(MouseEvent e) {
108
                if(isActive()) {
109
                        x = initPoint.getX();
110
                        y = initPoint.getY();
111
                        w = Math.abs(e.getX() - x);
112
                        h = Math.abs(e.getY() - y);
113
                        if(e.getX() < x) 
114
                                x = e.getX();
115
                        if(e.getY() < y) 
116
                                y = e.getY();
117
                        Graphics g1 = canvas.getGraphics();
118
                        g1.setColor(Color.RED);
119
                        g1.drawRect((int)x,(int)y, (int)w, (int)h);
120
                }
121
        }
122

    
123
        /*
124
         * (non-Javadoc)
125
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
126
         */
127
        public void mouseReleased(MouseEvent e) {
128
                if(isActive()) {
129
                        
130
                        //Ajuste de la petici?n a la proporci?n del canvas. Esto se hace porque la relaci?n entre
131
                        //el ancho y alto del canvas es distinta a la del cuadro que hemos pintado en pantalla.
132
                        double centerX = x + (w / 2);
133
                        double centerY = y + (h / 2);
134
                        
135
                        if(w < h) {
136
                                if(((double)canvas.getWidth() / (double)canvas.getHeight()) <= (w / h)) {
137
                                        h = (canvas.getHeight() * w) / canvas.getWidth();
138
                                        y = centerY - (h / 2);
139
                                } else {
140
                                        w = (canvas.getWidth() * h) / canvas.getHeight();
141
                                        x = centerX - (w / 2);
142
                                }
143
                        } else { 
144
                                if(((double)canvas.getWidth() / (double)canvas.getHeight()) >= (w / h)) {
145
                                        w = (canvas.getWidth() * h) / canvas.getHeight();
146
                                        x = centerX - (w / 2);
147
                                } else {
148
                                        h = (canvas.getHeight() * w) / canvas.getWidth();
149
                                        y = centerY - (h / 2);
150
                                }
151
                        }
152
                        
153
                        Point2D pInit = canvas.viewCoordsToWorld(new Point2D.Double(x, y));
154
                        Point2D pEnd = canvas.viewCoordsToWorld(new Point2D.Double(x + w, y + h));
155
                        if(canvas.getMinxMaxyUL())
156
                                result = new Rectangle2D.Double(pInit.getX(), pEnd.getY(), Math.abs(pInit.getX() - pEnd.getX()), Math.abs(pInit.getY() - pEnd.getY()));
157
                        else
158
                        result = new Rectangle2D.Double(pInit.getX(), pEnd.getY(), Math.abs(pInit.getX() - pEnd.getX()), Math.abs(pInit.getY() - pEnd.getY()));
159
                        initPoint = null;
160
                        x = y = w = h = 0;
161
                        for (int i = 0; i < listeners.size(); i++) 
162
                                ((ToolListener)listeners.get(i)).endAction(new ToolEvent(this));        
163
                }
164
        }
165

    
166
        public void mouseClicked(MouseEvent e) {
167
        }
168

    
169
        public void mouseEntered(MouseEvent e) {
170
        }
171

    
172
        public void mouseExited(MouseEvent e) {
173
        }
174

    
175
        public void mouseMoved(MouseEvent e) {                        
176
        }
177
}