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 com.kitfox.svg.xml.StyleAttribute;
|
---|
31 | import java.awt.Graphics2D;
|
---|
32 | import java.awt.Shape;
|
---|
33 | import java.awt.geom.Line2D;
|
---|
34 | import java.awt.geom.Rectangle2D;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * @author Mark McKay
|
---|
38 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
---|
39 | */
|
---|
40 | public class Line extends ShapeElement {
|
---|
41 |
|
---|
42 | float x1 = 0f;
|
---|
43 | float y1 = 0f;
|
---|
44 | float x2 = 0f;
|
---|
45 | float y2 = 0f;
|
---|
46 |
|
---|
47 | Line2D.Float line;
|
---|
48 | // RectangularShape rect;
|
---|
49 |
|
---|
50 | /** Creates a new instance of Rect */
|
---|
51 | public Line() {
|
---|
52 | }
|
---|
53 |
|
---|
54 | /*
|
---|
55 | public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
|
---|
56 | {
|
---|
57 | //Load style string
|
---|
58 | super.loaderStartElement(helper, attrs, parent);
|
---|
59 |
|
---|
60 | String x1 = attrs.getValue("x1");
|
---|
61 | String y1 = attrs.getValue("y1");
|
---|
62 | String x2 = attrs.getValue("x2");
|
---|
63 | String y2 = attrs.getValue("y2");
|
---|
64 |
|
---|
65 | this.x1 = XMLParseUtil.parseFloat(x1);
|
---|
66 | this.y1 = XMLParseUtil.parseFloat(y1);
|
---|
67 | this.x2 = XMLParseUtil.parseFloat(x2);
|
---|
68 | this.y2 = XMLParseUtil.parseFloat(y2);
|
---|
69 |
|
---|
70 | build();
|
---|
71 | }
|
---|
72 | */
|
---|
73 | protected void build() throws SVGException
|
---|
74 | {
|
---|
75 | super.build();
|
---|
76 |
|
---|
77 | StyleAttribute sty = new StyleAttribute();
|
---|
78 |
|
---|
79 | if (getPres(sty.setName("x1"))) x1 = sty.getFloatValueWithUnits();
|
---|
80 |
|
---|
81 | if (getPres(sty.setName("y1"))) y1 = sty.getFloatValueWithUnits();
|
---|
82 |
|
---|
83 | if (getPres(sty.setName("x2"))) x2 = sty.getFloatValueWithUnits();
|
---|
84 |
|
---|
85 | if (getPres(sty.setName("y2"))) y2 = sty.getFloatValueWithUnits();
|
---|
86 |
|
---|
87 | line = new Line2D.Float(x1, y1, x2, y2);
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | public void render(Graphics2D g) throws SVGException
|
---|
92 | {
|
---|
93 | beginLayer(g);
|
---|
94 | renderShape(g, line);
|
---|
95 | finishLayer(g);
|
---|
96 | }
|
---|
97 |
|
---|
98 | public Shape getShape()
|
---|
99 | {
|
---|
100 | return shapeToParent(line);
|
---|
101 | }
|
---|
102 |
|
---|
103 | public Rectangle2D getBoundingBox() throws SVGException
|
---|
104 | {
|
---|
105 | return boundsToParent(includeStrokeInBounds(line.getBounds2D()));
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Updates all attributes in this diagram associated with a time event.
|
---|
110 | * Ie, all attributes with track information.
|
---|
111 | * @return - true if this node has changed state as a result of the time
|
---|
112 | * update
|
---|
113 | */
|
---|
114 | public boolean updateTime(double curTime) throws SVGException
|
---|
115 | {
|
---|
116 | // if (trackManager.getNumTracks() == 0) return false;
|
---|
117 | boolean changeState = super.updateTime(curTime);
|
---|
118 |
|
---|
119 | //Get current values for parameters
|
---|
120 | StyleAttribute sty = new StyleAttribute();
|
---|
121 | boolean shapeChange = false;
|
---|
122 |
|
---|
123 | if (getPres(sty.setName("x1")))
|
---|
124 | {
|
---|
125 | float newVal = sty.getFloatValueWithUnits();
|
---|
126 | if (newVal != x1)
|
---|
127 | {
|
---|
128 | x1 = newVal;
|
---|
129 | shapeChange = true;
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | if (getPres(sty.setName("y1")))
|
---|
134 | {
|
---|
135 | float newVal = sty.getFloatValueWithUnits();
|
---|
136 | if (newVal != y1)
|
---|
137 | {
|
---|
138 | y1 = newVal;
|
---|
139 | shapeChange = true;
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | if (getPres(sty.setName("x2")))
|
---|
144 | {
|
---|
145 | float newVal = sty.getFloatValueWithUnits();
|
---|
146 | if (newVal != x2)
|
---|
147 | {
|
---|
148 | x2 = newVal;
|
---|
149 | shapeChange = true;
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | if (getPres(sty.setName("y2")))
|
---|
154 | {
|
---|
155 | float newVal = sty.getFloatValueWithUnits();
|
---|
156 | if (newVal != y2)
|
---|
157 | {
|
---|
158 | y2 = newVal;
|
---|
159 | shapeChange = true;
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | if (shapeChange)
|
---|
164 | {
|
---|
165 | build();
|
---|
166 | // line = new Line2D.Float(x1, y1, x2, y2);
|
---|
167 | // return true;
|
---|
168 | }
|
---|
169 |
|
---|
170 | return changeState || shapeChange;
|
---|
171 | }
|
---|
172 | }
|
---|