source: josm/trunk/src/com/kitfox/svg/Polygon.java@ 4656

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

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

File size: 5.1 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;
31import com.kitfox.svg.xml.XMLParseUtil;
32import java.awt.geom.*;
33import java.awt.*;
34
35import com.kitfox.svg.xml.*;
36import org.xml.sax.*;
37
38/**
39 * @author Mark McKay
40 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
41 */
42public class Polygon extends ShapeElement {
43
44 int fillRule = GeneralPath.WIND_NON_ZERO;
45 String pointsStrn = "";
46// float[] points = null;
47 GeneralPath path;
48
49 /** Creates a new instance of Rect */
50 public Polygon() {
51 }
52/*
53 public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
54 {
55 //Load style string
56 super.loaderStartElement(helper, attrs, parent);
57
58
59 points = XMLParseUtil.parseFloatList(attrs.getValue("points"));
60
61 build();
62 }
63*/
64/*
65 public void build()
66 {
67 StyleAttribute styleAttrib = getStyle("fill-rule");
68 String fillRule = (styleAttrib == null) ? "nonzero" : styleAttrib.getStringValue();
69
70 path = new GeneralPath(
71 fillRule.equals("evenodd") ? GeneralPath.WIND_EVEN_ODD : GeneralPath.WIND_NON_ZERO,
72 points.length / 2);
73
74 path.moveTo(points[0], points[1]);
75 for (int i = 2; i < points.length; i += 2)
76 {
77 path.lineTo(points[i], points[i + 1]);
78 }
79 path.closePath();
80 }
81*/
82
83//static int yyyyy = 0;
84
85 protected void build() throws SVGException
86 {
87 super.build();
88
89 StyleAttribute sty = new StyleAttribute();
90
91 if (getPres(sty.setName("points"))) pointsStrn = sty.getStringValue();
92
93 String fillRuleStrn = getStyle(sty.setName("fill-rule")) ? sty.getStringValue() : "nonzero";
94 fillRule = fillRuleStrn.equals("evenodd") ? GeneralPath.WIND_EVEN_ODD : GeneralPath.WIND_NON_ZERO;
95
96 buildPath();
97 }
98
99 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}
Note: See TracBrowser for help on using the repository browser.