[8084] | 1 | /*
|
---|
| 2 | * SVG Salamander
|
---|
| 3 | * Copyright (c) 2004, Mark McKay
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
[10787] | 6 | * Redistribution and use in source and binary forms, with or
|
---|
[8084] | 7 | * without modification, are permitted provided that the following
|
---|
| 8 | * conditions are met:
|
---|
| 9 | *
|
---|
[10787] | 10 | * - Redistributions of source code must retain the above
|
---|
[8084] | 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
|
---|
[10787] | 15 | * disclaimer in the documentation and/or other materials
|
---|
[8084] | 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
|
---|
[10787] | 29 | * OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 30 | *
|
---|
[8084] | 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 |
|
---|
[10787] | 38 | import java.awt.MultipleGradientPaint;
|
---|
[8084] | 39 | import java.awt.Paint;
|
---|
[10787] | 40 | import java.awt.RadialGradientPaint;
|
---|
[8084] | 41 | import java.awt.geom.AffineTransform;
|
---|
| 42 | import java.awt.geom.Point2D;
|
---|
| 43 | import java.awt.geom.Rectangle2D;
|
---|
[10787] | 44 | import java.util.Arrays;
|
---|
[8084] | 45 |
|
---|
[10787] | 46 | import com.kitfox.svg.xml.StyleAttribute;
|
---|
| 47 |
|
---|
[8084] | 48 | /**
|
---|
| 49 | * @author Mark McKay
|
---|
| 50 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
---|
| 51 | */
|
---|
| 52 | public class RadialGradient extends Gradient
|
---|
| 53 | {
|
---|
| 54 | public static final String TAG_NAME = "radialgradient";
|
---|
| 55 |
|
---|
| 56 | float cx = 0.5f;
|
---|
| 57 | float cy = 0.5f;
|
---|
[10787] | 58 | boolean hasFocus = false;
|
---|
| 59 | float fx = 0f;
|
---|
| 60 | float fy = 0f;
|
---|
[8084] | 61 | float r = 0.5f;
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Creates a new instance of RadialGradient
|
---|
| 65 | */
|
---|
| 66 | public RadialGradient()
|
---|
| 67 | {
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[10787] | 70 | @Override
|
---|
[8084] | 71 | public String getTagName()
|
---|
| 72 | {
|
---|
| 73 | return TAG_NAME;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[10787] | 76 | @Override
|
---|
[8084] | 77 | protected void build() throws SVGException
|
---|
| 78 | {
|
---|
| 79 | super.build();
|
---|
| 80 |
|
---|
| 81 | StyleAttribute sty = new StyleAttribute();
|
---|
| 82 |
|
---|
| 83 | if (getPres(sty.setName("cx")))
|
---|
| 84 | {
|
---|
| 85 | cx = sty.getFloatValueWithUnits();
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | if (getPres(sty.setName("cy")))
|
---|
| 89 | {
|
---|
| 90 | cy = sty.getFloatValueWithUnits();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[10787] | 93 | hasFocus = false;
|
---|
[8084] | 94 | if (getPres(sty.setName("fx")))
|
---|
| 95 | {
|
---|
| 96 | fx = sty.getFloatValueWithUnits();
|
---|
[10787] | 97 | hasFocus = true;
|
---|
[8084] | 98 | }
|
---|
| 99 |
|
---|
| 100 | if (getPres(sty.setName("fy")))
|
---|
| 101 | {
|
---|
| 102 | fy = sty.getFloatValueWithUnits();
|
---|
[10787] | 103 | hasFocus = true;
|
---|
[8084] | 104 | }
|
---|
| 105 |
|
---|
| 106 | if (getPres(sty.setName("r")))
|
---|
| 107 | {
|
---|
| 108 | r = sty.getFloatValueWithUnits();
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[10787] | 112 | @Override
|
---|
[8084] | 113 | public Paint getPaint(Rectangle2D bounds, AffineTransform xform)
|
---|
| 114 | {
|
---|
[10787] | 115 | MultipleGradientPaint.CycleMethod method;
|
---|
[8084] | 116 | switch (spreadMethod)
|
---|
| 117 | {
|
---|
| 118 | default:
|
---|
| 119 | case SM_PAD:
|
---|
[10787] | 120 | method = MultipleGradientPaint.CycleMethod.NO_CYCLE;
|
---|
[8084] | 121 | break;
|
---|
| 122 | case SM_REPEAT:
|
---|
[10787] | 123 | method = MultipleGradientPaint.CycleMethod.REPEAT;
|
---|
[8084] | 124 | break;
|
---|
| 125 | case SM_REFLECT:
|
---|
[10787] | 126 | method = MultipleGradientPaint.CycleMethod.REFLECT;
|
---|
[8084] | 127 | break;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | Paint paint;
|
---|
| 131 | Point2D.Float pt1 = new Point2D.Float(cx, cy);
|
---|
[10787] | 132 | Point2D.Float pt2 = hasFocus ? new Point2D.Float(fx, fy) : pt1;
|
---|
[8084] | 133 | if (gradientUnits == GU_USER_SPACE_ON_USE)
|
---|
| 134 | {
|
---|
[10787] | 135 | paint = new RadialGradientPaint(
|
---|
[8084] | 136 | pt1,
|
---|
| 137 | r,
|
---|
| 138 | pt2,
|
---|
| 139 | getStopFractions(),
|
---|
| 140 | getStopColors(),
|
---|
| 141 | method,
|
---|
[10787] | 142 | MultipleGradientPaint.ColorSpaceType.SRGB,
|
---|
[8084] | 143 | gradientTransform);
|
---|
| 144 | } else
|
---|
| 145 | {
|
---|
| 146 | AffineTransform viewXform = new AffineTransform();
|
---|
| 147 | viewXform.translate(bounds.getX(), bounds.getY());
|
---|
| 148 | viewXform.scale(bounds.getWidth(), bounds.getHeight());
|
---|
| 149 |
|
---|
| 150 | viewXform.concatenate(gradientTransform);
|
---|
| 151 |
|
---|
[10787] | 152 | paint = new RadialGradientPaint(
|
---|
[8084] | 153 | pt1,
|
---|
| 154 | r,
|
---|
| 155 | pt2,
|
---|
| 156 | getStopFractions(),
|
---|
| 157 | getStopColors(),
|
---|
| 158 | method,
|
---|
[10787] | 159 | MultipleGradientPaint.ColorSpaceType.SRGB,
|
---|
[8084] | 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 | */
|
---|
[10787] | 173 | @Override
|
---|
[8084] | 174 | public boolean updateTime(double curTime) throws SVGException
|
---|
| 175 | {
|
---|
| 176 | // if (trackManager.getNumTracks() == 0) return false;
|
---|
| 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("cx")))
|
---|
| 184 | {
|
---|
| 185 | float newVal = sty.getFloatValueWithUnits();
|
---|
| 186 | if (newVal != cx)
|
---|
| 187 | {
|
---|
| 188 | cx = newVal;
|
---|
| 189 | shapeChange = true;
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | if (getPres(sty.setName("cy")))
|
---|
| 194 | {
|
---|
| 195 | float newVal = sty.getFloatValueWithUnits();
|
---|
| 196 | if (newVal != cy)
|
---|
| 197 | {
|
---|
| 198 | cy = newVal;
|
---|
| 199 | shapeChange = true;
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | if (getPres(sty.setName("fx")))
|
---|
| 204 | {
|
---|
| 205 | float newVal = sty.getFloatValueWithUnits();
|
---|
| 206 | if (newVal != fx)
|
---|
| 207 | {
|
---|
| 208 | fx = newVal;
|
---|
| 209 | shapeChange = true;
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | if (getPres(sty.setName("fy")))
|
---|
| 214 | {
|
---|
| 215 | float newVal = sty.getFloatValueWithUnits();
|
---|
| 216 | if (newVal != fy)
|
---|
| 217 | {
|
---|
| 218 | fy = newVal;
|
---|
| 219 | shapeChange = true;
|
---|
| 220 | }
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | if (getPres(sty.setName("r")))
|
---|
| 224 | {
|
---|
| 225 | float newVal = sty.getFloatValueWithUnits();
|
---|
| 226 | if (newVal != r)
|
---|
| 227 | {
|
---|
| 228 | r = newVal;
|
---|
| 229 | shapeChange = true;
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | return changeState;
|
---|
| 234 | }
|
---|
| 235 | }
|
---|