Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libCq CMS for java.old / src / org / cresques / px / PxContour.java @ 83

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

    
10
import org.cresques.geo.Hoja;
11
import org.cresques.geo.Polygon2D;
12
import org.cresques.geo.Projected;
13
import org.cresques.geo.Projection;
14
import org.cresques.geo.ReProjection;
15
import org.cresques.geo.ViewPort;
16

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

    
60
        private void setContour(Point2D [] v) {
61
                extent = new Extent();
62
                polygon = new Polygon2D();
63
                for (int i=0; i<v.length; i++) {
64
                        polygon.addPoint(v[i]);
65
                        extent.add(v[i]);
66
                }
67
        }
68
        
69
        public String getName() { return name; }
70
        public Color c() {return pc;}
71
        public Color c(Color color) {this.pc = color; return pc;}
72

    
73
        public Color fillColor() {return fColor;}
74
        public Color fillColor(Color c) {fColor = c; return fColor;}
75
        
76
        public void setColor(Color color) {pc = color;}
77
        public Color getColor() { return pc;        }
78

    
79
        public void setFillColor(Color color) {fColor = color;}
80
        public Color getFillColor() { return fColor; }
81
        
82
        public void reProject(ReProjection rp) {
83
                Polygon2D savePol = polygon;
84

    
85
                polygon = new Polygon2D();
86
                extent = new Extent();
87
                Point2D ptDest = null;
88
                for (int i=0; i<savePol.size(); i++) {
89
                        ptDest = rp.getPDest().createPoint(0.0,0.0);
90
                        rp.convert((Point2D) savePol.get(i), ptDest);
91
                        polygon.addPoint(ptDest);
92
                        extent.add(ptDest);
93
                }
94
                setProjection(rp.getPDest());
95
        }
96

    
97
        public void draw(Graphics2D g, ViewPort vp, ReProjection rp) {
98
                Projection saveProj = proj;
99
                Polygon2D savePol = polygon;
100
                Extent saveExt = extent;
101
                
102
                reProject(rp);
103
                draw(g, vp);
104
                
105
                polygon = savePol;
106
                extent = saveExt;
107
                proj = saveProj;
108
        }
109
        
110
        public void draw(Graphics2D g, ViewPort vp) {
111
                //AffineTransform msave=g.getTransform();
112
                //g.setTransform(vp.mat);
113
                // relleno el marco si es preciso
114
                if (fColor != null) {
115
                        g.setColor(fColor);
116
                        if (polygon == null)
117
                                g.fillRect((int)extent.minX(), (int)extent.minY(),
118
                                        (int)extent.width(), (int)extent.height());
119
                        else
120
                                polygon.fill(g, vp);
121
                }
122
                // pinto el marco si es preciso
123
                if (pc != null) g.setColor(pc);
124
                if (polygon != null)
125
                        polygon.draw(g, vp);
126
                else
127
                        g.drawRect((int)extent.minX(), (int)extent.minY(),
128
                                (int)extent.width(), (int)extent.height());
129
                //g.setTransform(msave);
130
                // Pinto el name
131
                FontMetrics fm = g.getFontMetrics();
132
                int w = fm.stringWidth(name), h = fm.getAscent();
133
                Point2D.Double pt = new Point2D.Double((extent.minX()+extent.width()/2.0), 
134
                        (extent.minY()+extent.height()/2.0));
135
                try {
136
                        Point2D.Double min = new Point2D.Double(extent.minX(), extent.minY());
137
                        Point2D.Double max = new Point2D.Double(extent.maxX(), extent.minY());
138
                        vp.mat.transform(min, min);
139
                        vp.mat.transform(max, max);
140
                        if ((max.getX()-min.getX()) < w) return;
141
                        vp.mat.transform(pt, pt);
142
                        //if ((int)(pt2.getY()-pt1.getY()) >= g.getFontMetrics().getAscent())
143
                        g.drawString(name, (int) pt.getX()-w/2, (int)pt.getY()+h/2) ;
144
                        
145
                } catch (Exception e) {
146
                        e.printStackTrace();
147
                }
148
        }
149
}