Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / tools / Behavior / PolygonBehavior.java @ 20100

History | View | Annotate | Download (4.57 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
 * <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
 */
60
public class PolygonBehavior extends PolylineBehavior {
61
        /**
62
         * <p>Creates a new behavior for drawing a polygon by its vertexes.</p>
63
         *
64
         * @param ali tool listener used to permit this object to work with the associated <code>MapControl</code>
65
         */
66
        public PolygonBehavior(PolylineListener ali) {
67
                super(ali);
68
        }
69

    
70
        /*
71
         * (non-Javadoc)
72
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.PolylineBehavior#paintComponent(java.awt.Graphics)
73
         */
74
        public void paintComponent(Graphics g) {
75
                g.drawImage(getMapControl().getImage(), 0, 0, null);
76
                g.setColor(Color.black);
77

    
78
                if (!arrayX.isEmpty()) {
79
                        drawPolyLine((Graphics2D) g);
80
                }
81
        }
82

    
83
        /*
84
         * (non-Javadoc)
85
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.PolylineBehavior#mousePressed(java.awt.event.MouseEvent)
86
         */
87
        public void mousePressed(MouseEvent E) throws BehaviorException {
88
                if (E.getClickCount() == 2) {
89
                        listener.polylineFinished(new MeasureEvent(
90
                                        (Double[]) arrayX.toArray(new Double[0]),
91
                                        (Double[]) arrayY.toArray(new Double[0]), E));
92
                        arrayX.clear();
93
                        arrayY.clear();
94
                        isClicked = false;
95
                } else {
96
                        isClicked = true;
97

    
98
                        if (arrayX.size() == 0) {
99
                                addPoint(getMapControl().getViewPort().toMapPoint(E.getPoint()));
100
                                addPoint(getMapControl().getViewPort().toMapPoint(E.getPoint()));
101
                                addPoint(getMapControl().getViewPort().toMapPoint(E.getPoint()));
102
                        } else {
103
                                addAntPoint(getMapControl().getViewPort().toMapPoint(E.getPoint()));
104
                        }
105

    
106
                        listener.pointFixed(new MeasureEvent(
107
                                        (Double[]) arrayX.toArray(new Double[0]),
108
                                        (Double[]) arrayY.toArray(new Double[0]), E));
109
                }
110
        }
111

    
112
        /**
113
         * <p>Adds a new vertex to the polygon.</p>
114
         *
115
         * @param p a new vertex to the polygon
116
         */
117
        private void addAntPoint(Point2D p) {
118
                arrayX.add(arrayX.size() - 1, new Double(p.getX()));
119
                arrayY.add(arrayY.size() - 1, new Double(p.getY()));
120
        }
121

    
122
        /**
123
         * <p>Changes the last vertex added to the polygon.</p>
124
         *
125
         * @param p a new vertex to the polygon
126
         */
127
        private void changeAntPoint(Point2D p) {
128
                if (arrayX.size() > 2) {
129
                        arrayX.set(arrayX.size() - 2, new Double(p.getX()));
130
                        arrayY.set(arrayY.size() - 2, new Double(p.getY()));
131
                }
132
        }
133

    
134
        /*
135
         * (non-Javadoc)
136
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.PolylineBehavior#mouseMoved(java.awt.event.MouseEvent)
137
         */
138
        public void mouseMoved(MouseEvent E) throws BehaviorException {
139
                if (isClicked) {
140
                        changeAntPoint(getMapControl().getViewPort().toMapPoint(E.getPoint()));
141

    
142
                        MeasureEvent event = new MeasureEvent((Double[]) arrayX.toArray(
143
                                                new Double[0]),
144
                                        (Double[]) arrayY.toArray(new Double[0]), E);
145
                        listener.points(event);
146
                        getMapControl().repaint();
147
                }
148
        }
149

    
150
        /*
151
         * (non-Javadoc)
152
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.PolylineBehavior#mouseDragged(java.awt.event.MouseEvent)
153
         */
154
        public void mouseDragged(MouseEvent e) throws BehaviorException {
155
                mouseMoved(e);
156
        }
157
}