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:55 AM
|
---|
35 | */
|
---|
36 | package com.kitfox.svg;
|
---|
37 |
|
---|
38 | import com.kitfox.svg.xml.StyleAttribute;
|
---|
39 | import java.awt.MultipleGradientPaint;
|
---|
40 | import java.awt.Paint;
|
---|
41 | import java.awt.RadialGradientPaint;
|
---|
42 | import java.awt.geom.AffineTransform;
|
---|
43 | import java.awt.geom.Point2D;
|
---|
44 | import java.awt.geom.Rectangle2D;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * @author Mark McKay
|
---|
48 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
---|
49 | */
|
---|
50 | public class RadialGradient extends Gradient
|
---|
51 | {
|
---|
52 | public static final String TAG_NAME = "radialgradient";
|
---|
53 |
|
---|
54 | float cx = 0.5f;
|
---|
55 | float cy = 0.5f;
|
---|
56 | boolean hasFocus = false;
|
---|
57 | float fx = 0f;
|
---|
58 | float fy = 0f;
|
---|
59 | float r = 0.5f;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Creates a new instance of RadialGradient
|
---|
63 | */
|
---|
64 | public RadialGradient()
|
---|
65 | {
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public String getTagName()
|
---|
70 | {
|
---|
71 | return TAG_NAME;
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | protected void build() throws SVGException
|
---|
76 | {
|
---|
77 | super.build();
|
---|
78 |
|
---|
79 | StyleAttribute sty = new StyleAttribute();
|
---|
80 |
|
---|
81 | if (getPres(sty.setName("cx")))
|
---|
82 | {
|
---|
83 | cx = sty.getFloatValueWithUnits();
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (getPres(sty.setName("cy")))
|
---|
87 | {
|
---|
88 | cy = sty.getFloatValueWithUnits();
|
---|
89 | }
|
---|
90 |
|
---|
91 | hasFocus = false;
|
---|
92 | if (getPres(sty.setName("fx")))
|
---|
93 | {
|
---|
94 | fx = sty.getFloatValueWithUnits();
|
---|
95 | hasFocus = true;
|
---|
96 | }
|
---|
97 |
|
---|
98 | if (getPres(sty.setName("fy")))
|
---|
99 | {
|
---|
100 | fy = sty.getFloatValueWithUnits();
|
---|
101 | hasFocus = true;
|
---|
102 | }
|
---|
103 |
|
---|
104 | if (getPres(sty.setName("r")))
|
---|
105 | {
|
---|
106 | r = sty.getFloatValueWithUnits();
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Override
|
---|
111 | public Paint getPaint(Rectangle2D bounds, AffineTransform xform)
|
---|
112 | {
|
---|
113 | MultipleGradientPaint.CycleMethod method;
|
---|
114 | switch (spreadMethod)
|
---|
115 | {
|
---|
116 | default:
|
---|
117 | case SM_PAD:
|
---|
118 | method = MultipleGradientPaint.CycleMethod.NO_CYCLE;
|
---|
119 | break;
|
---|
120 | case SM_REPEAT:
|
---|
121 | method = MultipleGradientPaint.CycleMethod.REPEAT;
|
---|
122 | break;
|
---|
123 | case SM_REFLECT:
|
---|
124 | method = MultipleGradientPaint.CycleMethod.REFLECT;
|
---|
125 | break;
|
---|
126 | }
|
---|
127 |
|
---|
128 | Paint paint;
|
---|
129 | Point2D.Float pt1 = new Point2D.Float(cx, cy);
|
---|
130 | Point2D.Float pt2 = hasFocus ? new Point2D.Float(fx, fy) : pt1;
|
---|
131 | if (gradientUnits == GU_USER_SPACE_ON_USE)
|
---|
132 | {
|
---|
133 | paint = new RadialGradientPaint(
|
---|
134 | pt1,
|
---|
135 | r,
|
---|
136 | pt2,
|
---|
137 | getStopFractions(),
|
---|
138 | getStopColors(),
|
---|
139 | method,
|
---|
140 | MultipleGradientPaint.ColorSpaceType.SRGB,
|
---|
141 | gradientTransform);
|
---|
142 | } else
|
---|
143 | {
|
---|
144 | AffineTransform viewXform = new AffineTransform();
|
---|
145 | viewXform.translate(bounds.getX(), bounds.getY());
|
---|
146 | viewXform.scale(bounds.getWidth(), bounds.getHeight());
|
---|
147 |
|
---|
148 | viewXform.concatenate(gradientTransform);
|
---|
149 |
|
---|
150 | paint = new RadialGradientPaint(
|
---|
151 | pt1,
|
---|
152 | r,
|
---|
153 | pt2,
|
---|
154 | getStopFractions(),
|
---|
155 | getStopColors(),
|
---|
156 | method,
|
---|
157 | MultipleGradientPaint.ColorSpaceType.SRGB,
|
---|
158 | viewXform);
|
---|
159 | }
|
---|
160 |
|
---|
161 | return paint;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Updates all attributes in this diagram associated with a time event. Ie,
|
---|
166 | * all attributes with track information.
|
---|
167 | *
|
---|
168 | * @return - true if this node has changed state as a result of the time
|
---|
169 | * update
|
---|
170 | */
|
---|
171 | @Override
|
---|
172 | public boolean updateTime(double curTime) throws SVGException
|
---|
173 | {
|
---|
174 | // if (trackManager.getNumTracks() == 0) return false;
|
---|
175 | boolean changeState = super.updateTime(curTime);
|
---|
176 |
|
---|
177 | //Get current values for parameters
|
---|
178 | StyleAttribute sty = new StyleAttribute();
|
---|
179 | boolean shapeChange = false;
|
---|
180 |
|
---|
181 | if (getPres(sty.setName("cx")))
|
---|
182 | {
|
---|
183 | float newVal = sty.getFloatValueWithUnits();
|
---|
184 | if (newVal != cx)
|
---|
185 | {
|
---|
186 | cx = newVal;
|
---|
187 | shapeChange = true;
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | if (getPres(sty.setName("cy")))
|
---|
192 | {
|
---|
193 | float newVal = sty.getFloatValueWithUnits();
|
---|
194 | if (newVal != cy)
|
---|
195 | {
|
---|
196 | cy = newVal;
|
---|
197 | shapeChange = true;
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | if (getPres(sty.setName("fx")))
|
---|
202 | {
|
---|
203 | float newVal = sty.getFloatValueWithUnits();
|
---|
204 | if (newVal != fx)
|
---|
205 | {
|
---|
206 | fx = newVal;
|
---|
207 | shapeChange = true;
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | if (getPres(sty.setName("fy")))
|
---|
212 | {
|
---|
213 | float newVal = sty.getFloatValueWithUnits();
|
---|
214 | if (newVal != fy)
|
---|
215 | {
|
---|
216 | fy = newVal;
|
---|
217 | shapeChange = true;
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | if (getPres(sty.setName("r")))
|
---|
222 | {
|
---|
223 | float newVal = sty.getFloatValueWithUnits();
|
---|
224 | if (newVal != r)
|
---|
225 | {
|
---|
226 | r = newVal;
|
---|
227 | shapeChange = true;
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | return changeState;
|
---|
232 | }
|
---|
233 | }
|
---|