Statistics
| Revision:

svn-gvsig-desktop / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / px / PxContour.java @ 1733

History | View | Annotate | Download (4.62 KB)

1
package org.cresques.px;
2

    
3
import java.awt.Color;
4
//import java.awt.Polygon;
5
import java.awt.Graphics2D;
6
import java.awt.FontMetrics;
7

    
8
import java.awt.geom.Point2D;
9
import java.util.Vector;
10

    
11
import org.cresques.cts.ICoordTrans;
12
import org.cresques.cts.IProjection;
13
import org.cresques.geo.Polygon2D;
14
import org.cresques.geo.Projected;
15
import org.cresques.geo.Projection;
16
import org.cresques.geo.ReProjection;
17
import org.cresques.geo.ViewPortData;
18
import org.cresques.geo.cover.Hoja;
19

    
20
public class PxContour extends PxObj implements Projected {
21
        IProjection proj = null;
22
        final static Color colorBase  = new Color(  0,  64, 128, 255);//Color(255,214,132,255);
23
        final static Color fColorBase = new Color( 64, 128, 192, 255);//Color(255,222,165,64);
24
        protected String name;
25
        protected String fName;
26
        private Color fColor = fColorBase;
27
        private Color pc = colorBase;
28
        private Polygon2D polygon = null;
29
        
30
        public void _PxContour(Point2D pt1, Point2D pt2, String fName, String name) {
31
                this.fName = fName;
32
                this.name = name;
33
                extent = new Extent(pt1, pt2);
34
        }
35
        public PxContour(Extent e, String fName, String name, IProjection proj) {
36
                this.fName = fName;
37
                this.name = name;
38
                this.proj = proj;
39
                Point2D [] v = new Point2D[4]; 
40
                v[0] = proj.createPoint(e.minX(), e.minY());
41
                v[1] = proj.createPoint(e.maxX(), e.minY());
42
                v[2] = proj.createPoint(e.maxX(), e.maxY());
43
                v[3] = proj.createPoint(e.minX(), e.maxY());
44
                setContour(v);
45
        }
46
        public void _PxContour(Point2D[] v, String fName, String name) {
47
                this.fName = fName;
48
                this.name = name;
49
                setContour(v);
50
        }
51
        public PxContour(Hoja h) {
52
                name = h.getCode();
53
                setContour(h.getVertex());
54
        }
55
        public PxContour(Point2D[] v, String name) {
56
                this.name = name;
57
                setContour(v);
58
        }
59
        
60
        public IProjection getProjection() { return proj; }
61
        public void setProjection(IProjection p) { proj = p; }
62

    
63
        private void setContour(Point2D [] v) {
64
                extent = new Extent();
65
                polygon = new Polygon2D();
66
                for (int i=0; i<v.length; i++) {
67
                        polygon.addPoint(v[i]);
68
                        extent.add(v[i]);
69
                }
70
        }
71
        /**
72
         * Vertices de un contorno.
73
         * @return
74
         */
75
        public Vector getVertex() {
76
                return polygon;
77
        }
78
        
79
        public Point2D [] getPtList() {
80
                Point2D [] v = new Point2D[polygon.size()];
81
                for (int i=0; i<polygon.size(); i++)
82
                        v[i] = (Point2D) polygon.get(i);
83
                return v;
84
        }
85
        public String getName() { return name; }
86
        public Color c() {return pc;}
87
        public Color c(Color color) {this.pc = color; return pc;}
88

    
89
        public Color fillColor() {return fColor;}
90
        public Color fillColor(Color c) {fColor = c; return fColor;}
91
        
92
        public void setColor(Color color) {pc = color;}
93
        public Color getColor() { return pc;        }
94

    
95
        public void setFillColor(Color color) {fColor = color;}
96
        public Color getFillColor() { return fColor; }
97
        
98
        public void reProject(ICoordTrans rp) {
99
                Polygon2D savePol = polygon;
100

    
101
                polygon = new Polygon2D();
102
                extent = new Extent();
103
                Point2D ptDest = null;
104
                for (int i=0; i<savePol.size(); i++) {
105
                        ptDest = rp.getPDest().createPoint(0.0,0.0);
106
                        ptDest = rp.convert((Point2D) savePol.get(i), ptDest);
107
                        polygon.addPoint(ptDest);
108
                        extent.add(ptDest);
109
                }
110
                setProjection(rp.getPDest());
111
        }
112

    
113
        public void draw(Graphics2D g, ViewPortData vp, ICoordTrans rp) {
114
                IProjection saveProj = proj;
115
                Polygon2D savePol = polygon;
116
                Extent saveExt = extent;
117
                
118
                reProject(rp);
119
                draw(g, vp);
120
                
121
                polygon = savePol;
122
                extent = saveExt;
123
                proj = saveProj;
124
        }
125
        
126
        public void draw(Graphics2D g, ViewPortData vp) {
127
                //AffineTransform msave=g.getTransform();
128
                //g.setTransform(vp.mat);
129
                // relleno el marco si es preciso
130
                if (fColor != null) {
131
                        g.setColor(fColor);
132
                        if (polygon == null)
133
                                g.fillRect((int)extent.minX(), (int)extent.minY(),
134
                                        (int)extent.width(), (int)extent.height());
135
                        else
136
                                polygon.fill(g, vp);
137
                }
138
                // pinto el marco si es preciso
139
                if (pc != null) g.setColor(pc);
140
                if (polygon != null)
141
                        polygon.draw(g, vp);
142
                else
143
                        g.drawRect((int)extent.minX(), (int)extent.minY(),
144
                                (int)extent.width(), (int)extent.height());
145
                //g.setTransform(msave);
146
                // Pinto el name
147
                FontMetrics fm = g.getFontMetrics();
148
                int w = fm.stringWidth(name), h = fm.getAscent();
149
                Point2D.Double pt = new Point2D.Double((extent.minX()+extent.width()/2.0), 
150
                        (extent.minY()+extent.height()/2.0));
151
                try {
152
                        Point2D.Double min = new Point2D.Double(extent.minX(), extent.minY());
153
                        Point2D.Double max = new Point2D.Double(extent.maxX(), extent.minY());
154
                        vp.mat.transform(min, min);
155
                        vp.mat.transform(max, max);
156
                        if ((max.getX()-min.getX()) < w) return;
157
                        vp.mat.transform(pt, pt);
158
                        //if ((int)(pt2.getY()-pt1.getY()) >= g.getFontMetrics().getAscent())
159
                        g.drawString(name, (int) pt.getX()-w/2, (int)pt.getY()+h/2) ;
160
                        
161
                } catch (Exception e) {
162
                        e.printStackTrace();
163
                }
164
        }
165
}