Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2058 / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / tools / Behavior / CircleBehavior.java @ 39241

History | View | Annotate | Download (6.1 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.Color;
44
import java.awt.Rectangle;
45
import java.awt.event.MouseEvent;
46
import java.awt.geom.Point2D;
47
import java.awt.geom.Rectangle2D;
48
import java.awt.image.BufferedImage;
49

    
50
import org.gvsig.fmap.geom.primitive.Arc;
51
import org.gvsig.fmap.mapcontext.ViewPort;
52
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
53
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
54
import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent;
55
import org.gvsig.fmap.mapcontrol.tools.Listeners.CircleListener;
56
import org.gvsig.fmap.mapcontrol.tools.Listeners.PolylineListener;
57
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
58

    
59

    
60

    
61
/**
62
 * <p>Behavior that allows user to draw a circle on the image of the associated
63
 *  <code>MapControl</code> using a {@link PolylineListener PolylineListener}.</p>
64
 *
65
 * @author Laura
66
 * @author Pablo Piqueras Bartolom?
67
 */
68
public class CircleBehavior extends Behavior {
69
        /**
70
         * First point set, that represents the center of the circle.
71
         */
72
        protected Point2D m_FirstPoint;
73

    
74
        /**
75
         * Second point set, that permits calculate the radius of the circle.
76
         */
77
        protected Point2D m_LastPoint;
78

    
79
        /**
80
         * Tool listener used to work with the <code>MapControl</code> object.
81
         *
82
         * @see #getListener()
83
         * @see #setListener(ToolListener)
84
         */
85
        private CircleListener listener;
86

    
87
        /**
88
         * Determines if user setting the radius of the circle (with one click of the button 1 of the mouse), or not.
89
         */
90
        protected boolean isClicked = false;
91

    
92
        /**
93
         * <p>Creates a new behavior for selecting circle areas.</p>
94
         *
95
         * @param zili listener used to permit this object to work with the associated <code>MapControl</code>
96
         */
97
        public CircleBehavior(CircleListener zili) {
98
                listener = zili;
99
        }
100

    
101
        /*
102
         * (non-Javadoc)
103
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
104
         */
105
        public void paintComponent(MapControlDrawer mapControlDrawer) {
106

    
107
                double radio;
108
                BufferedImage img = getMapControl().getImage();
109
                mapControlDrawer.drawImage(img, 0, 0);
110
                mapControlDrawer.setColor(Color.black);
111

    
112

    
113
                if ((m_FirstPoint != null) && (m_LastPoint != null)) {
114
                        ViewPort vp = getMapControl().getMapContext().getViewPort();
115
                        Point2D p1 = vp.toMapPoint(m_FirstPoint);
116
                        Point2D p2 = vp.toMapPoint(m_LastPoint);
117

    
118
                        radio = p1.distance(p2);
119
                        if(radio!=0.0){
120
                                Arc arc = null;                        
121
                                arc = createArc(p1.getX(), p1.getY(),
122
                                    radio, 0, Math.PI*2);
123
                                if (arc != null) {
124
                                    mapControlDrawer.draw(arc);
125
                                }
126
                        }
127

    
128
                }
129
        }
130

    
131
        /*
132
         * (non-Javadoc)
133
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mousePressed(java.awt.event.MouseEvent)
134
         */
135
        public void mousePressed(MouseEvent e) {
136
                if (e.getButton() == MouseEvent.BUTTON1) {
137
                        m_FirstPoint = e.getPoint();
138
                        isClicked = true;
139
                        getMapControl().repaint();
140
                }
141

    
142
                if (listener.cancelDrawing()) {
143
                        getMapControl().cancelDrawing();
144
                        isClicked = false;
145
                        getMapControl().repaint();
146
                }
147
        }
148

    
149
        /*
150
         * (non-Javadoc)
151
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseReleased(java.awt.event.MouseEvent)
152
         */
153
        public void mouseReleased(MouseEvent e) throws BehaviorException {
154
                m_FirstPoint = null;
155
                m_LastPoint = null;
156
                isClicked = false;
157
        }
158

    
159
        /*
160
         * (non-Javadoc)
161
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseDragged(java.awt.event.MouseEvent)
162
         */
163
        public void mouseDragged(MouseEvent e) throws BehaviorException {
164
                mouseMoved(e);
165
        }
166

    
167
        /*
168
         * (non-Javadoc)
169
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseMoved(java.awt.event.MouseEvent)
170
         */
171
        public void mouseMoved(MouseEvent e)  throws BehaviorException {
172
                if (m_FirstPoint == null) return;
173

    
174
                m_LastPoint = e.getPoint();
175

    
176
                ViewPort vp = getMapControl().getMapContext().getViewPort();
177

    
178
                Point2D p1 = vp.toMapPoint(m_FirstPoint);
179
                Point2D p2 = vp.toMapPoint(m_LastPoint);
180

    
181
                //        Fijamos el nuevo extent
182
                Rectangle2D.Double r = new Rectangle2D.Double();
183
                r.setFrameFromDiagonal(p1, p2);
184

    
185
                Rectangle2D rectPixel = new Rectangle();
186
                rectPixel.setFrameFromDiagonal(m_FirstPoint, m_LastPoint);
187

    
188
                Double[] x = new Double[2];
189
                Double[] y = new Double[2];
190
                x[0] = new Double(p1.getX());
191
                x[1] = new Double(p2.getX());
192
                y[0] = new Double(p1.getY());
193
                y[1] = new Double(p2.getY());
194
                MeasureEvent event = new MeasureEvent(x, y, e);
195
                listener.circle(event);
196
                getMapControl().repaint();
197
        }
198

    
199
        /**
200
         * <p>Sets a tool listener to work with the <code>MapControl</code> using this behavior.</p>
201
         *
202
         * @param listener a <code>CircleListener</code> object for this behavior
203
         */
204
        public void setListener(ToolListener listener) {
205
                this.listener = (CircleListener)listener;
206
        }
207

    
208
        /*
209
         * (non-Javadoc)
210
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
211
         */
212
        public ToolListener getListener() {
213
                return listener;
214
        }
215
}
216