svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / tools / Behavior / CircleBehavior.java @ 41977
History | View | Annotate | Download (6.16 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.fmap.mapcontrol.tools.Behavior; |
25 |
|
26 |
import java.awt.Color; |
27 |
import java.awt.Rectangle; |
28 |
import java.awt.event.MouseEvent; |
29 |
import java.awt.geom.Point2D; |
30 |
import java.awt.geom.Rectangle2D; |
31 |
import java.awt.image.BufferedImage; |
32 |
|
33 |
import org.gvsig.fmap.geom.primitive.Arc; |
34 |
import org.gvsig.fmap.mapcontext.ViewPort; |
35 |
import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
36 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
37 |
import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent; |
38 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.CircleListener; |
39 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.PolylineListener; |
40 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener; |
41 |
import org.slf4j.Logger; |
42 |
import org.slf4j.LoggerFactory; |
43 |
|
44 |
|
45 |
|
46 |
/**
|
47 |
* <p>Behavior that allows user to draw a circle on the image of the associated
|
48 |
* <code>MapControl</code> using a {@link PolylineListener PolylineListener}.</p>
|
49 |
*
|
50 |
* @author Laura
|
51 |
* @author Pablo Piqueras Bartolom?
|
52 |
*/
|
53 |
public class CircleBehavior extends Behavior { |
54 |
private static final Logger logger = LoggerFactory.getLogger(CircleBehavior.class); |
55 |
|
56 |
/**
|
57 |
* First point set, that represents the center of the circle.
|
58 |
*/
|
59 |
protected Point2D m_FirstPoint; |
60 |
|
61 |
/**
|
62 |
* Second point set, that permits calculate the radius of the circle.
|
63 |
*/
|
64 |
protected Point2D m_LastPoint; |
65 |
|
66 |
/**
|
67 |
* Tool listener used to work with the <code>MapControl</code> object.
|
68 |
*
|
69 |
* @see #getListener()
|
70 |
* @see #setListener(ToolListener)
|
71 |
*/
|
72 |
private CircleListener listener;
|
73 |
|
74 |
/**
|
75 |
* Determines if user setting the radius of the circle (with one click of the button 1 of the mouse), or not.
|
76 |
*/
|
77 |
protected boolean isClicked = false; |
78 |
|
79 |
/**
|
80 |
* <p>Creates a new behavior for selecting circle areas.</p>
|
81 |
*
|
82 |
* @param zili listener used to permit this object to work with the associated <code>MapControl</code>
|
83 |
*/
|
84 |
public CircleBehavior(CircleListener zili) {
|
85 |
listener = zili; |
86 |
} |
87 |
|
88 |
/*
|
89 |
* (non-Javadoc)
|
90 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
|
91 |
*/
|
92 |
public void paintComponent(MapControlDrawer mapControlDrawer) { |
93 |
double radio;
|
94 |
BufferedImage img = getMapControl().getImage();
|
95 |
mapControlDrawer.drawImage(img, 0, 0); |
96 |
mapControlDrawer.setColor(Color.black);
|
97 |
|
98 |
|
99 |
if ((m_FirstPoint != null) && (m_LastPoint != null)) { |
100 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
101 |
Point2D p1 = vp.toMapPoint(m_FirstPoint);
|
102 |
Point2D p2 = vp.toMapPoint(m_LastPoint);
|
103 |
|
104 |
radio = p1.distance(p2); |
105 |
if(radio!=0.0){ |
106 |
Arc arc = null;
|
107 |
arc = createArc(p1.getX(), p1.getY(), |
108 |
radio, 0, Math.PI*2); |
109 |
if (arc != null) { |
110 |
mapControlDrawer.draw(arc); |
111 |
} |
112 |
} |
113 |
|
114 |
} |
115 |
} |
116 |
|
117 |
/*
|
118 |
* (non-Javadoc)
|
119 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mousePressed(java.awt.event.MouseEvent)
|
120 |
*/
|
121 |
public void mousePressed(MouseEvent e) { |
122 |
if (this.isMyButton(e)) { |
123 |
m_FirstPoint = e.getPoint(); |
124 |
isClicked = true;
|
125 |
getMapControl().repaint(); |
126 |
if (listener.cancelDrawing()) {
|
127 |
getMapControl().cancelDrawing(); |
128 |
isClicked = false;
|
129 |
getMapControl().repaint(); |
130 |
} |
131 |
} |
132 |
|
133 |
|
134 |
} |
135 |
|
136 |
/*
|
137 |
* (non-Javadoc)
|
138 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseReleased(java.awt.event.MouseEvent)
|
139 |
*/
|
140 |
public void mouseReleased(MouseEvent e) throws BehaviorException { |
141 |
if( this.isMyButton(e) ) { |
142 |
m_FirstPoint = null;
|
143 |
m_LastPoint = null;
|
144 |
isClicked = false;
|
145 |
} |
146 |
} |
147 |
|
148 |
/*
|
149 |
* (non-Javadoc)
|
150 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseDragged(java.awt.event.MouseEvent)
|
151 |
*/
|
152 |
public void mouseDragged(MouseEvent e) throws BehaviorException { |
153 |
mouseMoved(e); |
154 |
} |
155 |
|
156 |
/*
|
157 |
* (non-Javadoc)
|
158 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseMoved(java.awt.event.MouseEvent)
|
159 |
*/
|
160 |
public void mouseMoved(MouseEvent e) throws BehaviorException { |
161 |
if (m_FirstPoint == null) return; |
162 |
|
163 |
m_LastPoint = e.getPoint(); |
164 |
|
165 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
166 |
|
167 |
Point2D p1 = vp.toMapPoint(m_FirstPoint);
|
168 |
Point2D p2 = vp.toMapPoint(m_LastPoint);
|
169 |
|
170 |
// Fijamos el nuevo extent
|
171 |
Rectangle2D.Double r = new Rectangle2D.Double(); |
172 |
r.setFrameFromDiagonal(p1, p2); |
173 |
|
174 |
Rectangle2D rectPixel = new Rectangle(); |
175 |
rectPixel.setFrameFromDiagonal(m_FirstPoint, m_LastPoint); |
176 |
|
177 |
Double[] x = new Double[2]; |
178 |
Double[] y = new Double[2]; |
179 |
x[0] = new Double(p1.getX()); |
180 |
x[1] = new Double(p2.getX()); |
181 |
y[0] = new Double(p1.getY()); |
182 |
y[1] = new Double(p2.getY()); |
183 |
MeasureEvent event = new MeasureEvent(x, y, e);
|
184 |
listener.circle(event); |
185 |
getMapControl().repaint(); |
186 |
} |
187 |
|
188 |
/**
|
189 |
* <p>Sets a tool listener to work with the <code>MapControl</code> using this behavior.</p>
|
190 |
*
|
191 |
* @param listener a <code>CircleListener</code> object for this behavior
|
192 |
*/
|
193 |
public void setListener(ToolListener listener) { |
194 |
this.listener = (CircleListener)listener;
|
195 |
} |
196 |
|
197 |
/*
|
198 |
* (non-Javadoc)
|
199 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
|
200 |
*/
|
201 |
public ToolListener getListener() {
|
202 |
return listener;
|
203 |
} |
204 |
} |
205 |
|