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