1 | /*
|
---|
2 | * SVG Salamander
|
---|
3 | * Copyright (c) 2004, Mark McKay
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or
|
---|
7 | * without modification, are permitted provided that the following
|
---|
8 | * conditions are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above
|
---|
11 | * copyright notice, this list of conditions and the following
|
---|
12 | * disclaimer.
|
---|
13 | * - Redistributions in binary form must reproduce the above
|
---|
14 | * copyright notice, this list of conditions and the following
|
---|
15 | * disclaimer in the documentation and/or other materials
|
---|
16 | * provided with the distribution.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
---|
21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
---|
22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
---|
23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
---|
25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
---|
27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
---|
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
---|
29 | * OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
30 | *
|
---|
31 | * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
|
---|
32 | * projects can be found at http://www.kitfox.com
|
---|
33 | *
|
---|
34 | * Created on January 26, 2004, 1:54 AM
|
---|
35 | */
|
---|
36 | package com.kitfox.svg;
|
---|
37 |
|
---|
38 | import com.kitfox.svg.xml.StyleAttribute;
|
---|
39 | import java.awt.Color;
|
---|
40 | import java.awt.LinearGradientPaint;
|
---|
41 | import java.awt.MultipleGradientPaint;
|
---|
42 | import java.awt.Paint;
|
---|
43 | import java.awt.geom.AffineTransform;
|
---|
44 | import java.awt.geom.Point2D;
|
---|
45 | import java.awt.geom.Rectangle2D;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * @author Mark McKay
|
---|
49 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
---|
50 | */
|
---|
51 | public class LinearGradient extends Gradient
|
---|
52 | {
|
---|
53 | public static final String TAG_NAME = "lineargradient";
|
---|
54 |
|
---|
55 | float x1 = 0f;
|
---|
56 | float y1 = 0f;
|
---|
57 | float x2 = 1f;
|
---|
58 | float y2 = 0f;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Creates a new instance of LinearGradient
|
---|
62 | */
|
---|
63 | public LinearGradient()
|
---|
64 | {
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public String getTagName()
|
---|
69 | {
|
---|
70 | return TAG_NAME;
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | protected void build() throws SVGException
|
---|
75 | {
|
---|
76 | super.build();
|
---|
77 |
|
---|
78 | StyleAttribute sty = new StyleAttribute();
|
---|
79 |
|
---|
80 | if (getPres(sty.setName("x1")))
|
---|
81 | {
|
---|
82 | x1 = sty.getFloatValueWithUnits();
|
---|
83 | }
|
---|
84 |
|
---|
85 | if (getPres(sty.setName("y1")))
|
---|
86 | {
|
---|
87 | y1 = sty.getFloatValueWithUnits();
|
---|
88 | }
|
---|
89 |
|
---|
90 | if (getPres(sty.setName("x2")))
|
---|
91 | {
|
---|
92 | x2 = sty.getFloatValueWithUnits();
|
---|
93 | }
|
---|
94 |
|
---|
95 | if (getPres(sty.setName("y2")))
|
---|
96 | {
|
---|
97 | y2 = sty.getFloatValueWithUnits();
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public Paint getPaint(Rectangle2D bounds, AffineTransform xform)
|
---|
103 | {
|
---|
104 | MultipleGradientPaint.CycleMethod method;
|
---|
105 | switch (spreadMethod)
|
---|
106 | {
|
---|
107 | default:
|
---|
108 | case SM_PAD:
|
---|
109 | method = MultipleGradientPaint.CycleMethod.NO_CYCLE;
|
---|
110 | break;
|
---|
111 | case SM_REPEAT:
|
---|
112 | method = MultipleGradientPaint.CycleMethod.REPEAT;
|
---|
113 | break;
|
---|
114 | case SM_REFLECT:
|
---|
115 | method = MultipleGradientPaint.CycleMethod.REFLECT;
|
---|
116 | break;
|
---|
117 | }
|
---|
118 |
|
---|
119 | Paint paint;
|
---|
120 | Point2D.Float pt1 = new Point2D.Float(x1, y1);
|
---|
121 | Point2D.Float pt2 = new Point2D.Float(x2, y2);
|
---|
122 | if (pt1.equals(pt2))
|
---|
123 | {
|
---|
124 | Color[] colors = getStopColors();
|
---|
125 | paint = colors.length > 0 ? colors[0] : Color.black;
|
---|
126 | } else if (gradientUnits == GU_USER_SPACE_ON_USE)
|
---|
127 | {
|
---|
128 | paint = new LinearGradientPaint(
|
---|
129 | pt1,
|
---|
130 | pt2,
|
---|
131 | getStopFractions(),
|
---|
132 | getStopColors(),
|
---|
133 | method,
|
---|
134 | MultipleGradientPaint.ColorSpaceType.SRGB,
|
---|
135 | gradientTransform == null
|
---|
136 | ? new AffineTransform()
|
---|
137 | : gradientTransform);
|
---|
138 | } else
|
---|
139 | {
|
---|
140 | AffineTransform viewXform = new AffineTransform();
|
---|
141 | viewXform.translate(bounds.getX(), bounds.getY());
|
---|
142 |
|
---|
143 | //This is a hack to get around shapes that have a width or height of 0. Should be close enough to the true answer.
|
---|
144 | double width = Math.max(1, bounds.getWidth());
|
---|
145 | double height = Math.max(1, bounds.getHeight());
|
---|
146 | viewXform.scale(width, height);
|
---|
147 |
|
---|
148 | if (gradientTransform != null)
|
---|
149 | {
|
---|
150 | viewXform.concatenate(gradientTransform);
|
---|
151 | }
|
---|
152 |
|
---|
153 | paint = new LinearGradientPaint(
|
---|
154 | pt1,
|
---|
155 | pt2,
|
---|
156 | getStopFractions(),
|
---|
157 | getStopColors(),
|
---|
158 | method,
|
---|
159 | MultipleGradientPaint.ColorSpaceType.SRGB,
|
---|
160 | viewXform);
|
---|
161 | }
|
---|
162 |
|
---|
163 | return paint;
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Updates all attributes in this diagram associated with a time event. Ie,
|
---|
168 | * all attributes with track information.
|
---|
169 | *
|
---|
170 | * @return - true if this node has changed state as a result of the time
|
---|
171 | * update
|
---|
172 | */
|
---|
173 | @Override
|
---|
174 | public boolean updateTime(double curTime) throws SVGException
|
---|
175 | {
|
---|
176 | // if (trackManager.getNumTracks() == 0) return stopChange;
|
---|
177 | boolean changeState = super.updateTime(curTime);
|
---|
178 |
|
---|
179 | //Get current values for parameters
|
---|
180 | StyleAttribute sty = new StyleAttribute();
|
---|
181 | boolean shapeChange = false;
|
---|
182 |
|
---|
183 | if (getPres(sty.setName("x1")))
|
---|
184 | {
|
---|
185 | float newVal = sty.getFloatValueWithUnits();
|
---|
186 | if (newVal != x1)
|
---|
187 | {
|
---|
188 | x1 = newVal;
|
---|
189 | shapeChange = true;
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | if (getPres(sty.setName("y1")))
|
---|
194 | {
|
---|
195 | float newVal = sty.getFloatValueWithUnits();
|
---|
196 | if (newVal != y1)
|
---|
197 | {
|
---|
198 | y1 = newVal;
|
---|
199 | shapeChange = true;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | if (getPres(sty.setName("x2")))
|
---|
204 | {
|
---|
205 | float newVal = sty.getFloatValueWithUnits();
|
---|
206 | if (newVal != x2)
|
---|
207 | {
|
---|
208 | x2 = newVal;
|
---|
209 | shapeChange = true;
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | if (getPres(sty.setName("y2")))
|
---|
214 | {
|
---|
215 | float newVal = sty.getFloatValueWithUnits();
|
---|
216 | if (newVal != y2)
|
---|
217 | {
|
---|
218 | y2 = newVal;
|
---|
219 | shapeChange = true;
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | return changeState || shapeChange;
|
---|
224 | }
|
---|
225 | }
|
---|