Statistics
| Revision:

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

History | View | Annotate | Download (5.5 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.px;
25

    
26
import java.awt.Color;
27
//import java.awt.Polygon;
28
import java.awt.Graphics2D;
29
import java.awt.FontMetrics;
30

    
31
import java.awt.geom.Point2D;
32
import java.util.Vector;
33

    
34
import org.cresques.cts.ICoordTrans;
35
import org.cresques.cts.IProjection;
36
import org.cresques.geo.Polygon2D;
37
import org.cresques.geo.Projected;
38
import org.cresques.geo.Projection;
39
import org.cresques.geo.ReProjection;
40
import org.cresques.geo.ViewPortData;
41
import org.cresques.geo.cover.Hoja;
42

    
43
public class PxContour extends PxObj implements Projected {
44
        IProjection proj = null;
45
        final static Color colorBase  = new Color(  0,  64, 128, 255);//Color(255,214,132,255);
46
        final static Color fColorBase = new Color( 64, 128, 192, 255);//Color(255,222,165,64);
47
        protected String name;
48
        protected String fName;
49
        private Color fColor = fColorBase;
50
        private Color pc = colorBase;
51
        private Polygon2D polygon = null;
52
        
53
        public void _PxContour(Point2D pt1, Point2D pt2, String fName, String name) {
54
                this.fName = fName;
55
                this.name = name;
56
                extent = new Extent(pt1, pt2);
57
        }
58
        public PxContour(Extent e, String fName, String name, IProjection proj) {
59
                this.fName = fName;
60
                this.name = name;
61
                this.proj = proj;
62
                Point2D [] v = new Point2D[4]; 
63
                v[0] = proj.createPoint(e.minX(), e.minY());
64
                v[1] = proj.createPoint(e.maxX(), e.minY());
65
                v[2] = proj.createPoint(e.maxX(), e.maxY());
66
                v[3] = proj.createPoint(e.minX(), e.maxY());
67
                setContour(v);
68
        }
69
        public void _PxContour(Point2D[] v, String fName, String name) {
70
                this.fName = fName;
71
                this.name = name;
72
                setContour(v);
73
        }
74
        public PxContour(Hoja h) {
75
                name = h.getCode();
76
                setContour(h.getVertex());
77
        }
78
        public PxContour(Point2D[] v, String name) {
79
                this.name = name;
80
                setContour(v);
81
        }
82
        
83
        public IProjection getProjection() { return proj; }
84
        public void setProjection(IProjection p) { proj = p; }
85

    
86
        private void setContour(Point2D [] v) {
87
                extent = new Extent();
88
                polygon = new Polygon2D();
89
                for (int i=0; i<v.length; i++) {
90
                        polygon.addPoint(v[i]);
91
                        extent.add(v[i]);
92
                }
93
        }
94
        /**
95
         * Vertices de un contorno.
96
         * @return
97
         */
98
        public Vector getVertex() {
99
                return polygon;
100
        }
101
        
102
        public Point2D [] getPtList() {
103
                Point2D [] v = new Point2D[polygon.size()];
104
                for (int i=0; i<polygon.size(); i++)
105
                        v[i] = (Point2D) polygon.get(i);
106
                return v;
107
        }
108
        public String getName() { return name; }
109
        public Color c() {return pc;}
110
        public Color c(Color color) {this.pc = color; return pc;}
111

    
112
        public Color fillColor() {return fColor;}
113
        public Color fillColor(Color c) {fColor = c; return fColor;}
114
        
115
        public void setColor(Color color) {pc = color;}
116
        public Color getColor() { return pc;        }
117

    
118
        public void setFillColor(Color color) {fColor = color;}
119
        public Color getFillColor() { return fColor; }
120
        
121
        public void reProject(ICoordTrans rp) {
122
                Polygon2D savePol = polygon;
123

    
124
                polygon = new Polygon2D();
125
                extent = new Extent();
126
                Point2D ptDest = null;
127
                for (int i=0; i<savePol.size(); i++) {
128
                        ptDest = rp.getPDest().createPoint(0.0,0.0);
129
                        ptDest = rp.convert((Point2D) savePol.get(i), ptDest);
130
                        polygon.addPoint(ptDest);
131
                        extent.add(ptDest);
132
                }
133
                setProjection(rp.getPDest());
134
        }
135

    
136
        public void draw(Graphics2D g, ViewPortData vp, ICoordTrans rp) {
137
                IProjection saveProj = proj;
138
                Polygon2D savePol = polygon;
139
                Extent saveExt = extent;
140
                
141
                reProject(rp);
142
                draw(g, vp);
143
                
144
                polygon = savePol;
145
                extent = saveExt;
146
                proj = saveProj;
147
        }
148
        
149
        public void draw(Graphics2D g, ViewPortData vp) {
150
                //AffineTransform msave=g.getTransform();
151
                //g.setTransform(vp.mat);
152
                // relleno el marco si es preciso
153
                if (fColor != null) {
154
                        g.setColor(fColor);
155
                        if (polygon == null)
156
                                g.fillRect((int)extent.minX(), (int)extent.minY(),
157
                                        (int)extent.width(), (int)extent.height());
158
                        else
159
                                polygon.fill(g, vp);
160
                }
161
                // pinto el marco si es preciso
162
                if (pc != null) g.setColor(pc);
163
                if (polygon != null)
164
                        polygon.draw(g, vp);
165
                else
166
                        g.drawRect((int)extent.minX(), (int)extent.minY(),
167
                                (int)extent.width(), (int)extent.height());
168
                //g.setTransform(msave);
169
                // Pinto el name
170
                FontMetrics fm = g.getFontMetrics();
171
                int w = fm.stringWidth(name), h = fm.getAscent();
172
                Point2D.Double pt = new Point2D.Double((extent.minX()+extent.width()/2.0), 
173
                        (extent.minY()+extent.height()/2.0));
174
                try {
175
                        Point2D.Double min = new Point2D.Double(extent.minX(), extent.minY());
176
                        Point2D.Double max = new Point2D.Double(extent.maxX(), extent.minY());
177
                        vp.mat.transform(min, min);
178
                        vp.mat.transform(max, max);
179
                        if ((max.getX()-min.getX()) < w) return;
180
                        vp.mat.transform(pt, pt);
181
                        //if ((int)(pt2.getY()-pt1.getY()) >= g.getFontMetrics().getAscent())
182
                        g.drawString(name, (int) pt.getX()-w/2, (int)pt.getY()+h/2) ;
183
                        
184
                } catch (Exception e) {
185
                        e.printStackTrace();
186
                }
187
        }
188
}