Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1006 / libraries / libCq CMS for java.old / src / org / cresques / px / dxf / DxfEllipse.java @ 12458

History | View | Annotate | Download (7.99 KB)

1 2809 nacho
/*
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.dxf;
25
26
import org.cresques.cts.ICoordTrans;
27
import org.cresques.cts.IProjection;
28
29
import org.cresques.geo.ViewPortData;
30
31
import org.cresques.io.DxfGroup;
32
33
import org.cresques.px.Extent;
34
35
import java.awt.Color;
36
import java.awt.Graphics2D;
37
import java.awt.geom.GeneralPath;
38
import java.awt.geom.Point2D;
39
40
41
/**
42
 * Entidad ELLIPSE de un fichero DXF.
43
 * @author jmorell
44
 */
45
public class DxfEllipse extends DxfEntity {
46
    final static Color baseColor = new Color(69, 106, 121);
47
48
    //Vector points = null;
49
    GeneralPath gp = null;
50
    boolean closed = true;
51
    Point2D[] pts;
52
    private double minorAxisLength;
53
    private Point2D center;
54
    private double minorToMajorAxisRatio;
55
    private Color color = baseColor; //Color(255,214,132,255);
56
57
    /**
58
     * Constructor de DxfEllipse.
59
     * @param proj, proyecci?n cartogr?fica en la que se encuentra el DxfEllipse.
60
     * @param layer, capa del DXF en la que se encuentra el DxfEllipse.
61
     * @param pt1, primer punto del semieje mayor.
62
     * @param pt2, segundo punto del semieje mayor.
63
     * @param minorAxisLength, longitud del semieje menor.
64
     */
65
    public DxfEllipse(IProjection proj, DxfLayer layer, Point2D pt1,
66
                      Point2D pt2, double minorAxisLength) {
67
        super(proj, layer);
68
        pts = new Point2D[2];
69
        pts[0] = pt1;
70
        pts[1] = pt2;
71
        this.minorAxisLength = minorAxisLength;
72
        extent = new Extent();
73
74
        for (int i = 0; i < pts.length; i++) {
75
            extent.add(pts[i]);
76
        }
77
78
        center = new Point2D.Double((pts[0].getX() + pts[1].getX()) / 2.0,
79
                                    (pts[0].getY() + pts[1].getY()) / 2.0);
80
81
        double majorAxisLength = pt1.distance(pt2);
82
83
        //System.out.println("minorAxisLength = " + minorAxisLength);
84
        //System.out.println("majorAxisLength = " + majorAxisLength);
85
        minorToMajorAxisRatio = minorAxisLength / majorAxisLength;
86
    }
87
88
    /**
89
     * Devuelve el color del DxfEllipse.
90
     * @return Color
91
     */
92
    public Color c() {
93
        return color;
94
    }
95
96
    /**
97
     * Establece el color del DxfEllipse.
98
     * @param color
99
     * @return Color
100
     */
101
    public Color c(Color color) {
102
        this.color = color;
103
104
        return color;
105
    }
106
107
    /**
108
     * Permite reproyectar un DxfEllipse dado un conjunto de coordenadas de transformaci?n.
109
     * @param rp, coordenadas de transformaci?n.
110
     */
111
    public void reProject(ICoordTrans rp) {
112
        Point2D[] savePts = pts;
113
114
        pts = new Point2D[savePts.length];
115
        extent = new Extent();
116
117
        Point2D ptDest = null;
118
119
        for (int i = 0; i < savePts.length; i++) {
120
            ptDest = rp.getPDest().createPoint(0.0, 0.0);
121
            ptDest = rp.convert((Point2D) savePts[i], ptDest);
122
            this.pts[i] = ptDest;
123
            extent.add(ptDest);
124
        }
125
126
        setProjection(rp.getPDest());
127
    }
128
129
    /**
130
     * Permite dibujar un DxfEllipse.
131
     */
132
    public void draw(Graphics2D g, ViewPortData vp) {
133
        //System.out.println("Va a pintar un circle");
134
        Color color = null;
135
136
        if (dxfColor == AcadColor.BYLAYER) {
137
            //g.setColor(layer.getColor());
138
            color = layer.getColor();
139
        } else {
140
            //g.setColor(AcadColor.getColor(dxfColor));
141
            color = AcadColor.getColor(dxfColor);
142
        }
143
144
        newGP(vp);
145
146
        if (closed) {
147
            g.setColor(new Color(color.getRed(), color.getBlue(),
148
                                 color.getGreen(), 0x80));
149
            g.fill(gp);
150
        }
151
152
        g.setColor(color);
153
        g.draw(gp);
154
    }
155
156
    /**
157
     * Permite generar un GeneralPath partiendo del array de Point2D que conforma el
158
     * DxfEllipse.
159
     * @param vp
160
     */
161
    private void newGP(ViewPortData vp) {
162
        //if (gp != null) return;
163
        gp = new GeneralPath();
164
165
        Point2D pt0 = null;
166
        Point2D pt = null;
167
        Point2D pt1 = null;
168
        Point2D.Double ptTmp = new Point2D.Double(0.0, 0.0);
169
170
        //System.out.println("pts.length = " + pts.length);
171
        for (int i = 0; i < pts.length; i++) {
172
            pt1 = (Point2D) pts[i];
173
            vp.mat.transform(pt1, ptTmp);
174
175
            if (pt0 == null) {
176
                pt0 = ptTmp;
177
                gp.moveTo((float) ptTmp.getX(), (float) ptTmp.getY());
178
            } else {
179
                gp.lineTo((float) ptTmp.getX(), (float) ptTmp.getY());
180
            }
181
        }
182
183
        if (closed) {
184
            gp.closePath();
185
        }
186
    }
187
188
    /**
189
     * Permite la escritura de entidades DxfEllipse en un fichero DXF2000.
190
     * @return String, la cadena que se escribir? en el fichero con la informaci?n
191
     * del DxfEllipse.
192
     */
193
    public String toDxfString() {
194
        StringBuffer sb = null;
195
        sb = new StringBuffer(DxfGroup.toString(0, "ELLIPSE"));
196
        sb.append(DxfGroup.toString(5, getHandle()));
197
        sb.append(DxfGroup.toString(100, "AcDbEntity"));
198
        sb.append(DxfGroup.toString(8, layer.getName()));
199
        sb.append(DxfGroup.toString(62, dxfColor));
200
        sb.append(DxfGroup.toString(100, "AcDbEllipse"));
201
        sb.append(DxfGroup.toString(10, getCenter().getX(), 6));
202
        sb.append(DxfGroup.toString(20, getCenter().getY(), 6));
203
        sb.append(DxfGroup.toString(30, 0.0, 6));
204
        sb.append(DxfGroup.toString(11, pts[1].getX() - getCenter().getX(), 6));
205
        sb.append(DxfGroup.toString(21, pts[1].getY() - getCenter().getY(), 6));
206
        sb.append(DxfGroup.toString(31, 0.0, 6));
207
        sb.append(DxfGroup.toString(40, getMinorToMajorAxisRatio(), 6));
208
        sb.append(DxfGroup.toString(41, 0.0, 6));
209
        sb.append(DxfGroup.toString(42, 2 * Math.PI, 6));
210
211
        return sb.toString();
212
    }
213
214
    /**
215
     * Devuelve el array de puntos que conforman el DxfEllipse.
216
     * @return Point2D[], puntos del DxfEllipse.
217
     */
218
    public Point2D[] getPts() {
219
        return pts;
220
    }
221
222
    /**
223
     * Devuelve el GeneralPath qie conforma el DxfEllipse.
224
     * @return GeneralPath del DxfEllipse.
225
     */
226
    /*public GeneralPath getGeneralPath(ViewPort vp) {
227
            newGP(vp);
228
            return (GeneralPath) gp.clone();
229
    }*/
230
231
    /**
232
     * @return Returns the minorAxisLength.
233
     */
234
    public double getMinorAxisLength() {
235
        return minorAxisLength;
236
    }
237
238
    /**
239
     * @param minorAxisLength The minorAxisLength to set.
240
     */
241
    public void setMinorAxisLength(double minorAxisLength) {
242
        this.minorAxisLength = minorAxisLength;
243
    }
244
245
    /**
246
     * @param pts The pts to set.
247
     */
248
    public void setPts(Point2D[] pts) {
249
        this.pts = pts;
250
    }
251
252
    /**
253
     * @return Returns the center.
254
     */
255
    public Point2D getCenter() {
256
        return center;
257
    }
258
259
    /**
260
     * @param center The center to set.
261
     */
262
    public void setCenter(Point2D center) {
263
        this.center = center;
264
    }
265
266
    /**
267
     * @return Returns the majorToMinorAxisRatio.
268
     */
269
    public double getMinorToMajorAxisRatio() {
270
        return minorToMajorAxisRatio;
271
    }
272
273
    /**
274
     * @param majorToMinorAxisRatio The majorToMinorAxisRatio to set.
275
     */
276
    public void setMinorToMajorAxisRatio(double majorToMinorAxisRatio) {
277
        this.minorToMajorAxisRatio = majorToMinorAxisRatio;
278
    }
279
}