Statistics
| Revision:

root / trunk / libraries / libCq_CMS_praster / src / org / cresques / geo / Polygon2D.java @ 8026

History | View | Annotate | Download (2.6 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

    
30
import java.util.Iterator;
31
import java.util.Vector;
32

    
33

    
34
/**
35
 * Clase que representa un pol?gono 2D
36
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
37
 */
38
public class Polygon2D extends Vector {
39
    GeneralPath gp = null;
40

    
41
    public Polygon2D() {
42
        super();
43
        gp = null;
44
    }
45

    
46
    /**
47
     * A?ade un vertice al po??gono
48
     * @param pt        punto 2D que representa el vertice a?adido
49
     */
50
    public void addPoint(Point2D pt) {
51
        super.add(pt);
52
    }
53

    
54
    /**
55
     * Dibuja el pol?gono
56
     * @param g        Graphics sobre el que dibuja
57
     * @param vp        ViewPort con la vista
58
     */
59
    public void draw(Graphics2D g, ViewPortData vp) {
60
        newGP(vp);
61
        g.draw(gp);
62

    
63
        //g.draw(new Line2D.Double(pt,pt0));
64
    }
65

    
66
    /**
67
     *
68
     * @param g
69
     * @param vp
70
     */
71
    public void fill(Graphics2D g, ViewPortData vp) {
72
        newGP(vp);
73
        g.fill(gp);
74
    }
75

    
76
    /**
77
     *
78
     * @param vp
79
     */
80
    private void newGP(ViewPortData vp) {
81
        //if (gp != null) return;
82
        gp = new GeneralPath();
83

    
84
        Point2D pt0 = null;
85
        Point2D pt = null;
86
        Point2D pt1 = null;
87
        Point2D.Double ptTmp = new Point2D.Double(0.0, 0.0);
88
        Iterator iter = iterator();
89

    
90
        while (iter.hasNext()) {
91
            pt1 = (Point2D) iter.next();
92
            vp.mat.transform(pt1, ptTmp);
93

    
94
            if (pt0 == null) {
95
                pt0 = ptTmp;
96
                gp.moveTo((float) ptTmp.getX(), (float) ptTmp.getY());
97
            } else {
98
                gp.lineTo((float) ptTmp.getX(), (float) ptTmp.getY());
99
            }
100
        }
101

    
102
        gp.closePath();
103
    }
104
}