| 1 | /*
|
|---|
| 2 | * RadialGradient.java
|
|---|
| 3 | *
|
|---|
| 4 | *
|
|---|
| 5 | * The Salamander Project - 2D and 3D graphics libraries in Java
|
|---|
| 6 | * Copyright (C) 2004 Mark McKay
|
|---|
| 7 | *
|
|---|
| 8 | * This library is free software; you can redistribute it and/or
|
|---|
| 9 | * modify it under the terms of the GNU Lesser General Public
|
|---|
| 10 | * License as published by the Free Software Foundation; either
|
|---|
| 11 | * version 2.1 of the License, or (at your option) any later version.
|
|---|
| 12 | *
|
|---|
| 13 | * This library is distributed in the hope that it will be useful,
|
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 16 | * Lesser General Public License for more details.
|
|---|
| 17 | *
|
|---|
| 18 | * You should have received a copy of the GNU Lesser General Public
|
|---|
| 19 | * License along with this library; if not, write to the Free Software
|
|---|
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 21 | *
|
|---|
| 22 | * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
|
|---|
| 23 | * projects can be found at http://www.kitfox.com
|
|---|
| 24 | *
|
|---|
| 25 | * Created on January 26, 2004, 1:55 AM
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | package com.kitfox.svg;
|
|---|
| 29 |
|
|---|
| 30 | import com.kitfox.svg.xml.StyleAttribute;
|
|---|
| 31 | import java.awt.geom.*;
|
|---|
| 32 | import java.awt.*;
|
|---|
| 33 |
|
|---|
| 34 | import com.kitfox.svg.xml.*;
|
|---|
| 35 | import org.xml.sax.*;
|
|---|
| 36 |
|
|---|
| 37 | //import org.apache.batik.ext.awt.*;
|
|---|
| 38 | import com.kitfox.svg.batik.*;
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * @author Mark McKay
|
|---|
| 43 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
|---|
| 44 | */
|
|---|
| 45 | public class RadialGradient extends Gradient {
|
|---|
| 46 |
|
|---|
| 47 | float cx = 0.5f;
|
|---|
| 48 | float cy = 0.5f;
|
|---|
| 49 | float fx = 0.5f;
|
|---|
| 50 | float fy = 0.5f;
|
|---|
| 51 | float r = 0.5f;
|
|---|
| 52 |
|
|---|
| 53 | /** Creates a new instance of RadialGradient */
|
|---|
| 54 | public RadialGradient() {
|
|---|
| 55 | }
|
|---|
| 56 | /*
|
|---|
| 57 | public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
|
|---|
| 58 | {
|
|---|
| 59 | //Load style string
|
|---|
| 60 | super.loaderStartElement(helper, attrs, parent);
|
|---|
| 61 |
|
|---|
| 62 | String cx = attrs.getValue("cx");
|
|---|
| 63 | String cy = attrs.getValue("cy");
|
|---|
| 64 | String fx = attrs.getValue("fx");
|
|---|
| 65 | String fy = attrs.getValue("fy");
|
|---|
| 66 | String r = attrs.getValue("r");
|
|---|
| 67 |
|
|---|
| 68 | if (cx != null) this.cx = (float)XMLParseUtil.parseRatio(cx);
|
|---|
| 69 | if (cy != null) this.cy = (float)XMLParseUtil.parseRatio(cy);
|
|---|
| 70 | if (fx != null) this.fx = (float)XMLParseUtil.parseRatio(fx);
|
|---|
| 71 | if (fy != null) this.fy = (float)XMLParseUtil.parseRatio(fy);
|
|---|
| 72 | if (r != null) this.r = (float)XMLParseUtil.parseRatio(r);
|
|---|
| 73 | }
|
|---|
| 74 | */
|
|---|
| 75 |
|
|---|
| 76 | /*
|
|---|
| 77 | public void loaderEndElement(SVGLoaderHelper helper)
|
|---|
| 78 | {
|
|---|
| 79 | super.loaderEndElement(helper);
|
|---|
| 80 |
|
|---|
| 81 | build();
|
|---|
| 82 | }
|
|---|
| 83 | */
|
|---|
| 84 |
|
|---|
| 85 | protected void build() throws SVGException
|
|---|
| 86 | {
|
|---|
| 87 | super.build();
|
|---|
| 88 |
|
|---|
| 89 | StyleAttribute sty = new StyleAttribute();
|
|---|
| 90 |
|
|---|
| 91 | if (getPres(sty.setName("cx"))) cx = sty.getFloatValueWithUnits();
|
|---|
| 92 |
|
|---|
| 93 | if (getPres(sty.setName("cy"))) cy = sty.getFloatValueWithUnits();
|
|---|
| 94 |
|
|---|
| 95 | if (getPres(sty.setName("fx"))) fx = sty.getFloatValueWithUnits();
|
|---|
| 96 |
|
|---|
| 97 | if (getPres(sty.setName("fy"))) fy = sty.getFloatValueWithUnits();
|
|---|
| 98 |
|
|---|
| 99 | if (getPres(sty.setName("r"))) r = sty.getFloatValueWithUnits();
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | public Paint getPaint(Rectangle2D bounds, AffineTransform xform)
|
|---|
| 103 | {
|
|---|
| 104 | com.kitfox.svg.batik.MultipleGradientPaint.CycleMethodEnum method;
|
|---|
| 105 | switch (spreadMethod)
|
|---|
| 106 | {
|
|---|
| 107 | default:
|
|---|
| 108 | case SM_PAD:
|
|---|
| 109 | method = com.kitfox.svg.batik.MultipleGradientPaint.NO_CYCLE;
|
|---|
| 110 | break;
|
|---|
| 111 | case SM_REPEAT:
|
|---|
| 112 | method = com.kitfox.svg.batik.MultipleGradientPaint.REPEAT;
|
|---|
| 113 | break;
|
|---|
| 114 | case SM_REFLECT:
|
|---|
| 115 | method = com.kitfox.svg.batik.MultipleGradientPaint.REFLECT;
|
|---|
| 116 | break;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | com.kitfox.svg.batik.RadialGradientPaint paint;
|
|---|
| 120 |
|
|---|
| 121 | if (gradientUnits == GU_USER_SPACE_ON_USE)
|
|---|
| 122 | {
|
|---|
| 123 | paint = new com.kitfox.svg.batik.RadialGradientPaint(
|
|---|
| 124 | new Point2D.Float(cx, cy),
|
|---|
| 125 | r,
|
|---|
| 126 | new Point2D.Float(fx, fy),
|
|---|
| 127 | getStopFractions(),
|
|---|
| 128 | getStopColors(),
|
|---|
| 129 | method,
|
|---|
| 130 | com.kitfox.svg.batik.MultipleGradientPaint.SRGB,
|
|---|
| 131 | gradientTransform);
|
|---|
| 132 | }
|
|---|
| 133 | else
|
|---|
| 134 | {
|
|---|
| 135 | AffineTransform viewXform = new AffineTransform();
|
|---|
| 136 | viewXform.translate(bounds.getX(), bounds.getY());
|
|---|
| 137 | viewXform.scale(bounds.getWidth(), bounds.getHeight());
|
|---|
| 138 |
|
|---|
| 139 | viewXform.concatenate(gradientTransform);
|
|---|
| 140 |
|
|---|
| 141 | paint = new com.kitfox.svg.batik.RadialGradientPaint(
|
|---|
| 142 | new Point2D.Float(cx, cy),
|
|---|
| 143 | r,
|
|---|
| 144 | new Point2D.Float(fx, fy),
|
|---|
| 145 | getStopFractions(),
|
|---|
| 146 | getStopColors(),
|
|---|
| 147 | method,
|
|---|
| 148 | com.kitfox.svg.batik.MultipleGradientPaint.SRGB,
|
|---|
| 149 | viewXform);
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | return paint;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /**
|
|---|
| 156 | * Updates all attributes in this diagram associated with a time event.
|
|---|
| 157 | * Ie, all attributes with track information.
|
|---|
| 158 | * @return - true if this node has changed state as a result of the time
|
|---|
| 159 | * update
|
|---|
| 160 | */
|
|---|
| 161 | public boolean updateTime(double curTime) throws SVGException
|
|---|
| 162 | {
|
|---|
| 163 | // if (trackManager.getNumTracks() == 0) return false;
|
|---|
| 164 | boolean changeState = super.updateTime(curTime);
|
|---|
| 165 |
|
|---|
| 166 | //Get current values for parameters
|
|---|
| 167 | StyleAttribute sty = new StyleAttribute();
|
|---|
| 168 | boolean shapeChange = false;
|
|---|
| 169 |
|
|---|
| 170 | if (getPres(sty.setName("cx")))
|
|---|
| 171 | {
|
|---|
| 172 | float newVal = sty.getFloatValueWithUnits();
|
|---|
| 173 | if (newVal != cx)
|
|---|
| 174 | {
|
|---|
| 175 | cx = newVal;
|
|---|
| 176 | shapeChange = true;
|
|---|
| 177 | }
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | if (getPres(sty.setName("cy")))
|
|---|
| 181 | {
|
|---|
| 182 | float newVal = sty.getFloatValueWithUnits();
|
|---|
| 183 | if (newVal != cy)
|
|---|
| 184 | {
|
|---|
| 185 | cy = newVal;
|
|---|
| 186 | shapeChange = true;
|
|---|
| 187 | }
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | if (getPres(sty.setName("fx")))
|
|---|
| 191 | {
|
|---|
| 192 | float newVal = sty.getFloatValueWithUnits();
|
|---|
| 193 | if (newVal != fx)
|
|---|
| 194 | {
|
|---|
| 195 | fx = newVal;
|
|---|
| 196 | shapeChange = true;
|
|---|
| 197 | }
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | if (getPres(sty.setName("fy")))
|
|---|
| 201 | {
|
|---|
| 202 | float newVal = sty.getFloatValueWithUnits();
|
|---|
| 203 | if (newVal != fy)
|
|---|
| 204 | {
|
|---|
| 205 | fy = newVal;
|
|---|
| 206 | shapeChange = true;
|
|---|
| 207 | }
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | if (getPres(sty.setName("r")))
|
|---|
| 211 | {
|
|---|
| 212 | float newVal = sty.getFloatValueWithUnits();
|
|---|
| 213 | if (newVal != r)
|
|---|
| 214 | {
|
|---|
| 215 | r = newVal;
|
|---|
| 216 | shapeChange = true;
|
|---|
| 217 | }
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | return changeState;
|
|---|
| 221 | }
|
|---|
| 222 | }
|
|---|