svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.selectiontools.app / org.gvsig.selectiontools.app.mainplugin / src / main / java / org / gvsig / selectiontools / app / extension / tools / behavior / CircleSelectionBehavior.java @ 42036
History | View | Annotate | Download (6.96 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.selectiontools.app.extension.tools.behavior; |
25 |
|
26 |
/* gvSIG. Geographic Information System of the Valencian Government
|
27 |
*
|
28 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
29 |
* of the Valencian Government (CIT)
|
30 |
*
|
31 |
* This program is free software; you can redistribute it and/or
|
32 |
* modify it under the terms of the GNU General Public License
|
33 |
* as published by the Free Software Foundation; either version 2
|
34 |
* of the License, or (at your option) any later version.
|
35 |
*
|
36 |
* This program is distributed in the hope that it will be useful,
|
37 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
38 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
39 |
* GNU General Public License for more details.
|
40 |
*
|
41 |
* You should have received a copy of the GNU General Public License
|
42 |
* along with this program; if not, write to the Free Software
|
43 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
44 |
* MA 02110-1301, USA.
|
45 |
*
|
46 |
*/
|
47 |
|
48 |
import java.awt.Color; |
49 |
import java.awt.Graphics; |
50 |
import java.awt.Graphics2D; |
51 |
import java.awt.Point; |
52 |
import java.awt.Rectangle; |
53 |
import java.awt.event.MouseEvent; |
54 |
import java.awt.geom.Arc2D; |
55 |
import java.awt.geom.Point2D; |
56 |
import java.awt.geom.Rectangle2D; |
57 |
import java.awt.image.BufferedImage; |
58 |
|
59 |
import org.gvsig.fmap.mapcontext.ViewPort; |
60 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
61 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.CircleBehavior; |
62 |
import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent; |
63 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.CircleListener; |
64 |
|
65 |
/**
|
66 |
*
|
67 |
*
|
68 |
* @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
|
69 |
*/
|
70 |
public class CircleSelectionBehavior extends CircleBehavior { |
71 |
|
72 |
/**
|
73 |
*
|
74 |
*/
|
75 |
private CircleListener listener;
|
76 |
|
77 |
/**
|
78 |
* Auxiliary point that represents a corner selected in image coordinates.
|
79 |
*/
|
80 |
private Point2D m_PointAnt; |
81 |
|
82 |
public CircleSelectionBehavior(CircleListener zili) {
|
83 |
super(zili);
|
84 |
|
85 |
listener = zili; |
86 |
} |
87 |
|
88 |
/*
|
89 |
* (non-Javadoc)
|
90 |
*
|
91 |
* @see
|
92 |
* com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt
|
93 |
* .Graphics)
|
94 |
*/
|
95 |
public void paintComponent(Graphics g) { |
96 |
double radio;
|
97 |
g.setColor(Color.black);
|
98 |
g.setXORMode(Color.white);
|
99 |
if ((m_FirstPoint != null) && (m_LastPoint != null)) { |
100 |
radio = m_LastPoint.distance(m_FirstPoint); |
101 |
Arc2D.Double arc =
|
102 |
new Arc2D.Double(m_FirstPoint.getX() - radio, |
103 |
m_FirstPoint.getY() - radio, |
104 |
2 * radio,
|
105 |
2 * radio,
|
106 |
0,
|
107 |
360,
|
108 |
Arc2D.OPEN);
|
109 |
|
110 |
((Graphics2D) g).draw(arc);
|
111 |
} |
112 |
} |
113 |
|
114 |
/*
|
115 |
* (non-Javadoc)
|
116 |
*
|
117 |
* @see
|
118 |
* com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mousePressed(java.awt
|
119 |
* .event.MouseEvent)
|
120 |
*/
|
121 |
public void mousePressed(MouseEvent e) { |
122 |
if( !this.isMyButton(e) ) { |
123 |
return;
|
124 |
} |
125 |
Point pScreen = e.getPoint();
|
126 |
m_PointAnt = pScreen; |
127 |
|
128 |
if ((!isClicked) && (e.getButton() == MouseEvent.BUTTON1)) { |
129 |
m_PointAnt = pScreen; |
130 |
m_FirstPoint = m_PointAnt; |
131 |
isClicked = true;
|
132 |
} |
133 |
|
134 |
if (listener.cancelDrawing()) {
|
135 |
getMapControl().cancelDrawing(); |
136 |
isClicked = false;
|
137 |
} |
138 |
getMapControl().repaint(); |
139 |
} |
140 |
|
141 |
/*
|
142 |
* (non-Javadoc)
|
143 |
*
|
144 |
* @see
|
145 |
* com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseReleased(java.awt
|
146 |
* .event.MouseEvent)
|
147 |
*/
|
148 |
public void mouseReleased(MouseEvent e) throws BehaviorException { |
149 |
if( !this.isMyButton(e) ) { |
150 |
return;
|
151 |
} |
152 |
if ((m_FirstPoint == null) || (m_LastPoint == null)) |
153 |
return;
|
154 |
|
155 |
Point2D p1;
|
156 |
Point2D p2;
|
157 |
Point pScreen = e.getPoint();
|
158 |
|
159 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
160 |
|
161 |
p1 = vp.toMapPoint(m_FirstPoint); |
162 |
p2 = vp.toMapPoint(pScreen); |
163 |
|
164 |
// Fijamos el nuevo extent
|
165 |
Rectangle2D.Double r = new Rectangle2D.Double(); |
166 |
r.setFrameFromDiagonal(p1, p2); |
167 |
|
168 |
Rectangle2D rectPixel = new Rectangle(); |
169 |
rectPixel.setFrameFromDiagonal(m_FirstPoint, pScreen); |
170 |
|
171 |
Double[] x = new Double[2]; |
172 |
Double[] y = new Double[2]; |
173 |
x[0] = new Double(p1.getX()); |
174 |
x[1] = new Double(p2.getX()); |
175 |
y[0] = new Double(p1.getY()); |
176 |
y[1] = new Double(p2.getY()); |
177 |
MeasureEvent event = new MeasureEvent(x, y, e);
|
178 |
listener.circle(event); |
179 |
getMapControl().repaint(); |
180 |
|
181 |
m_FirstPoint = null;
|
182 |
m_LastPoint = null;
|
183 |
isClicked = false;
|
184 |
} |
185 |
|
186 |
/*
|
187 |
* (non-Javadoc)
|
188 |
*
|
189 |
* @see
|
190 |
* com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseDragged(java.awt
|
191 |
* .event.MouseEvent)
|
192 |
*/
|
193 |
public void mouseDragged(MouseEvent e) throws BehaviorException { |
194 |
mouseMoved(e); |
195 |
} |
196 |
|
197 |
/*
|
198 |
* (non-Javadoc)
|
199 |
*
|
200 |
* @see
|
201 |
* com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseMoved(java.awt.event
|
202 |
* .MouseEvent)
|
203 |
*/
|
204 |
public void mouseMoved(MouseEvent e) throws BehaviorException { |
205 |
if (!isClicked)
|
206 |
return;
|
207 |
|
208 |
m_LastPoint = e.getPoint(); |
209 |
|
210 |
if (m_FirstPoint == null) |
211 |
return;
|
212 |
Point2D p1;
|
213 |
Point2D p2;
|
214 |
Point pScreen = e.getPoint();
|
215 |
|
216 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
217 |
|
218 |
p1 = vp.toMapPoint(m_FirstPoint); |
219 |
p2 = vp.toMapPoint(pScreen); |
220 |
|
221 |
// Fijamos el nuevo extent
|
222 |
Rectangle2D.Double r = new Rectangle2D.Double(); |
223 |
r.setFrameFromDiagonal(p1, p2); |
224 |
|
225 |
Rectangle2D rectPixel = new Rectangle(); |
226 |
rectPixel.setFrameFromDiagonal(m_FirstPoint, pScreen); |
227 |
|
228 |
Double[] x = new Double[2]; |
229 |
Double[] y = new Double[2]; |
230 |
x[0] = new Double(p1.getX()); |
231 |
x[1] = new Double(p2.getX()); |
232 |
y[0] = new Double(p1.getY()); |
233 |
y[1] = new Double(p2.getY()); |
234 |
MeasureEvent event = new MeasureEvent(x, y, e);
|
235 |
listener.circle(event); |
236 |
getMapControl().repaint(); |
237 |
} |
238 |
} |