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