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 @ 40556
History | View | Annotate | Download (6.94 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 |
|
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 ((m_FirstPoint == null) || (m_LastPoint == null)) |
150 |
return;
|
151 |
|
152 |
Point2D p1;
|
153 |
Point2D p2;
|
154 |
Point pScreen = e.getPoint();
|
155 |
|
156 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
157 |
|
158 |
p1 = vp.toMapPoint(m_FirstPoint); |
159 |
p2 = vp.toMapPoint(pScreen); |
160 |
|
161 |
// Fijamos el nuevo extent
|
162 |
Rectangle2D.Double r = new Rectangle2D.Double(); |
163 |
r.setFrameFromDiagonal(p1, p2); |
164 |
|
165 |
Rectangle2D rectPixel = new Rectangle(); |
166 |
rectPixel.setFrameFromDiagonal(m_FirstPoint, pScreen); |
167 |
|
168 |
Double[] x = new Double[2]; |
169 |
Double[] y = new Double[2]; |
170 |
x[0] = new Double(p1.getX()); |
171 |
x[1] = new Double(p2.getX()); |
172 |
y[0] = new Double(p1.getY()); |
173 |
y[1] = new Double(p2.getY()); |
174 |
MeasureEvent event = new MeasureEvent(x, y, e);
|
175 |
listener.circle(event); |
176 |
getMapControl().repaint(); |
177 |
|
178 |
m_FirstPoint = null;
|
179 |
m_LastPoint = null;
|
180 |
isClicked = false;
|
181 |
} |
182 |
|
183 |
/*
|
184 |
* (non-Javadoc)
|
185 |
*
|
186 |
* @see
|
187 |
* com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseDragged(java.awt
|
188 |
* .event.MouseEvent)
|
189 |
*/
|
190 |
public void mouseDragged(MouseEvent e) throws BehaviorException { |
191 |
mouseMoved(e); |
192 |
} |
193 |
|
194 |
/*
|
195 |
* (non-Javadoc)
|
196 |
*
|
197 |
* @see
|
198 |
* com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseMoved(java.awt.event
|
199 |
* .MouseEvent)
|
200 |
*/
|
201 |
public void mouseMoved(MouseEvent e) throws BehaviorException { |
202 |
if (!isClicked)
|
203 |
return;
|
204 |
|
205 |
m_LastPoint = e.getPoint(); |
206 |
|
207 |
if (m_FirstPoint == null) |
208 |
return;
|
209 |
Point2D p1;
|
210 |
Point2D p2;
|
211 |
Point pScreen = e.getPoint();
|
212 |
|
213 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
214 |
|
215 |
p1 = vp.toMapPoint(m_FirstPoint); |
216 |
p2 = vp.toMapPoint(pScreen); |
217 |
|
218 |
// Fijamos el nuevo extent
|
219 |
Rectangle2D.Double r = new Rectangle2D.Double(); |
220 |
r.setFrameFromDiagonal(p1, p2); |
221 |
|
222 |
Rectangle2D rectPixel = new Rectangle(); |
223 |
rectPixel.setFrameFromDiagonal(m_FirstPoint, pScreen); |
224 |
|
225 |
Double[] x = new Double[2]; |
226 |
Double[] y = new Double[2]; |
227 |
x[0] = new Double(p1.getX()); |
228 |
x[1] = new Double(p2.getX()); |
229 |
y[0] = new Double(p1.getY()); |
230 |
y[1] = new Double(p2.getY()); |
231 |
MeasureEvent event = new MeasureEvent(x, y, e);
|
232 |
listener.circle(event); |
233 |
getMapControl().repaint(); |
234 |
} |
235 |
} |