Revision 2669 branches/CqCMSDvp/libraries/libCq CMS for java.old/src/org/cresques/geo/Polygon2D.java

View differences:

Polygon2D.java
1 1
/*
2 2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 * 
4
 * Copyright (C) 2004-5. 
5 3
 *
4
 * Copyright (C) 2004-5.
5
 *
6 6
 * This program is free software; you can redistribute it and/or
7 7
 * modify it under the terms of the GNU General Public License
8 8
 * as published by the Free Software Foundation; either version 2
......
18 18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19 19
 *
20 20
 * For more information, contact:
21
 * 
21
 *
22 22
 * cresques@gmail.com
23 23
 */
24 24
package org.cresques.geo;
......
26 26
import java.awt.Graphics2D;
27 27
import java.awt.geom.GeneralPath;
28 28
import java.awt.geom.Point2D;
29

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

  
33

  
32 34
/**
33 35
 * Clase que representa un pol?gono 2D
34 36
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
35 37
 */
36 38
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
}
39
    GeneralPath gp = null;
93 40

  
41
    public Polygon2D() {
42
        super();
43
        gp = null;
44
    }
94 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
}

Also available in: Unified diff