001 /*
002 * Rect.java
003 *
004 *
005 * The Salamander Project - 2D and 3D graphics libraries in Java
006 * Copyright (C) 2004 Mark McKay
007 *
008 * This library is free software; you can redistribute it and/or
009 * modify it under the terms of the GNU Lesser General Public
010 * License as published by the Free Software Foundation; either
011 * version 2.1 of the License, or (at your option) any later version.
012 *
013 * This library is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016 * Lesser General Public License for more details.
017 *
018 * You should have received a copy of the GNU Lesser General Public
019 * License along with this library; if not, write to the Free Software
020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021 *
022 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
023 * projects can be found at http://www.kitfox.com
024 *
025 * Created on January 26, 2004, 5:25 PM
026 */
027
028 package com.kitfox.svg;
029
030 import com.kitfox.svg.xml.StyleAttribute;
031
032 import java.awt.*;
033 import java.awt.geom.*;
034 import java.io.IOException;
035 import java.io.ObjectInputStream;
036 import java.io.ObjectOutputStream;
037
038 /**
039 * @author Mark McKay
040 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
041 */
042 public class Rect extends ShapeElement {
043
044 float x = 0f;
045 float y = 0f;
046 float width = 0f;
047 float height = 0f;
048 float rx = 0f;
049 float ry = 0f;
050
051 RectangularShape rect;
052
053 /** Creates a new instance of Rect */
054 public Rect() {
055 }
056
057 private void writeObject(ObjectOutputStream out) throws IOException
058 {
059 out.writeFloat(x);
060 out.writeFloat(y);
061 out.writeFloat(width);
062 out.writeFloat(height);
063 out.writeFloat(rx);
064 out.writeFloat(ry);
065 }
066
067 private void readObject(ObjectInputStream in) throws IOException
068 {
069 x = in.readFloat();
070 y = in.readFloat();
071 width = in.readFloat();
072 height = in.readFloat();
073 rx = in.readFloat();
074 ry = in.readFloat();
075
076 if (rx == 0f && ry == 0f)
077 {
078 rect = new Rectangle2D.Float(x, y, width, height);
079 }
080 else
081 {
082 rect = new RoundRectangle2D.Float(x, y, width, height, rx * 2, ry * 2);
083 }
084 }
085
086 /*
087 public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
088 {
089 //Load style string
090 super.loaderStartElement(helper, attrs, parent);
091
092 String x = attrs.getValue("x");
093 String y = attrs.getValue("y");
094 String width = attrs.getValue("width");
095 String height = attrs.getValue("height");
096 String rx = attrs.getValue("rx");
097 String ry = attrs.getValue("ry");
098
099 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 }