Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / tools / Behavior / PolygonBehavior.java @ 30327

History | View | Annotate | Download (4.74 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 org.gvsig.fmap.mapcontrol.tools.Behavior;
42

    
43
import java.awt.Graphics;
44
import java.awt.event.MouseEvent;
45
import java.awt.geom.Point2D;
46

    
47
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
48
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
49
import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent;
50
import org.gvsig.fmap.mapcontrol.tools.Listeners.PolylineListener;
51

    
52

    
53

    
54
/**
55
 * <p>Behavior that permits user to draw a polygon by its vertexes on the image of the associated
56
 *  <code>MapControl</code> using a {@link PolylineListener PolylineListener}.</p>
57
 *
58
 * @author Vicente Caballero Navarro
59
 * @author Pablo Piqueras Bartolom?
60
 */
61
public class PolygonBehavior extends PolylineBehavior {
62
        /**
63
         * <p>Creates a new behavior for drawing a polygon by its vertexes.</p>
64
         *
65
         * @param ali tool listener used to permit this object to work with the associated <code>MapControl</code>
66
         */
67
        public PolygonBehavior(PolylineListener ali) {
68
                super(ali);
69
        }
70

    
71
        /*
72
         * (non-Javadoc)
73
         * @see org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior#paintComponent(org.gvsig.fmap.mapcontrol.MapControlDrawer)
74
         */
75
        public void paintComponent(MapControlDrawer mapControlDrawer) {
76
                super.paintComponent(mapControlDrawer);
77
        }
78

    
79
        /*
80
         * (non-Javadoc)
81
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.PolylineBehavior#mousePressed(java.awt.event.MouseEvent)
82
         */
83
        public void mousePressed(MouseEvent e) throws BehaviorException {
84
                if (e.getClickCount() == 2) {
85
                        listener.polylineFinished(new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e));
86

    
87
                        arrayX.clear();
88
                        arrayY.clear();
89

    
90
                        isClicked = false;
91
                } else {
92
                        isClicked = true;
93

    
94
                        if (arrayX.size() == 0) {
95
                                addPoint(getMapControl().getViewPort().toMapPoint(e.getPoint()));
96
                                addPoint(getMapControl().getViewPort().toMapPoint(e.getPoint()));
97
                                addPoint(getMapControl().getViewPort().toMapPoint(e.getPoint()));
98
                        } else {
99
                                addAntPoint(getMapControl().getViewPort().toMapPoint(e.getPoint()));
100
                        }
101

    
102
                        listener.pointFixed(new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e));
103
                }
104
        }
105

    
106
        /**
107
         * <p>Adds a new vertex to the polygon.</p>
108
         *
109
         * @param p a new vertex to the polygon
110
         */
111
        private void addAntPoint(Point2D p) {
112
                arrayX.add(arrayX.size() - 1, new Double(p.getX()));
113
                arrayY.add(arrayY.size() - 1, new Double(p.getY()));
114
        }
115

    
116
        /**
117
         * <p>Changes the last vertex added to the polygon.</p>
118
         *
119
         * @param p a new vertex to the polygon
120
         */
121
        private void changeAntPoint(Point2D p) {
122
                if (arrayX.size() > 2) {
123
                        arrayX.set(arrayX.size() - 2, new Double(p.getX()));
124
                        arrayY.set(arrayY.size() - 2, new Double(p.getY()));
125
                }
126
        }
127

    
128
        /*
129
         * (non-Javadoc)
130
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.PolylineBehavior#mouseMoved(java.awt.event.MouseEvent)
131
         */
132
        public void mouseMoved(MouseEvent e) throws BehaviorException {
133
                if (isClicked) {
134
                        changeAntPoint(getMapControl().getViewPort().toMapPoint(e.getPoint()));
135

    
136
                        MeasureEvent event = new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e);
137

    
138
                        listener.points(event);
139

    
140
                        getMapControl().repaint();
141
                }
142
        }
143

    
144
        /*
145
         * (non-Javadoc)
146
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.PolylineBehavior#mouseDragged(java.awt.event.MouseEvent)
147
         */
148
        public void mouseDragged(MouseEvent e) throws BehaviorException {
149
                mouseMoved(e);
150
        }
151
}