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