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