| 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 |
|
|---|
| 28 | package com.kitfox.svg;
|
|---|
| 29 |
|
|---|
| 30 | import java.awt.geom.*;
|
|---|
| 31 | import java.awt.*;
|
|---|
| 32 |
|
|---|
| 33 | import com.kitfox.svg.xml.*;
|
|---|
| 34 |
|
|---|
| 35 | /**
|
|---|
| 36 | * @author Mark McKay
|
|---|
| 37 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
|---|
| 38 | */
|
|---|
| 39 | public class Polyline extends ShapeElement {
|
|---|
| 40 |
|
|---|
| 41 | int fillRule = GeneralPath.WIND_NON_ZERO;
|
|---|
| 42 | String pointsStrn = "";
|
|---|
| 43 | // float[] points = null;
|
|---|
| 44 | GeneralPath path;
|
|---|
| 45 |
|
|---|
| 46 | /** Creates a new instance of Rect */
|
|---|
| 47 | public Polyline() {
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | /*
|
|---|
| 51 | public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
|
|---|
| 52 | {
|
|---|
| 53 | //Load style string
|
|---|
| 54 | super.loaderStartElement(helper, attrs, parent);
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | points = XMLParseUtil.parseFloatList(attrs.getValue("points"));
|
|---|
| 58 |
|
|---|
| 59 | build();
|
|---|
| 60 | }
|
|---|
| 61 | */
|
|---|
| 62 |
|
|---|
| 63 | public void build() throws SVGException
|
|---|
| 64 | {
|
|---|
| 65 | super.build();
|
|---|
| 66 |
|
|---|
| 67 | StyleAttribute sty = new StyleAttribute();
|
|---|
| 68 |
|
|---|
| 69 | if (getPres(sty.setName("points"))) pointsStrn = sty.getStringValue();
|
|---|
| 70 |
|
|---|
| 71 | String fillRuleStrn = getStyle(sty.setName("fill-rule")) ? sty.getStringValue() : "nonzero";
|
|---|
| 72 | fillRule = fillRuleStrn.equals("evenodd") ? GeneralPath.WIND_EVEN_ODD : GeneralPath.WIND_NON_ZERO;
|
|---|
| 73 |
|
|---|
| 74 | buildPath();
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | protected void buildPath()
|
|---|
| 78 | {
|
|---|
| 79 | float[] points = XMLParseUtil.parseFloatList(pointsStrn);
|
|---|
| 80 | path = new GeneralPath(fillRule, points.length / 2);
|
|---|
| 81 |
|
|---|
| 82 | path.moveTo(points[0], points[1]);
|
|---|
| 83 | for (int i = 2; i < points.length; i += 2)
|
|---|
| 84 | {
|
|---|
| 85 | path.lineTo(points[i], points[i + 1]);
|
|---|
| 86 | }
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | public void render(Graphics2D g) throws SVGException
|
|---|
| 90 | {
|
|---|
| 91 | beginLayer(g);
|
|---|
| 92 | renderShape(g, path);
|
|---|
| 93 | finishLayer(g);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | public Shape getShape()
|
|---|
| 97 | {
|
|---|
| 98 | return shapeToParent(path);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | public Rectangle2D getBoundingBox() throws SVGException
|
|---|
| 102 | {
|
|---|
| 103 | return boundsToParent(includeStrokeInBounds(path.getBounds2D()));
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /**
|
|---|
| 107 | * Updates all attributes in this diagram associated with a time event.
|
|---|
| 108 | * Ie, all attributes with track information.
|
|---|
| 109 | * @return - true if this node has changed state as a result of the time
|
|---|
| 110 | * update
|
|---|
| 111 | */
|
|---|
| 112 | public boolean updateTime(double curTime) throws SVGException
|
|---|
| 113 | {
|
|---|
| 114 | // if (trackManager.getNumTracks() == 0) return false;
|
|---|
| 115 | boolean changeState = super.updateTime(curTime);
|
|---|
| 116 |
|
|---|
| 117 | //Get current values for parameters
|
|---|
| 118 | StyleAttribute sty = new StyleAttribute();
|
|---|
| 119 | boolean shapeChange = false;
|
|---|
| 120 |
|
|---|
| 121 | if (getStyle(sty.setName("fill-rule")))
|
|---|
| 122 | {
|
|---|
| 123 | int newVal = sty.getStringValue().equals("evenodd")
|
|---|
| 124 | ? GeneralPath.WIND_EVEN_ODD
|
|---|
| 125 | : GeneralPath.WIND_NON_ZERO;
|
|---|
| 126 | if (newVal != fillRule)
|
|---|
| 127 | {
|
|---|
| 128 | fillRule = newVal;
|
|---|
| 129 | shapeChange = true;
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | if (getPres(sty.setName("points")))
|
|---|
| 134 | {
|
|---|
| 135 | String newVal = sty.getStringValue();
|
|---|
| 136 | if (!newVal.equals(pointsStrn))
|
|---|
| 137 | {
|
|---|
| 138 | pointsStrn = newVal;
|
|---|
| 139 | shapeChange = true;
|
|---|
| 140 | }
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 | if (shapeChange)
|
|---|
| 145 | {
|
|---|
| 146 | build();
|
|---|
| 147 | // buildPath();
|
|---|
| 148 | // return true;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | return changeState || shapeChange;
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|