svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / tools / Behavior / PolygonBehavior.java @ 41971
History | View | Annotate | Download (4.59 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.event.MouseEvent; |
27 |
import java.awt.geom.Point2D; |
28 |
|
29 |
import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
30 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
31 |
import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent; |
32 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.PolylineListener; |
33 |
|
34 |
|
35 |
|
36 |
/**
|
37 |
* <p>Behavior that permits user to draw a polygon by its vertexes on the image of the associated
|
38 |
* <code>MapControl</code> using a {@link PolylineListener PolylineListener}.</p>
|
39 |
*
|
40 |
* @author Vicente Caballero Navarro
|
41 |
* @author Pablo Piqueras Bartolom?
|
42 |
*/
|
43 |
public class PolygonBehavior extends PolylineBehavior { |
44 |
/**
|
45 |
* <p>Creates a new behavior for drawing a polygon by its vertexes.</p>
|
46 |
*
|
47 |
* @param ali tool listener used to permit this object to work with the associated <code>MapControl</code>
|
48 |
*/
|
49 |
public PolygonBehavior(PolylineListener ali) {
|
50 |
super(ali);
|
51 |
} |
52 |
|
53 |
/*
|
54 |
* (non-Javadoc)
|
55 |
* @see org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior#paintComponent(org.gvsig.fmap.mapcontrol.MapControlDrawer)
|
56 |
*/
|
57 |
public void paintComponent(MapControlDrawer mapControlDrawer) { |
58 |
super.paintComponent(mapControlDrawer);
|
59 |
} |
60 |
|
61 |
/*
|
62 |
* (non-Javadoc)
|
63 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.PolylineBehavior#mousePressed(java.awt.event.MouseEvent)
|
64 |
*/
|
65 |
public void mousePressed(MouseEvent e) throws BehaviorException { |
66 |
if( !isMyButton(e) ) {
|
67 |
return;
|
68 |
} |
69 |
if (e.getClickCount() == 2) { |
70 |
listener.polylineFinished(new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e)); |
71 |
|
72 |
arrayX.clear(); |
73 |
arrayY.clear(); |
74 |
|
75 |
isClicked = false;
|
76 |
} else {
|
77 |
isClicked = true;
|
78 |
|
79 |
if (arrayX.size() == 0) { |
80 |
addPoint(getMapControl().getViewPort().toMapPoint(e.getPoint())); |
81 |
addPoint(getMapControl().getViewPort().toMapPoint(e.getPoint())); |
82 |
addPoint(getMapControl().getViewPort().toMapPoint(e.getPoint())); |
83 |
} else {
|
84 |
addAntPoint(getMapControl().getViewPort().toMapPoint(e.getPoint())); |
85 |
} |
86 |
|
87 |
listener.pointFixed(new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e)); |
88 |
} |
89 |
} |
90 |
|
91 |
/**
|
92 |
* <p>Adds a new vertex to the polygon.</p>
|
93 |
*
|
94 |
* @param p a new vertex to the polygon
|
95 |
*/
|
96 |
private void addAntPoint(Point2D p) { |
97 |
arrayX.add(arrayX.size() - 1, new Double(p.getX())); |
98 |
arrayY.add(arrayY.size() - 1, new Double(p.getY())); |
99 |
} |
100 |
|
101 |
/**
|
102 |
* <p>Changes the last vertex added to the polygon.</p>
|
103 |
*
|
104 |
* @param p a new vertex to the polygon
|
105 |
*/
|
106 |
private void changeAntPoint(Point2D p) { |
107 |
if (arrayX.size() > 2) { |
108 |
arrayX.set(arrayX.size() - 2, new Double(p.getX())); |
109 |
arrayY.set(arrayY.size() - 2, new Double(p.getY())); |
110 |
} |
111 |
} |
112 |
|
113 |
/*
|
114 |
* (non-Javadoc)
|
115 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.PolylineBehavior#mouseMoved(java.awt.event.MouseEvent)
|
116 |
*/
|
117 |
public void mouseMoved(MouseEvent e) throws BehaviorException { |
118 |
if( !isMyButton(e) ) {
|
119 |
return;
|
120 |
} |
121 |
if (isClicked) {
|
122 |
changeAntPoint(getMapControl().getViewPort().toMapPoint(e.getPoint())); |
123 |
|
124 |
MeasureEvent event = new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e); |
125 |
|
126 |
listener.points(event); |
127 |
|
128 |
getMapControl().repaint(); |
129 |
} |
130 |
} |
131 |
|
132 |
/*
|
133 |
* (non-Javadoc)
|
134 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.PolylineBehavior#mouseDragged(java.awt.event.MouseEvent)
|
135 |
*/
|
136 |
public void mouseDragged(MouseEvent e) throws BehaviorException { |
137 |
mouseMoved(e); |
138 |
} |
139 |
} |