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 @ 41976
History | View | Annotate | Download (7.07 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 |
BufferedImage img = getMapControl().getImage();
|
98 |
g.drawImage(img, 0, 0, null); |
99 |
g.setColor(Color.black);
|
100 |
g.setXORMode(Color.white);
|
101 |
if ((m_FirstPoint != null) && (m_LastPoint != null)) { |
102 |
radio = m_LastPoint.distance(m_FirstPoint); |
103 |
Arc2D.Double arc =
|
104 |
new Arc2D.Double(m_FirstPoint.getX() - radio, |
105 |
m_FirstPoint.getY() - radio, |
106 |
2 * radio,
|
107 |
2 * radio,
|
108 |
0,
|
109 |
360,
|
110 |
Arc2D.OPEN);
|
111 |
|
112 |
((Graphics2D) g).draw(arc);
|
113 |
} |
114 |
} |
115 |
|
116 |
/*
|
117 |
* (non-Javadoc)
|
118 |
*
|
119 |
* @see
|
120 |
* com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mousePressed(java.awt
|
121 |
* .event.MouseEvent)
|
122 |
*/
|
123 |
public void mousePressed(MouseEvent e) { |
124 |
if( !this.isMyButton(e) ) { |
125 |
return;
|
126 |
} |
127 |
Point pScreen = e.getPoint();
|
128 |
m_PointAnt = pScreen; |
129 |
|
130 |
if ((!isClicked) && (e.getButton() == MouseEvent.BUTTON1)) { |
131 |
m_PointAnt = pScreen; |
132 |
m_FirstPoint = m_PointAnt; |
133 |
isClicked = true;
|
134 |
} |
135 |
|
136 |
if (listener.cancelDrawing()) {
|
137 |
getMapControl().cancelDrawing(); |
138 |
isClicked = false;
|
139 |
} |
140 |
getMapControl().repaint(); |
141 |
} |
142 |
|
143 |
/*
|
144 |
* (non-Javadoc)
|
145 |
*
|
146 |
* @see
|
147 |
* com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseReleased(java.awt
|
148 |
* .event.MouseEvent)
|
149 |
*/
|
150 |
public void mouseReleased(MouseEvent e) throws BehaviorException { |
151 |
if( !this.isMyButton(e) ) { |
152 |
return;
|
153 |
} |
154 |
if ((m_FirstPoint == null) || (m_LastPoint == null)) |
155 |
return;
|
156 |
|
157 |
Point2D p1;
|
158 |
Point2D p2;
|
159 |
Point pScreen = e.getPoint();
|
160 |
|
161 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
162 |
|
163 |
p1 = vp.toMapPoint(m_FirstPoint); |
164 |
p2 = vp.toMapPoint(pScreen); |
165 |
|
166 |
// Fijamos el nuevo extent
|
167 |
Rectangle2D.Double r = new Rectangle2D.Double(); |
168 |
r.setFrameFromDiagonal(p1, p2); |
169 |
|
170 |
Rectangle2D rectPixel = new Rectangle(); |
171 |
rectPixel.setFrameFromDiagonal(m_FirstPoint, pScreen); |
172 |
|
173 |
Double[] x = new Double[2]; |
174 |
Double[] y = new Double[2]; |
175 |
x[0] = new Double(p1.getX()); |
176 |
x[1] = new Double(p2.getX()); |
177 |
y[0] = new Double(p1.getY()); |
178 |
y[1] = new Double(p2.getY()); |
179 |
MeasureEvent event = new MeasureEvent(x, y, e);
|
180 |
listener.circle(event); |
181 |
getMapControl().repaint(); |
182 |
|
183 |
m_FirstPoint = null;
|
184 |
m_LastPoint = null;
|
185 |
isClicked = false;
|
186 |
} |
187 |
|
188 |
/*
|
189 |
* (non-Javadoc)
|
190 |
*
|
191 |
* @see
|
192 |
* com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseDragged(java.awt
|
193 |
* .event.MouseEvent)
|
194 |
*/
|
195 |
public void mouseDragged(MouseEvent e) throws BehaviorException { |
196 |
mouseMoved(e); |
197 |
} |
198 |
|
199 |
/*
|
200 |
* (non-Javadoc)
|
201 |
*
|
202 |
* @see
|
203 |
* com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseMoved(java.awt.event
|
204 |
* .MouseEvent)
|
205 |
*/
|
206 |
public void mouseMoved(MouseEvent e) throws BehaviorException { |
207 |
if (!isClicked)
|
208 |
return;
|
209 |
|
210 |
m_LastPoint = e.getPoint(); |
211 |
|
212 |
if (m_FirstPoint == null) |
213 |
return;
|
214 |
Point2D p1;
|
215 |
Point2D p2;
|
216 |
Point pScreen = e.getPoint();
|
217 |
|
218 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
219 |
|
220 |
p1 = vp.toMapPoint(m_FirstPoint); |
221 |
p2 = vp.toMapPoint(pScreen); |
222 |
|
223 |
// Fijamos el nuevo extent
|
224 |
Rectangle2D.Double r = new Rectangle2D.Double(); |
225 |
r.setFrameFromDiagonal(p1, p2); |
226 |
|
227 |
Rectangle2D rectPixel = new Rectangle(); |
228 |
rectPixel.setFrameFromDiagonal(m_FirstPoint, pScreen); |
229 |
|
230 |
Double[] x = new Double[2]; |
231 |
Double[] y = new Double[2]; |
232 |
x[0] = new Double(p1.getX()); |
233 |
x[1] = new Double(p2.getX()); |
234 |
y[0] = new Double(p1.getY()); |
235 |
y[1] = new Double(p2.getY()); |
236 |
MeasureEvent event = new MeasureEvent(x, y, e);
|
237 |
listener.circle(event); |
238 |
getMapControl().repaint(); |
239 |
} |
240 |
} |