Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / georeferencing / ui / zoom / tools / ZoomRectangleTool.java @ 18321

History | View | Annotate | Download (4.29 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 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
package org.gvsig.rastertools.georeferencing.ui.zoom.tools;
20

    
21
import java.awt.Color;
22
import java.awt.Graphics;
23
import java.awt.event.MouseEvent;
24
import java.awt.event.MouseListener;
25
import java.awt.event.MouseMotionListener;
26
import java.awt.geom.Point2D;
27
import java.awt.geom.Rectangle2D;
28

    
29
import org.gvsig.rastertools.georeferencing.ui.zoom.CanvasZone;
30

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

    
42

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

    
64
        /*
65
         * (non-Javadoc)
66
         * @see org.gvsig.rastertools.georeferencing.ui.zoom.IViewTool#getResult()
67
         */
68
        public Object getResult() {
69
                return result;
70
        }
71

    
72
        /**
73
         * Selecciona el punto inicial del cuadro del que se quiere el zoom
74
         */
75
        public void mousePressed(MouseEvent e) {
76
                if(selectArea) {
77
                        initPoint = e.getPoint();
78
                        result = null;
79
                }
80
        }
81
        
82
        /**
83
         * Dibujado del cuadro con el ?rea a hacer zoom.
84
         */
85
        public void mouseDragged(MouseEvent e) {
86
                if(selectArea) {
87
                        x = initPoint.getX();
88
                        y = initPoint.getY();
89
                        w = Math.abs(e.getX() - x);
90
                        h = Math.abs(e.getY() - y);
91
                        if(e.getX() < x) 
92
                                x = e.getX();
93
                        if(e.getY() < y) 
94
                                y = e.getY();
95
                        Graphics g1 = canvas.getGraphics();
96
                        g1.setColor(Color.RED);
97
                        g1.drawRect((int)x,(int)y, (int)w, (int)h);
98
                }
99
        }
100

    
101
        /*
102
         * (non-Javadoc)
103
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
104
         */
105
        public void mouseReleased(MouseEvent e) {
106
                if(selectArea) {
107
                        
108
                        //Ajuste de la petici?n a la proporci?n del canvas. Esto se hace porque la relaci?n entre
109
                        //el ancho y alto del canvas es distinta a la del cuadro que hemos pintado en pantalla.
110
                        double centerX = x + (w / 2);
111
                        double centerY = y + (h / 2);
112
                        if(w >= h) { //La X y la W no varian
113
                                h = (canvas.getHeight() * w) / canvas.getWidth();
114
                                y = centerY - (h / 2);
115
                        } else { //La Y y la H no varian
116
                                w = (canvas.getWidth() * h) / canvas.getHeight();
117
                                x = centerX - (w / 2);
118
                        }
119
                        
120
                        Point2D pInit = canvas.viewCoordsToWorld(new Point2D.Double(x, y));
121
                        Point2D pEnd = canvas.viewCoordsToWorld(new Point2D.Double(x + w, y + h));
122
                        if(canvas.getMinxMaxyUL())
123
                                result = new Rectangle2D.Double(pInit.getX(), pEnd.getY(), Math.abs(pInit.getX() - pEnd.getX()), Math.abs(pInit.getY() - pEnd.getY()));
124
                        else
125
                                result = new Rectangle2D.Double(pInit.getX(), pInit.getY(), Math.abs(pInit.getX() - pEnd.getX()), Math.abs(pInit.getY() - pEnd.getY()));
126
                        initPoint = null;
127
                        x = y = w = h = 0;
128
                        listener.endAction(new ToolEvent(this));
129
                }
130
        }
131

    
132
        public void mouseClicked(MouseEvent e) {
133
        }
134

    
135
        public void mouseEntered(MouseEvent e) {
136
        }
137

    
138
        public void mouseExited(MouseEvent e) {
139
        }
140

    
141
        public void mouseMoved(MouseEvent e) {                        
142
        }
143
}