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 import com.kitfox.svg.xml.*;
032 import org.xml.sax.*;
033
034 import java.awt.*;
035 import java.awt.geom.*;
036
037 /**
038 * @author Mark McKay
039 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
040 */
041 public class Ellipse extends ShapeElement {
042
043 float cx = 0.0f;
044 float cy = 0.0f;
045 float rx = 0.0f;
046 float ry = 0.0f;
047
048 Ellipse2D.Float ellipse = new Ellipse2D.Float();
049
050 /** Creates a new instance of Rect */
051 public Ellipse() {
052 }
053 /*
054 protected void init(String idIn, Style parentStyle, String cx, String cy, String rx, String ry) {
055 super.init(idIn, parentStyle);
056
057 this.cx = parseDouble(cx);
058 this.cy = parseDouble(cy);
059 this.rx = parseDouble(rx);
060 this.ry = parseDouble(ry);
061
062 setBounds(this.cx - this.rx, this.cy - this.ry, this.rx * 2.0, this.ry * 2.0);
063 }
064 */
065 /*
066 public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
067 {
068 //Load style string
069 super.loaderStartElement(helper, attrs, parent);
070
071 String cx = attrs.getValue("cx");
072 String cy = attrs.getValue("cy");
073 String rx = attrs.getValue("rx");
074 String ry = attrs.getValue("ry");
075
076 this.cx = XMLParseUtil.parseDouble(cx);
077 this.cy = XMLParseUtil.parseDouble(cy);
078 this.rx = XMLParseUtil.parseDouble(rx);
079 this.ry = XMLParseUtil.parseDouble(ry);
080
081 build();
082 }
083 */
084
085 /*
086 public void loaderEndElement(SVGLoaderHelper helper)
087 {
088 super.loaderEndElement(helper);
089
090 build();
091 }
092 */
093
094 protected void build() throws SVGException
095 {
096 super.build();
097
098 StyleAttribute sty = new StyleAttribute();
099
100 if (getPres(sty.setName("cx"))) cx = sty.getFloatValueWithUnits();
101
102 if (getPres(sty.setName("cy"))) cy = sty.getFloatValueWithUnits();
103
104 if (getPres(sty.setName("rx"))) rx = sty.getFloatValueWithUnits();
105
106 if (getPres(sty.setName("ry"))) ry = sty.getFloatValueWithUnits();
107
108 ellipse.setFrame(cx - rx, cy - ry, rx * 2f, ry * 2f);
109 }
110
111 public void render(Graphics2D g) throws SVGException
112 {
113 beginLayer(g);
114 renderShape(g, ellipse);
115 finishLayer(g);
116 }
117
118 public Shape getShape()
119 {
120 return shapeToParent(ellipse);
121 }
122
123 public Rectangle2D getBoundingBox() throws SVGException
124 {
125 return boundsToParent(includeStrokeInBounds(ellipse.getBounds2D()));
126 }
127
128 /**
129 * Updates all attributes in this diagram associated with a time event.
130 * Ie, all attributes with track information.
131 * @return - true if this node has changed state as a result of the time
132 * update
133 */
134 public boolean updateTime(double curTime) throws SVGException
135 {
136 // if (trackManager.getNumTracks() == 0) return false;
137 boolean changeState = super.updateTime(curTime);
138
139 //Get current values for parameters
140 StyleAttribute sty = new StyleAttribute();
141 boolean shapeChange = false;
142
143 if (getPres(sty.setName("cx")))
144 {
145 float newCx = sty.getFloatValueWithUnits();
146 if (newCx != cx)
147 {
148 cx = newCx;
149 shapeChange = true;
150 }
151 }
152
153 if (getPres(sty.setName("cy")))
154 {
155 float newCy = sty.getFloatValueWithUnits();
156 if (newCy != cy)
157 {
158 cy = newCy;
159 shapeChange = true;
160 }
161 }
162
163 if (getPres(sty.setName("rx")))
164 {
165 float newRx = sty.getFloatValueWithUnits();
166 if (newRx != rx)
167 {
168 rx = newRx;
169 shapeChange = true;
170 }
171 }
172
173 if (getPres(sty.setName("ry")))
174 {
175 float newRy = sty.getFloatValueWithUnits();
176 if (newRy != ry)
177 {
178 ry = newRy;
179 shapeChange = true;
180 }
181 }
182
183 if (shapeChange)
184 {
185 build();
186 // ellipse.setFrame(cx - rx, cy - ry, rx * 2f, ry * 2f);
187 // return true;
188 }
189
190 return changeState || shapeChange;
191 }
192 }