Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / tools / Behavior / CircleBehavior.java @ 20098

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

    
53
import com.iver.cit.gvsig.fmap.ViewPort;
54
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
55
import com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent;
56
import com.iver.cit.gvsig.fmap.tools.Listeners.CircleListener;
57
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener;
58

    
59

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

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

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

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

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

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

    
106
                double radio;
107
                BufferedImage img = getMapControl().getImage();
108
                g.drawImage(img, 0, 0, null);
109
                g.setColor(Color.black);
110
                g.setXORMode(Color.white);
111
                if ((m_FirstPoint != null) && (m_LastPoint != null)) {
112
                        radio = m_LastPoint.distance(m_FirstPoint);
113
                        Arc2D.Double arc = new Arc2D.Double(m_FirstPoint.getX()-radio,
114
                                                                                                m_FirstPoint.getY()-radio,
115
                                                                                                2*radio,
116
                                                                                                2*radio, 0, 360, Arc2D.OPEN);
117
                        ((Graphics2D) g).draw(arc);
118
                }
119
        }
120

    
121
        /*
122
         * (non-Javadoc)
123
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mousePressed(java.awt.event.MouseEvent)
124
         */
125
        public void mousePressed(MouseEvent e) {
126
                if (e.getButton() == MouseEvent.BUTTON1) {
127
                        m_FirstPoint = e.getPoint();
128
                        isClicked = true;
129
                        getMapControl().repaint();
130
                }
131

    
132
                if (listener.cancelDrawing()) {
133
                        getMapControl().cancelDrawing();
134
                        isClicked = false;
135
                        getMapControl().repaint();
136
                }
137
        }
138

    
139
        /*
140
         * (non-Javadoc)
141
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseReleased(java.awt.event.MouseEvent)
142
         */
143
        public void mouseReleased(MouseEvent e) throws BehaviorException {
144
                m_FirstPoint = null;
145
                m_LastPoint = null;
146
                isClicked = false;
147
        }
148

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

    
157
        /*
158
         * (non-Javadoc)
159
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseMoved(java.awt.event.MouseEvent)
160
         */
161
        public void mouseMoved(MouseEvent e)  throws BehaviorException {
162
            if (m_FirstPoint == null) return;
163

    
164
            m_LastPoint = e.getPoint();
165

    
166
                ViewPort vp = getMapControl().getMapContext().getViewPort();
167

    
168
                Point2D p1 = vp.toMapPoint(m_FirstPoint);
169
                Point2D p2 = vp.toMapPoint(m_LastPoint);
170

    
171
                //        Fijamos el nuevo extent
172
                Rectangle2D.Double r = new Rectangle2D.Double();
173
                r.setFrameFromDiagonal(p1, p2);
174

    
175
                Rectangle2D rectPixel = new Rectangle();
176
                rectPixel.setFrameFromDiagonal(m_FirstPoint, m_LastPoint);
177

    
178
                Double[] x = new Double[2];
179
                Double[] y = new Double[2];
180
                x[0] = new Double(p1.getX());
181
                x[1] = new Double(p2.getX());
182
                y[0] = new Double(p1.getY());
183
                y[1] = new Double(p2.getY());
184
                MeasureEvent event = new MeasureEvent(x, y, e);
185
                listener.circle(event);
186
                getMapControl().repaint();
187
        }
188

    
189
        /**
190
         * <p>Sets a tool listener to work with the <code>MapControl</code> using this behavior.</p>
191
         * 
192
         * @param listener a <code>CircleListener</code> object for this behavior
193
         */
194
        public void setListener(ToolListener listener) {
195
                this.listener = (CircleListener)listener;
196
        }
197

    
198
        /*
199
         * (non-Javadoc)
200
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
201
         */
202
        public ToolListener getListener() {
203
                return listener;
204
        }
205
}
206