Statistics
| Revision:

svn-gvsig-desktop / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / geo / Polygon2D.java @ 2312

History | View | Annotate | Download (2.28 KB)

1
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 * 
4
 * Copyright (C) 2004-5. 
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 2
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 * 
22
 * cresques@gmail.com
23
 */
24
package org.cresques.geo;
25

    
26
import java.awt.Graphics2D;
27
import java.awt.geom.GeneralPath;
28
import java.awt.geom.Point2D;
29
import java.util.Iterator;
30
import java.util.Vector;
31

    
32
/**
33
 * Clase que representa un pol?gono 2D
34
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
35
 */
36
public class Polygon2D extends Vector {
37
        GeneralPath gp = null;
38
        public Polygon2D() {
39
                super();
40
                gp = null;
41
        }
42
        
43
        /**
44
         * A?ade un vertice al po??gono
45
         * @param pt        punto 2D que representa el vertice a?adido
46
         */
47
        public void addPoint(Point2D pt) {super.add(pt);}
48
        
49
        /**
50
         * Dibuja el pol?gono
51
         * @param g        Graphics sobre el que dibuja 
52
         * @param vp        ViewPort con la vista
53
         */
54
        public void draw(Graphics2D g, ViewPortData vp) {
55
                newGP(vp);
56
                g.draw(gp);
57
                //g.draw(new Line2D.Double(pt,pt0));
58
        }
59
        
60
        /**
61
         * 
62
         * @param g
63
         * @param vp
64
         */
65
        public void fill(Graphics2D g, ViewPortData vp) {
66
                newGP(vp);
67
                g.fill(gp);
68
        }
69
        
70
        /**
71
         * 
72
         * @param vp
73
         */
74
        private void newGP(ViewPortData vp) {
75
                //if (gp != null) return;
76
                gp = new GeneralPath();
77
                Point2D pt0 = null, pt=null, pt1=null;
78
                Point2D.Double ptTmp = new Point2D.Double(0.0, 0.0);
79
                Iterator iter = iterator();
80
                while (iter.hasNext()) {
81
                        pt1 = (Point2D) iter.next();
82
                        vp.mat.transform(pt1, ptTmp);
83
                        if (pt0 == null) { 
84
                                pt0 = ptTmp;
85
                                gp.moveTo((float)ptTmp.getX(), (float)ptTmp.getY());
86
                        } else {
87
                                gp.lineTo((float)ptTmp.getX(), (float)ptTmp.getY());
88
                        }
89
                }
90
                gp.closePath();
91
        }
92
}
93

    
94