Statistics
| Revision:

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

History | View | Annotate | Download (4.04 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;
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
/**
30
 * Herramienta de selecci?n de zoom sobre la vista.
31
 * 
32
 * 17/01/2008
33
 * @author Nacho Brodin (nachobrodin@gmail.com)
34
 */
35
public class ZoomRectangleTool extends BaseViewTool implements MouseListener, MouseMotionListener {
36
    private Point2D                  initPoint = null;
37
    private double                   x = 0, y = 0, w = 0, h = 0;
38
    private Rectangle2D              result = null;
39

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

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

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

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

    
126
        public void mouseClicked(MouseEvent e) {
127
        }
128

    
129
        public void mouseEntered(MouseEvent e) {
130
        }
131

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

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