Statistics
| Revision:

root / trunk / libraries / libCq_CMS_praster / src / org / cresques / px / PxContour.java @ 8026

History | View | Annotate | Download (6.59 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 org.cresques.cts.ICoordTrans;
27
import org.cresques.cts.IProjection;
28

    
29
import org.cresques.geo.Polygon2D;
30
import org.cresques.geo.Projected;
31
import org.cresques.geo.ViewPortData;
32
import org.cresques.geo.cover.Hoja;
33

    
34
import java.awt.Color;
35
import java.awt.FontMetrics;
36
import java.awt.Graphics2D;
37
import java.awt.geom.Point2D;
38

    
39
import java.util.Vector;
40

    
41

    
42
public class PxContour extends PxObj implements Projected {
43
    final static Color colorBase = new Color(0, 64, 128, 255); //Color(255,214,132,255);
44
    final static Color fColorBase = new Color(64, 128, 192, 255); //Color(255,222,165,64);
45
    IProjection proj = null;
46
    protected String name;
47
    protected String fName;
48
    private Color fColor = fColorBase;
49
    private Color pc = colorBase;
50
    private Polygon2D polygon = null;
51

    
52
    public PxContour(Extent e, String fName, String name, IProjection proj) {
53
        this.fName = fName;
54
        this.name = name;
55
        this.proj = proj;
56

    
57
        Point2D[] v = new Point2D[4];
58
        v[0] = proj.createPoint(e.minX(), e.minY());
59
        v[1] = proj.createPoint(e.maxX(), e.minY());
60
        v[2] = proj.createPoint(e.maxX(), e.maxY());
61
        v[3] = proj.createPoint(e.minX(), e.maxY());
62
        setContour(v);
63
    }
64

    
65
    public PxContour(Hoja h) {
66
        name = h.getCode();
67
        setContour(h.getVertex());
68
    }
69

    
70
    public PxContour(Point2D[] v, String name) {
71
        this.name = name;
72
        setContour(v);
73
    }
74

    
75
    public void _PxContour(Point2D pt1, Point2D pt2, String fName, String name) {
76
        this.fName = fName;
77
        this.name = name;
78
        extent = new Extent(pt1, pt2);
79
    }
80

    
81
    public void _PxContour(Point2D[] v, String fName, String name) {
82
        this.fName = fName;
83
        this.name = name;
84
        setContour(v);
85
    }
86

    
87
    public IProjection getProjection() {
88
        return proj;
89
    }
90

    
91
    public void setProjection(IProjection p) {
92
        proj = p;
93
    }
94

    
95
    private void setContour(Point2D[] v) {
96
        extent = new Extent();
97
        polygon = new Polygon2D();
98

    
99
        for (int i = 0; i < v.length; i++) {
100
            polygon.addPoint(v[i]);
101
            extent.add(v[i]);
102
        }
103
    }
104

    
105
    /**
106
     * Vertices de un contorno.
107
     * @return
108
     */
109
    public Vector getVertex() {
110
        return polygon;
111
    }
112

    
113
    public Point2D[] getPtList() {
114
        Point2D[] v = new Point2D[polygon.size()];
115

    
116
        for (int i = 0; i < polygon.size(); i++)
117
            v[i] = (Point2D) polygon.get(i);
118

    
119
        return v;
120
    }
121

    
122
    public String getName() {
123
        return name;
124
    }
125

    
126
    public Color c() {
127
        return pc;
128
    }
129

    
130
    public Color c(Color color) {
131
        this.pc = color;
132

    
133
        return pc;
134
    }
135

    
136
    public Color fillColor() {
137
        return fColor;
138
    }
139

    
140
    public Color fillColor(Color c) {
141
        fColor = c;
142

    
143
        return fColor;
144
    }
145

    
146
    public void setColor(Color color) {
147
        pc = color;
148
    }
149

    
150
    public Color getColor() {
151
        return pc;
152
    }
153

    
154
    public void setFillColor(Color color) {
155
        fColor = color;
156
    }
157

    
158
    public Color getFillColor() {
159
        return fColor;
160
    }
161

    
162
    public void reProject(ICoordTrans rp) {
163
        Polygon2D savePol = polygon;
164

    
165
        polygon = new Polygon2D();
166
        extent = new Extent();
167

    
168
        Point2D ptDest = null;
169

    
170
        for (int i = 0; i < savePol.size(); i++) {
171
            ptDest = rp.getPDest().createPoint(0.0, 0.0);
172
            ptDest = rp.convert((Point2D) savePol.get(i), ptDest);
173
            polygon.addPoint(ptDest);
174
            extent.add(ptDest);
175
        }
176

    
177
        setProjection(rp.getPDest());
178
    }
179

    
180
    public void draw(Graphics2D g, ViewPortData vp, ICoordTrans rp) {
181
        IProjection saveProj = proj;
182
        Polygon2D savePol = polygon;
183
        Extent saveExt = extent;
184

    
185
        reProject(rp);
186
        draw(g, vp);
187

    
188
        polygon = savePol;
189
        extent = saveExt;
190
        proj = saveProj;
191
    }
192

    
193
    public void draw(Graphics2D g, ViewPortData vp) {
194
        //AffineTransform msave=g.getTransform();
195
        //g.setTransform(vp.mat);
196
        // relleno el marco si es preciso
197
        if (fColor != null) {
198
            g.setColor(fColor);
199

    
200
            if (polygon == null) {
201
                g.fillRect((int) extent.minX(), (int) extent.minY(),
202
                           (int) extent.width(), (int) extent.height());
203
            } else {
204
                polygon.fill(g, vp);
205
            }
206
        }
207

    
208
        // pinto el marco si es preciso
209
        if (pc != null) {
210
            g.setColor(pc);
211
        }
212

    
213
        if (polygon != null) {
214
            polygon.draw(g, vp);
215
        } else {
216
            g.drawRect((int) extent.minX(), (int) extent.minY(),
217
                       (int) extent.width(), (int) extent.height());
218
        }
219

    
220
        //g.setTransform(msave);
221
        // Pinto el name
222
        FontMetrics fm = g.getFontMetrics();
223
        int w = fm.stringWidth(name);
224
        int h = fm.getAscent();
225
        Point2D.Double pt = new Point2D.Double((extent.minX() +
226
                                               (extent.width() / 2.0)),
227
                                               (extent.minY() +
228
                                               (extent.height() / 2.0)));
229

    
230
        try {
231
            Point2D.Double min = new Point2D.Double(extent.minX(), extent.minY());
232
            Point2D.Double max = new Point2D.Double(extent.maxX(), extent.minY());
233
            vp.mat.transform(min, min);
234
            vp.mat.transform(max, max);
235

    
236
            if ((max.getX() - min.getX()) < w) {
237
                return;
238
            }
239

    
240
            vp.mat.transform(pt, pt);
241

    
242
            //if ((int)(pt2.getY()-pt1.getY()) >= g.getFontMetrics().getAscent())
243
            g.drawString(name, (int) pt.getX() - (w / 2),
244
                         (int) pt.getY() + (h / 2));
245
        } catch (Exception e) {
246
            e.printStackTrace();
247
        }
248
    }
249
}