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