root / tags / v2_0_0_Build_2038 / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / tools / Behavior / PolylineBehavior.java @ 37103
History | View | Annotate | Download (6.63 KB)
1 | 21203 | vcaballero | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | 21711 | vcaballero | package org.gvsig.fmap.mapcontrol.tools.Behavior; |
42 | 21203 | vcaballero | |
43 | import java.awt.Color; |
||
44 | import java.awt.event.MouseEvent; |
||
45 | import java.awt.geom.Point2D; |
||
46 | import java.util.ArrayList; |
||
47 | |||
48 | import org.gvsig.fmap.geom.primitive.GeneralPathX; |
||
49 | 30327 | jpiera | import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
50 | 21203 | vcaballero | import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
51 | import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent; |
||
52 | import org.gvsig.fmap.mapcontrol.tools.Listeners.PolylineListener; |
||
53 | import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener; |
||
54 | |||
55 | |||
56 | |||
57 | /**
|
||
58 | * <p>Behavior that allows user to draw a polyline by its vertexes on the image of the associated
|
||
59 | * <code>MapControl</code> using a {@link PolylineListener PolylineListener}.</p>
|
||
60 | *
|
||
61 | * @author Vicente Caballero Navarro
|
||
62 | * @author Pablo Piqueras Bartolom?
|
||
63 | */
|
||
64 | public class PolylineBehavior extends Behavior { |
||
65 | /**
|
||
66 | * The abscissa coordinate of all vertexes of the polyline.
|
||
67 | */
|
||
68 | 22507 | vcaballero | protected ArrayList arrayX = new ArrayList(); //<Double> |
69 | 21203 | vcaballero | |
70 | /**
|
||
71 | * The ordinate coordinate of all vertexes of the polyline.
|
||
72 | */
|
||
73 | 22507 | vcaballero | protected ArrayList arrayY = new ArrayList(); //<Double> |
74 | 21203 | vcaballero | |
75 | /**
|
||
76 | * Determines if user is setting the vertexes (with one click of the button 1 of the mouse), or not.
|
||
77 | */
|
||
78 | protected boolean isClicked = false; |
||
79 | |||
80 | /**
|
||
81 | * Tool listener used to work with the <code>MapControl</code> object.
|
||
82 | *
|
||
83 | * @see #getListener()
|
||
84 | * @see #setListener(ToolListener)
|
||
85 | */
|
||
86 | protected PolylineListener listener;
|
||
87 | |||
88 | /**
|
||
89 | * <p>Creates a new behavior for drawing a polyline by its vertexes.</p>
|
||
90 | *
|
||
91 | * @param mli listener used to permit this object to work with the associated <code>MapControl</code>
|
||
92 | */
|
||
93 | public PolylineBehavior(PolylineListener mli) {
|
||
94 | listener = mli; |
||
95 | } |
||
96 | |||
97 | /*
|
||
98 | * (non-Javadoc)
|
||
99 | * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
|
||
100 | */
|
||
101 | 30327 | jpiera | public void paintComponent(MapControlDrawer mapControlDrawer) { |
102 | mapControlDrawer.drawImage(getMapControl().getImage(), 0, 0); |
||
103 | mapControlDrawer.setColor(Color.black);
|
||
104 | 21203 | vcaballero | |
105 | if (!arrayX.isEmpty()) {
|
||
106 | 30327 | jpiera | drawPolyLine(mapControlDrawer); |
107 | 21203 | vcaballero | } |
108 | } |
||
109 | |||
110 | /*
|
||
111 | * (non-Javadoc)
|
||
112 | * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mousePressed(java.awt.event.MouseEvent)
|
||
113 | */
|
||
114 | public void mousePressed(MouseEvent e) throws BehaviorException { |
||
115 | if (e.getClickCount() == 2) { |
||
116 | listener.polylineFinished(new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e)); |
||
117 | |||
118 | arrayX.clear(); |
||
119 | arrayY.clear(); |
||
120 | |||
121 | isClicked = false;
|
||
122 | } else {
|
||
123 | //System.err.println("simpleclick");
|
||
124 | isClicked = true;
|
||
125 | Point2D point = getMapControl().getViewPort().toMapPoint(e.getPoint());
|
||
126 | addPoint(point); |
||
127 | |||
128 | if (arrayX.size() < 2) { |
||
129 | addPoint(point); |
||
130 | } |
||
131 | |||
132 | listener.pointFixed(new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e)); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | /*
|
||
137 | * (non-Javadoc)
|
||
138 | * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseDragged(java.awt.event.MouseEvent)
|
||
139 | */
|
||
140 | public void mouseDragged(MouseEvent e) throws BehaviorException { |
||
141 | mouseMoved(e); |
||
142 | } |
||
143 | |||
144 | /**
|
||
145 | * <p>Changes the last point added of the polyline.</p>
|
||
146 | *
|
||
147 | * @param p a point which will replace the last added to the polyline
|
||
148 | */
|
||
149 | protected void changeLastPoint(Point2D p) { |
||
150 | if (arrayX.size() > 0) { |
||
151 | arrayX.set(arrayX.size() - 1, new Double(p.getX())); |
||
152 | arrayY.set(arrayY.size() - 1, new Double(p.getY())); |
||
153 | } |
||
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 | //System.err.println("moved antes de click");
|
||
162 | if (isClicked) {
|
||
163 | //System.err.println("moved despues de click");
|
||
164 | changeLastPoint(getMapControl().getViewPort().toMapPoint(e.getPoint())); |
||
165 | |||
166 | MeasureEvent event = new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e); |
||
167 | |||
168 | listener.points(event); |
||
169 | getMapControl().repaint(); |
||
170 | } |
||
171 | } |
||
172 | |||
173 | /**
|
||
174 | * <p>Draws the polyline in the <code>Graphics2D</code> of the associated <code>MapControl</code>.</p>
|
||
175 | *
|
||
176 | * @param g2 the 2D context that allows draw the polyline
|
||
177 | */
|
||
178 | 30327 | jpiera | protected void drawPolyLine(MapControlDrawer mapControlDrawer) { |
179 | 21203 | vcaballero | GeneralPathX polyline = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, arrayX.size());
|
180 | |||
181 | 33411 | fdiaz | polyline.moveTo(((Double) arrayX.get(0)).doubleValue(), |
182 | ((Double) arrayY.get(0)).doubleValue()); |
||
183 | 21203 | vcaballero | |
184 | for (int index = 0; index < arrayX.size(); index++) { |
||
185 | 33411 | fdiaz | polyline.lineTo(((Double) arrayX.get(index)).doubleValue(),((Double) arrayY.get(index)).doubleValue()); |
186 | 21203 | vcaballero | } |
187 | |||
188 | 30327 | jpiera | mapControlDrawer.draw(createLineString(polyline)); |
189 | 21203 | vcaballero | } |
190 | |||
191 | /**
|
||
192 | * <p>Adds a new point to the polyline.</p>
|
||
193 | *
|
||
194 | * @param p a new point to the polyline
|
||
195 | */
|
||
196 | protected void addPoint(Point2D p) { |
||
197 | arrayX.add(new Double(p.getX())); |
||
198 | arrayY.add(new Double(p.getY())); |
||
199 | } |
||
200 | |||
201 | /**
|
||
202 | * <p>Sets a tool listener to work with the <code>MapControl</code> using this behavior.</p>
|
||
203 | *
|
||
204 | * @param listener a <code>PolylineListener</code> object for this behavior
|
||
205 | */
|
||
206 | public void setListener(ToolListener listener) { |
||
207 | this.listener = (PolylineListener) listener;
|
||
208 | } |
||
209 | |||
210 | /*
|
||
211 | * (non-Javadoc)
|
||
212 | * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
|
||
213 | */
|
||
214 | public ToolListener getListener() {
|
||
215 | return listener;
|
||
216 | } |
||
217 | } |