svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / tools / Behavior / PolygonBehavior.java @ 7011
History | View | Annotate | Download (4.36 KB)
1 |
/* 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 |
package com.iver.cit.gvsig.fmap.tools.Behavior; |
42 |
|
43 |
import com.iver.cit.gvsig.fmap.tools.BehaviorException; |
44 |
import com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent; |
45 |
import com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener; |
46 |
|
47 |
import java.awt.Color; |
48 |
import java.awt.Graphics; |
49 |
import java.awt.Graphics2D; |
50 |
import java.awt.event.MouseEvent; |
51 |
import java.awt.geom.Point2D; |
52 |
|
53 |
|
54 |
/**
|
55 |
* Behaviour que espera un listener de tipo MeasureListener.
|
56 |
*
|
57 |
* @author Vicente Caballero Navarro
|
58 |
*/
|
59 |
public class PolygonBehavior extends PolylineBehavior { |
60 |
/**
|
61 |
* Crea un nuevo PolygonBehavior.
|
62 |
*
|
63 |
* @param ali listener.
|
64 |
*/
|
65 |
public PolygonBehavior(PolylineListener ali) {
|
66 |
super(ali);
|
67 |
} |
68 |
|
69 |
/**
|
70 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
|
71 |
*/
|
72 |
public void paintComponent(Graphics g) { |
73 |
g.drawImage(getMapControl().getImage(), 0, 0, null); |
74 |
g.setColor(Color.black);
|
75 |
|
76 |
if (!arrayX.isEmpty()) {
|
77 |
drawPolyLine((Graphics2D) g);
|
78 |
} |
79 |
} |
80 |
|
81 |
/**
|
82 |
* Reimplementaci?n del m?todo mousePressed de Behavior.
|
83 |
*
|
84 |
* @param E MouseEvent
|
85 |
*
|
86 |
* @throws BehaviorException Excepci?n lanzada cuando el Behavior.
|
87 |
*/
|
88 |
public void mousePressed(MouseEvent E) throws BehaviorException { |
89 |
if (E.getClickCount() == 2) { |
90 |
listener.polylineFinished(new MeasureEvent(
|
91 |
(Double[]) arrayX.toArray(new Double[0]), |
92 |
(Double[]) arrayY.toArray(new Double[0]), E)); |
93 |
arrayX.clear(); |
94 |
arrayY.clear(); |
95 |
isClicked = false;
|
96 |
} else {
|
97 |
isClicked = true;
|
98 |
|
99 |
if (arrayX.size() == 0) { |
100 |
addPoint(getMapControl().getViewPort().toMapPoint(E.getPoint())); |
101 |
addPoint(getMapControl().getViewPort().toMapPoint(E.getPoint())); |
102 |
addPoint(getMapControl().getViewPort().toMapPoint(E.getPoint())); |
103 |
} else {
|
104 |
addAntPoint(getMapControl().getViewPort().toMapPoint(E.getPoint())); |
105 |
} |
106 |
|
107 |
listener.pointFixed(new MeasureEvent(
|
108 |
(Double[]) arrayX.toArray(new Double[0]), |
109 |
(Double[]) arrayY.toArray(new Double[0]), E)); |
110 |
} |
111 |
} |
112 |
|
113 |
/**
|
114 |
* A?ade el punto anterior al actual.
|
115 |
*
|
116 |
* @param p Punto a a?adir.
|
117 |
*/
|
118 |
private void addAntPoint(Point2D p) { |
119 |
arrayX.add(arrayX.size() - 1, new Double(p.getX())); |
120 |
arrayY.add(arrayY.size() - 1, new Double(p.getY())); |
121 |
} |
122 |
|
123 |
/**
|
124 |
* Cambio de punto anterior.
|
125 |
*
|
126 |
* @param p Punto a cambiar.
|
127 |
*/
|
128 |
private void changeAntPoint(Point2D p) { |
129 |
if (arrayX.size() > 2) { |
130 |
arrayX.set(arrayX.size() - 2, new Double(p.getX())); |
131 |
arrayY.set(arrayY.size() - 2, new Double(p.getY())); |
132 |
} |
133 |
} |
134 |
|
135 |
/**
|
136 |
* Reimplementaci?n del m?todo mouseMoved de Behavior.
|
137 |
*
|
138 |
* @param E MouseEvent
|
139 |
*
|
140 |
* @throws BehaviorException Excepci?n lanzada cuando el Behavior.
|
141 |
*/
|
142 |
public void mouseMoved(MouseEvent E) throws BehaviorException { |
143 |
if (isClicked) {
|
144 |
changeAntPoint(getMapControl().getViewPort().toMapPoint(E.getPoint())); |
145 |
|
146 |
MeasureEvent event = new MeasureEvent((Double[]) arrayX.toArray( |
147 |
new Double[0]), |
148 |
(Double[]) arrayY.toArray(new Double[0]), E); |
149 |
listener.points(event); |
150 |
getMapControl().repaint(); |
151 |
} |
152 |
} |
153 |
|
154 |
/**
|
155 |
* Reimplementaci?n del m?todo mouseDragged de Behavior.
|
156 |
*
|
157 |
* @param e MouseEvent
|
158 |
*
|
159 |
* @throws BehaviorException Excepci?n lanzada cuando el Behavior.
|
160 |
*/
|
161 |
public void mouseDragged(MouseEvent e) throws BehaviorException { |
162 |
mouseMoved(e); |
163 |
} |
164 |
} |