source: josm/trunk/src/com/kitfox/svg/Rect.java@ 4256

Last change on this file since 4256 was 4256, checked in by bastiK, 13 years ago

see #6560 - basic svg support, includes kitfox svgsalamander, r 98, patched

File size: 7.6 KB
Line 
1/*
2 * Rect.java
3 *
4 *
5 * The Salamander Project - 2D and 3D graphics libraries in Java
6 * Copyright (C) 2004 Mark McKay
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
23 * projects can be found at http://www.kitfox.com
24 *
25 * Created on January 26, 2004, 5:25 PM
26 */
27
28package com.kitfox.svg;
29
30import com.kitfox.svg.xml.StyleAttribute;
31
32import java.awt.*;
33import java.awt.geom.*;
34import java.io.IOException;
35import java.io.ObjectInputStream;
36import java.io.ObjectOutputStream;
37
38/**
39 * @author Mark McKay
40 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
41 */
42public class Rect extends ShapeElement {
43
44 float x = 0f;
45 float y = 0f;
46 float width = 0f;
47 float height = 0f;
48 float rx = 0f;
49 float ry = 0f;
50
51 RectangularShape rect;
52
53 /** Creates a new instance of Rect */
54 public Rect() {
55 }
56
57 private void writeObject(ObjectOutputStream out) throws IOException
58 {
59 out.writeFloat(x);
60 out.writeFloat(y);
61 out.writeFloat(width);
62 out.writeFloat(height);
63 out.writeFloat(rx);
64 out.writeFloat(ry);
65 }
66
67 private void readObject(ObjectInputStream in) throws IOException
68 {
69 x = in.readFloat();
70 y = in.readFloat();
71 width = in.readFloat();
72 height = in.readFloat();
73 rx = in.readFloat();
74 ry = in.readFloat();
75
76 if (rx == 0f && ry == 0f)
77 {
78 rect = new Rectangle2D.Float(x, y, width, height);
79 }
80 else
81 {
82 rect = new RoundRectangle2D.Float(x, y, width, height, rx * 2, ry * 2);
83 }
84 }
85
86 /*
87 public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
88 {
89 //Load style string
90 super.loaderStartElement(helper, attrs, parent);
91
92 String x = attrs.getValue("x");
93 String y = attrs.getValue("y");
94 String width = attrs.getValue("width");
95 String height = attrs.getValue("height");
96 String rx = attrs.getValue("rx");
97 String ry = attrs.getValue("ry");
98
99 if (rx == null) rx = ry;
100 if (ry == null) ry = rx;
101
102 this.x = XMLParseUtil.parseFloat(x);
103 this.y = XMLParseUtil.parseFloat(y);
104 this.width = XMLParseUtil.parseFloat(width);
105 this.height = XMLParseUtil.parseFloat(height);
106 if (rx != null)
107 {
108 this.rx = XMLParseUtil.parseFloat(rx);
109 this.ry = XMLParseUtil.parseFloat(ry);
110 }
111
112 build();
113// setBounds(this.x, this.y, this.width, this.height);
114 }
115*/
116
117 protected void build() throws SVGException
118 {
119 super.build();
120
121 StyleAttribute sty = new StyleAttribute();
122
123// SVGElement parent = this.getParent();
124// if (parent instanceof RenderableElement)
125// {
126// RenderableElement re = (RenderableElement)parent;
127// Rectangle2D bounds = re.getBoundingBox();
128// bounds = null;
129// }
130
131
132 if (getPres(sty.setName("x"))) x = sty.getFloatValueWithUnits();
133
134 if (getPres(sty.setName("y"))) y = sty.getFloatValueWithUnits();
135
136 if (getPres(sty.setName("width"))) width = sty.getFloatValueWithUnits();
137
138 if (getPres(sty.setName("height"))) height = sty.getFloatValueWithUnits();
139
140 boolean rxSet = false;
141 if (getPres(sty.setName("rx"))) { rx = sty.getFloatValueWithUnits(); rxSet = true; }
142
143 boolean rySet = false;
144 if (getPres(sty.setName("ry"))) { ry = sty.getFloatValueWithUnits(); rySet = true; }
145
146 if (!rxSet) rx = ry;
147 if (!rySet) ry = rx;
148
149
150 if (rx == 0f && ry == 0f)
151 {
152 rect = new Rectangle2D.Float(x, y, width, height);
153 }
154 else
155 {
156 rect = new RoundRectangle2D.Float(x, y, width, height, rx * 2, ry * 2);
157 }
158 }
159
160 public void render(Graphics2D g) throws SVGException
161 {
162 beginLayer(g);
163 renderShape(g, rect);
164 finishLayer(g);
165 }
166
167 public Shape getShape()
168 {
169 return shapeToParent(rect);
170 }
171
172 public Rectangle2D getBoundingBox() throws SVGException
173 {
174 return boundsToParent(includeStrokeInBounds(rect.getBounds2D()));
175 }
176
177 /**
178 * Updates all attributes in this diagram associated with a time event.
179 * Ie, all attributes with track information.
180 * @return - true if this node has changed state as a result of the time
181 * update
182 */
183 public boolean updateTime(double curTime) throws SVGException
184 {
185// if (trackManager.getNumTracks() == 0) return false;
186 boolean changeState = super.updateTime(curTime);
187
188 //Get current values for parameters
189 StyleAttribute sty = new StyleAttribute();
190 boolean shapeChange = false;
191
192 if (getPres(sty.setName("x")))
193 {
194 float newVal = sty.getFloatValueWithUnits();
195 if (newVal != x)
196 {
197 x = newVal;
198 shapeChange = true;
199 }
200 }
201
202 if (getPres(sty.setName("y")))
203 {
204 float newVal = sty.getFloatValueWithUnits();
205 if (newVal != y)
206 {
207 y = newVal;
208 shapeChange = true;
209 }
210 }
211
212 if (getPres(sty.setName("width")))
213 {
214 float newVal = sty.getFloatValueWithUnits();
215 if (newVal != width)
216 {
217 width = newVal;
218 shapeChange = true;
219 }
220 }
221
222 if (getPres(sty.setName("height")))
223 {
224 float newVal = sty.getFloatValueWithUnits();
225 if (newVal != height)
226 {
227 height = newVal;
228 shapeChange = true;
229 }
230 }
231
232 if (getPres(sty.setName("rx")))
233 {
234 float newVal = sty.getFloatValueWithUnits();
235 if (newVal != rx)
236 {
237 rx = newVal;
238 shapeChange = true;
239 }
240 }
241
242 if (getPres(sty.setName("ry")))
243 {
244 float newVal = sty.getFloatValueWithUnits();
245 if (newVal != ry)
246 {
247 ry = newVal;
248 shapeChange = true;
249 }
250 }
251
252 if (shapeChange)
253 {
254 build();
255// if (rx == 0f && ry == 0f)
256// {
257// rect = new Rectangle2D.Float(x, y, width, height);
258// }
259// else
260// {
261// rect = new RoundRectangle2D.Float(x, y, width, height, rx * 2, ry * 2);
262// }
263// return true;
264 }
265
266 return changeState || shapeChange;
267 }
268}
Note: See TracBrowser for help on using the repository browser.