Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / tools / Behavior / EllipseBehavior.java @ 44246

History | View | Annotate | Download (11.8 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.mapcontrol.tools.Behavior;
25

    
26
import java.awt.Color;
27
import java.awt.Point;
28
import java.awt.event.MouseEvent;
29
import java.awt.geom.Point2D;
30
import java.util.ArrayList;
31
import java.util.List;
32
import java.util.logging.Level;
33
import java.util.logging.Logger;
34
import org.gvsig.fmap.geom.Geometry;
35
import org.gvsig.fmap.geom.GeometryException;
36
import org.gvsig.fmap.geom.aggregate.MultiLine;
37
import org.gvsig.fmap.geom.primitive.Arc;
38
import org.gvsig.fmap.geom.primitive.Ellipse;
39

    
40
import org.gvsig.fmap.geom.primitive.Line;
41
import org.gvsig.fmap.mapcontext.ViewPort;
42
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
43
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
44
import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent;
45
import org.gvsig.fmap.mapcontrol.tools.Listeners.EllipseListener;
46
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
47

    
48
/**
49
 * <p>
50
 * Behavior that allows user to draw a polyline by its vertexes on the image of
51
 * the associated <code>MapControl</code> using a
52
 * {@link EllipseListener EllipseListener}.</p>
53
 *
54
 * @author Vicente Caballero Navarro
55
 * @author Pablo Piqueras Bartolom?
56
 */
57
public class EllipseBehavior extends Behavior {
58

    
59
    /**
60
     * The abscissa coordinate of all vertexes of the polyline.
61
     */
62
    protected List<Double> arrayX = new ArrayList<>();
63

    
64
    /**
65
     * The ordinate coordinate of all vertexes of the polyline.
66
     */
67
    protected List<Double> arrayY = new ArrayList<>();
68

    
69
    /**
70
     * Determines if user is setting the vertexes (with one click of the button
71
     * 1 of the mouse), or not.
72
     */
73
    protected boolean isClicked = false;
74

    
75
    /**
76
     * Tool listener used to work with the <code>MapControl</code> object.
77
     *
78
     * @see #getListener()
79
     * @see #setListener(ToolListener)
80
     */
81
    protected EllipseListener listener;
82
    private Point2D m_FirstPoint;
83
    private Point2D m_SecondPoint;
84
    private Point2D m_ThirdPoint;
85

    
86
    /**
87
     * <p>
88
     * Creates a new behavior for drawing a polyline by its vertexes.</p>
89
     *
90
     * @param mli listener used to permit this object to work with the
91
     * associated <code>MapControl</code>
92
     */
93
    public EllipseBehavior(EllipseListener mli) {
94
        super(Behavior.BUTTON_LEFT);
95
        listener = mli;
96
    }
97

    
98
    /*
99
         * (non-Javadoc)
100
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
101
     */
102
    @Override
103
    public void paintComponent(MapControlDrawer mapControlDrawer) {
104
        mapControlDrawer.setColor(Color.black);
105
        if (arrayX.size() == 1) {
106
            drawCircle(mapControlDrawer);
107
        } else if (arrayX.size()  > 1) {
108
            drawEllipseLine(mapControlDrawer);
109
        }
110

    
111
    }
112

    
113
    /*
114
         * (non-Javadoc)
115
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mousePressed(java.awt.event.MouseEvent)
116
     */
117
    @Override
118
    public void mousePressed(MouseEvent e) throws BehaviorException {
119

    
120
        if (this.isMyButton(e)) {
121
            if (arrayX.isEmpty()) {
122
                Point2D point = getMapControl().getViewPort().toMapPoint(e.getPoint());
123
                addPoint(point);
124
                m_FirstPoint = point;
125
            } else if (arrayX.size() == 1) {
126
                Point2D point = getMapControl().getViewPort().toMapPoint(e.getPoint());
127
                addPoint(point);
128
                m_SecondPoint = point;
129
            } else {
130
                Point2D point = getMapControl().getViewPort().toMapPoint(e.getPoint());
131
                addPoint(point);
132
                m_ThirdPoint = point;
133
//                Double[] x = new Double[3];
134
//                Double[] y = new Double[3];
135
//
136
//                x[0] = m_FirstPoint.getX();
137
//                x[1] = m_SecondPoint.getX();
138
//                x[2] = m_ThirdPoint.getX();
139
//
140
//                y[0] = m_FirstPoint.getY();
141
//                y[1] = m_SecondPoint.getY();
142
//                y[2] = m_ThirdPoint.getY();
143

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

    
146
                listener.ellipseFinished(event);
147

    
148
                arrayX.clear();
149
                arrayY.clear();
150
                m_FirstPoint = null;
151
                m_SecondPoint = null;
152
                m_ThirdPoint = null;
153
                isClicked = false;
154
                getMapControl().repaint();
155
                return;
156
            }
157

    
158
            isClicked = true;
159

    
160
            getMapControl().repaint();
161
            if (listener.cancelDrawing()) {
162
                getMapControl().cancelDrawing();
163
                isClicked = false;
164
                getMapControl().repaint();
165
            }
166
        }
167
    }
168

    
169
    /*
170
         * (non-Javadoc)
171
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseDragged(java.awt.event.MouseEvent)
172
     */
173
    @Override
174
    public void mouseDragged(MouseEvent e) throws BehaviorException {
175
        mouseMoved(e);
176
    }
177

    
178
    /**
179
     * <p>
180
     * Changes the last point added of the polyline.</p>
181
     *
182
     * @param p a point which will replace the last added to the polyline
183
     */
184
    protected void changeLastPoint(Point2D p) {
185
        if (arrayX.size() > 0) {
186
            arrayX.set(arrayX.size() - 1, p.getX());
187
            arrayY.set(arrayY.size() - 1, p.getY());
188
        }
189
    }
190

    
191
    /*
192
         * (non-Javadoc)
193
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseMoved(java.awt.event.MouseEvent)
194
     */
195
    @Override
196
    public void mouseMoved(MouseEvent e) throws BehaviorException {
197
        if (isClicked) {
198
            //System.err.println("moved despues de click");
199
//            changeLastPoint(getMapControl().getViewPort().toMapPoint(e.getPoint()));
200

    
201
//            MeasureEvent event = new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e);
202
            if (arrayX.size() == 1) {
203
                Point2D point = getMapControl().getViewPort().toMapPoint(e.getPoint());
204
                m_SecondPoint = point;
205
                ViewPort vp = getMapControl().getMapContext().getViewPort();
206
                Double[] x = new Double[2];
207
                Double[] y = new Double[2];
208
//                x[0] = vp.toMapPoint(m_FirstPoint).getX();
209
//                x[1] = vp.toMapPoint(m_SecondPoint).getX();
210
//                y[0] = vp.toMapPoint(m_FirstPoint).getY();
211
//                y[1] = vp.toMapPoint(m_SecondPoint).getY();
212
                x[0] = m_FirstPoint.getX();
213
                x[1] = m_SecondPoint.getX();
214
                y[0] = m_FirstPoint.getY();
215
                y[1] = m_SecondPoint.getY();
216
                MeasureEvent event = new MeasureEvent(x, y, e);
217
                listener.circle(event);
218
            } else if (arrayX.size() == 2) {
219
                Point2D point = getMapControl().getViewPort().toMapPoint(e.getPoint());
220
                m_ThirdPoint = point;
221
                ViewPort vp = getMapControl().getMapContext().getViewPort();
222
            
223
                Double[] x = new Double[3];
224
                Double[] y = new Double[3];
225
//                x[0] = vp.toMapPoint(m_FirstPoint).getX();
226
//                x[1] = vp.toMapPoint(m_SecondPoint).getX();
227
//                x[2] = vp.toMapPoint(m_ThirdPoint).getX();
228
//
229
//                y[0] = vp.toMapPoint(m_FirstPoint).getY();
230
//                y[1] = vp.toMapPoint(m_SecondPoint).getY();
231
//                y[2] = vp.toMapPoint(m_ThirdPoint).getY();
232
                
233
                x[0] = m_FirstPoint.getX();
234
                x[1] = m_SecondPoint.getX();
235
                x[2] = m_ThirdPoint.getX();
236

    
237
                y[0] = m_FirstPoint.getY();
238
                y[1] = m_SecondPoint.getY();
239
                y[2] = m_ThirdPoint.getY();
240
                MeasureEvent event = new MeasureEvent(x, y, e);
241

    
242
                listener.ellipse(event);
243
            }
244

    
245
            getMapControl().repaint();
246
        }
247
    }
248

    
249
    /**
250
     * <p>
251
     * Draws the polyline in the <code>Graphics2D</code> of the associated
252
     * <code>MapControl</code>.</p>
253
     *
254
     * @param mapControlDrawer
255
     */
256
    protected void drawEllipseLine(MapControlDrawer mapControlDrawer) {
257

    
258
        double radio;
259
        mapControlDrawer.setColor(Color.black);
260

    
261
        if ((m_FirstPoint != null) && (m_SecondPoint != null) && (m_ThirdPoint != null)) {
262
            ViewPort vp = getMapControl().getMapContext().getViewPort();
263
//            Point2D p1 = vp.toMapPoint(m_FirstPoint);
264
//            Point2D p2 = vp.toMapPoint(m_SecondPoint);
265
//            Point2D p3 = vp.toMapPoint(m_ThirdPoint);
266
            
267
            Point2D p1 = m_FirstPoint;
268
            Point2D p2 = m_SecondPoint;
269
            Point2D p3 = m_ThirdPoint;
270
            radio = p2.distance(p3);
271
            if (radio > 0.0) {
272
                MultiLine ellipse = null;
273
                try {
274
                    ellipse = createEllipse(p1, p2, p3).toLines();
275
                } catch (GeometryException ex) {
276
                    Logger.getLogger(EllipseBehavior.class.getName()).log(Level.SEVERE, null, ex);
277
                }
278
                if (ellipse != null) {
279
                    mapControlDrawer.draw(ellipse);
280
                }
281
            }
282

    
283
        }
284
    }
285

    
286
    protected void drawCircle(MapControlDrawer mapControlDrawer) {
287
        double radio;
288
        mapControlDrawer.setColor(Color.black);
289

    
290
        if ((m_FirstPoint != null) && (m_SecondPoint != null)) {
291
            ViewPort vp = getMapControl().getMapContext().getViewPort();
292
//            Point2D p1 = vp.toMapPoint(m_FirstPoint);
293
//            Point2D p2 = vp.toMapPoint(m_SecondPoint);
294
            Point2D p1 = m_FirstPoint;
295
            Point2D p2 = m_SecondPoint;
296
            radio = p1.distance(p2)/2;
297
            if (radio > 0.0) {
298
                Arc arc = null;
299
                arc = createArc((p1.getX()+p2.getX())/2, (p1.getY()+p2.getY())/2,
300
                        radio, 0, Math.PI * 2);
301
                if (arc != null) {
302
                    mapControlDrawer.draw(arc);
303
                }
304
            }
305

    
306
        }
307
    }
308

    
309
    /**
310
     * <p>
311
     * Adds a new point to the polyline.</p>
312
     *
313
     * @param p a new point to the polyline
314
     */
315
    protected void addPoint(Point2D p) {
316
        arrayX.add(p.getX());
317
        arrayY.add(p.getY());
318
    }
319

    
320
    /**
321
     * <p>
322
     * Sets a tool listener to work with the <code>MapControl</code> using this
323
     * behavior.</p>
324
     *
325
     * @param listener a <code>EllipseListener</code> object for this behavior
326
     */
327
    public void setListener(ToolListener listener) {
328
        this.listener = (EllipseListener) listener;
329
    }
330

    
331
    /*
332
         * (non-Javadoc)
333
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
334
     */
335
    @Override
336
    public ToolListener getListener() {
337
        return listener;
338
    }
339
}