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