Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libDXF / src / org / cresques / px / PxContour.java @ 27517

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 java.awt.Color;
27
import java.awt.FontMetrics;
28
import java.awt.Graphics2D;
29
import java.awt.geom.Point2D;
30
import java.util.Vector;
31

    
32
import org.cresques.cts.ICoordTrans;
33
import org.cresques.cts.IProjection;
34
import org.cresques.geo.Polygon2D;
35
import org.cresques.geo.Projected;
36
import org.cresques.geo.ViewPortData;
37
import org.cresques.geo.cover.Hoja;
38

    
39

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

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

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

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

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

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

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

    
85
    public IProjection getProjection() {
86
        return proj;
87
    }
88

    
89
    public void setProjection(IProjection p) {
90
        proj = p;
91
    }
92

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

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

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

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

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

    
117
        return v;
118
    }
119

    
120
    public String getName() {
121
        return name;
122
    }
123

    
124
    public Color c() {
125
        return pc;
126
    }
127

    
128
    public Color c(Color color) {
129
        this.pc = color;
130

    
131
        return pc;
132
    }
133

    
134
    public Color fillColor() {
135
        return fColor;
136
    }
137

    
138
    public Color fillColor(Color c) {
139
        fColor = c;
140

    
141
        return fColor;
142
    }
143

    
144
    public void setColor(Color color) {
145
        pc = color;
146
    }
147

    
148
    public Color getColor() {
149
        return pc;
150
    }
151

    
152
    public void setFillColor(Color color) {
153
        fColor = color;
154
    }
155

    
156
    public Color getFillColor() {
157
        return fColor;
158
    }
159

    
160
    public void reProject(ICoordTrans rp) {
161
        Polygon2D savePol = polygon;
162

    
163
        polygon = new Polygon2D();
164
        extent = new Extent();
165

    
166
        Point2D ptDest = null;
167

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

    
175
        setProjection(rp.getPDest());
176
    }
177

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

    
183
        reProject(rp);
184
        draw(g, vp);
185

    
186
        polygon = savePol;
187
        extent = saveExt;
188
        proj = saveProj;
189
    }
190

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

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

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

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

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

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

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

    
238
            vp.mat.transform(pt, pt);
239

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