svn-gvsig-desktop / branches / v10 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / tools / Behavior / RectangleBehavior.java @ 20100
History | View | Annotate | Download (5.4 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.fmap.tools.Behavior; |
42 |
|
43 |
import com.iver.cit.gvsig.fmap.ViewPort; |
44 |
import com.iver.cit.gvsig.fmap.tools.BehaviorException; |
45 |
import com.iver.cit.gvsig.fmap.tools.Events.RectangleEvent; |
46 |
import com.iver.cit.gvsig.fmap.tools.Listeners.RectangleListener; |
47 |
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener; |
48 |
|
49 |
import java.awt.Color; |
50 |
import java.awt.Graphics; |
51 |
import java.awt.Point; |
52 |
import java.awt.Rectangle; |
53 |
import java.awt.event.MouseEvent; |
54 |
import java.awt.geom.Point2D; |
55 |
import java.awt.geom.Rectangle2D; |
56 |
import java.awt.image.BufferedImage; |
57 |
|
58 |
|
59 |
/**
|
60 |
* <p>Behavior that permits user to select a rectangular area on the associated <code>MapControl</code> using
|
61 |
* a {@link RectangleListener RectangleListener}.</p>
|
62 |
*
|
63 |
* @author Vicente Caballero Navarro
|
64 |
*/
|
65 |
public class RectangleBehavior extends Behavior { |
66 |
/**
|
67 |
* First point of the rectangle area, that represents a corner.
|
68 |
*/
|
69 |
private Point2D m_FirstPoint; |
70 |
|
71 |
/**
|
72 |
* Second point of the rectangle area, that represents the opposite corner of the first,
|
73 |
* along the diagonal.
|
74 |
*/
|
75 |
private Point2D m_LastPoint; |
76 |
|
77 |
/**
|
78 |
* Auxiliary point that represents a corner selected in image coordinates.
|
79 |
*/
|
80 |
private Point2D m_PointAnt; |
81 |
|
82 |
/**
|
83 |
* Tool listener used to work with the <code>MapControl</code> object.
|
84 |
*
|
85 |
* @see #getListener()
|
86 |
* @see #setListener(ToolListener)
|
87 |
*/
|
88 |
private RectangleListener listener;
|
89 |
|
90 |
/**
|
91 |
* <p>Creates a new behavior for selecting rectangle areas.</p>
|
92 |
*
|
93 |
* @param zili listener used to permit this object to work with the associated <code>MapControl</code>
|
94 |
*/
|
95 |
public RectangleBehavior(RectangleListener zili) {
|
96 |
listener = zili; |
97 |
} |
98 |
|
99 |
/*
|
100 |
* (non-Javadoc)
|
101 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
|
102 |
*/
|
103 |
public void paintComponent(Graphics g) { |
104 |
///g.setColor(Color.white);
|
105 |
///g.fillRect(0, 0, getMapControl().getWidth(), getMapControl().getHeight());
|
106 |
BufferedImage img = getMapControl().getImage();
|
107 |
g.drawImage(img, 0, 0, null); |
108 |
g.setColor(Color.black);
|
109 |
g.setXORMode(Color.white);
|
110 |
|
111 |
// Borramos el anterior
|
112 |
Rectangle r = new Rectangle(); |
113 |
|
114 |
// Dibujamos el actual
|
115 |
if ((m_FirstPoint != null) && (m_LastPoint != null)) { |
116 |
r.setFrameFromDiagonal(m_FirstPoint, m_LastPoint); |
117 |
g.drawRect(r.x, r.y, r.width, r.height); |
118 |
} |
119 |
g.setPaintMode(); |
120 |
} |
121 |
|
122 |
/*
|
123 |
* (non-Javadoc)
|
124 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mousePressed(java.awt.event.MouseEvent)
|
125 |
*/
|
126 |
public void mousePressed(MouseEvent e) { |
127 |
Point pScreen = e.getPoint();
|
128 |
m_PointAnt = pScreen; |
129 |
m_FirstPoint = m_PointAnt; |
130 |
|
131 |
if (e.getButton() == MouseEvent.BUTTON1) { |
132 |
m_PointAnt = pScreen; |
133 |
m_FirstPoint = m_PointAnt; |
134 |
} |
135 |
|
136 |
if (listener.cancelDrawing()) {
|
137 |
getMapControl().cancelDrawing(); |
138 |
} |
139 |
|
140 |
getMapControl().repaint(); |
141 |
} |
142 |
|
143 |
/*
|
144 |
* (non-Javadoc)
|
145 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseReleased(java.awt.event.MouseEvent)
|
146 |
*/
|
147 |
public void mouseReleased(MouseEvent e) throws BehaviorException { |
148 |
if (m_FirstPoint == null) return; |
149 |
Point2D p1;
|
150 |
Point2D p2;
|
151 |
Point pScreen = e.getPoint();
|
152 |
|
153 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
154 |
|
155 |
p1 = vp.toMapPoint(m_FirstPoint); |
156 |
p2 = vp.toMapPoint(pScreen); |
157 |
|
158 |
if (e.getButton() == MouseEvent.BUTTON1) { |
159 |
// Fijamos el nuevo extent
|
160 |
Rectangle2D.Double r = new Rectangle2D.Double(); |
161 |
r.setFrameFromDiagonal(p1, p2); |
162 |
|
163 |
Rectangle2D rectPixel = new Rectangle(); |
164 |
rectPixel.setFrameFromDiagonal(m_FirstPoint, pScreen); |
165 |
|
166 |
RectangleEvent event = new RectangleEvent(r, e, rectPixel);
|
167 |
listener.rectangle(event); |
168 |
} |
169 |
|
170 |
m_FirstPoint = null;
|
171 |
m_LastPoint = null;
|
172 |
} |
173 |
|
174 |
/*
|
175 |
* (non-Javadoc)
|
176 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseDragged(java.awt.event.MouseEvent)
|
177 |
*/
|
178 |
public void mouseDragged(MouseEvent e) { |
179 |
m_LastPoint = e.getPoint(); |
180 |
getMapControl().repaint(); |
181 |
} |
182 |
|
183 |
/**
|
184 |
* <p>Sets a tool listener to work with the <code>MapControl</code> using this behavior.</p>
|
185 |
*
|
186 |
* @param listener a <code>RectangleListener</code> object for this behavior
|
187 |
*/
|
188 |
public void setListener(ToolListener listener) { |
189 |
this.listener = (RectangleListener) listener;
|
190 |
} |
191 |
|
192 |
/*
|
193 |
* (non-Javadoc)
|
194 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
|
195 |
*/
|
196 |
public ToolListener getListener() {
|
197 |
return listener;
|
198 |
} |
199 |
} |