001/* 002 * SVG Salamander 003 * Copyright (c) 2004, Mark McKay 004 * All rights reserved. 005 * 006 * Redistribution and use in source and binary forms, with or 007 * without modification, are permitted provided that the following 008 * conditions are met: 009 * 010 * - Redistributions of source code must retain the above 011 * copyright notice, this list of conditions and the following 012 * disclaimer. 013 * - Redistributions in binary form must reproduce the above 014 * copyright notice, this list of conditions and the following 015 * disclaimer in the documentation and/or other materials 016 * provided with the distribution. 017 * 018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 019 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 020 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 021 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 022 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 025 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 026 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 027 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 029 * OF THE POSSIBILITY OF SUCH DAMAGE. 030 * 031 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other 032 * projects can be found at http://www.kitfox.com 033 * 034 * Created on January 26, 2004, 1:55 AM 035 */ 036package com.kitfox.svg; 037 038import com.kitfox.svg.xml.StyleAttribute; 039import java.awt.MultipleGradientPaint; 040import java.awt.Paint; 041import java.awt.RadialGradientPaint; 042import java.awt.geom.AffineTransform; 043import java.awt.geom.Point2D; 044import java.awt.geom.Rectangle2D; 045 046/** 047 * @author Mark McKay 048 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a> 049 */ 050public class RadialGradient extends Gradient 051{ 052 public static final String TAG_NAME = "radialgradient"; 053 054 float cx = 0.5f; 055 float cy = 0.5f; 056 boolean hasFocus = false; 057 float fx = 0f; 058 float fy = 0f; 059 float r = 0.5f; 060 061 /** 062 * Creates a new instance of RadialGradient 063 */ 064 public RadialGradient() 065 { 066 } 067 068 public String getTagName() 069 { 070 return TAG_NAME; 071 } 072 073 protected void build() throws SVGException 074 { 075 super.build(); 076 077 StyleAttribute sty = new StyleAttribute(); 078 079 if (getPres(sty.setName("cx"))) 080 { 081 cx = sty.getFloatValueWithUnits(); 082 } 083 084 if (getPres(sty.setName("cy"))) 085 { 086 cy = sty.getFloatValueWithUnits(); 087 } 088 089 hasFocus = false; 090 if (getPres(sty.setName("fx"))) 091 { 092 fx = sty.getFloatValueWithUnits(); 093 hasFocus = true; 094 } 095 096 if (getPres(sty.setName("fy"))) 097 { 098 fy = sty.getFloatValueWithUnits(); 099 hasFocus = true; 100 } 101 102 if (getPres(sty.setName("r"))) 103 { 104 r = sty.getFloatValueWithUnits(); 105 } 106 } 107 108 public Paint getPaint(Rectangle2D bounds, AffineTransform xform) 109 { 110 MultipleGradientPaint.CycleMethod method; 111 switch (spreadMethod) 112 { 113 default: 114 case SM_PAD: 115 method = MultipleGradientPaint.CycleMethod.NO_CYCLE; 116 break; 117 case SM_REPEAT: 118 method = MultipleGradientPaint.CycleMethod.REPEAT; 119 break; 120 case SM_REFLECT: 121 method = MultipleGradientPaint.CycleMethod.REFLECT; 122 break; 123 } 124 125 Paint paint; 126 Point2D.Float pt1 = new Point2D.Float(cx, cy); 127 Point2D.Float pt2 = hasFocus ? new Point2D.Float(fx, fy) : pt1; 128 if (gradientUnits == GU_USER_SPACE_ON_USE) 129 { 130 paint = new RadialGradientPaint( 131 pt1, 132 r, 133 pt2, 134 getStopFractions(), 135 getStopColors(), 136 method, 137 MultipleGradientPaint.ColorSpaceType.SRGB, 138 gradientTransform); 139 } else 140 { 141 AffineTransform viewXform = new AffineTransform(); 142 viewXform.translate(bounds.getX(), bounds.getY()); 143 viewXform.scale(bounds.getWidth(), bounds.getHeight()); 144 145 viewXform.concatenate(gradientTransform); 146 147 paint = new RadialGradientPaint( 148 pt1, 149 r, 150 pt2, 151 getStopFractions(), 152 getStopColors(), 153 method, 154 MultipleGradientPaint.ColorSpaceType.SRGB, 155 viewXform); 156 } 157 158 return paint; 159 } 160 161 /** 162 * Updates all attributes in this diagram associated with a time event. Ie, 163 * all attributes with track information. 164 * 165 * @return - true if this node has changed state as a result of the time 166 * update 167 */ 168 public boolean updateTime(double curTime) throws SVGException 169 { 170// if (trackManager.getNumTracks() == 0) return false; 171 boolean changeState = super.updateTime(curTime); 172 173 //Get current values for parameters 174 StyleAttribute sty = new StyleAttribute(); 175 boolean shapeChange = false; 176 177 if (getPres(sty.setName("cx"))) 178 { 179 float newVal = sty.getFloatValueWithUnits(); 180 if (newVal != cx) 181 { 182 cx = newVal; 183 shapeChange = true; 184 } 185 } 186 187 if (getPres(sty.setName("cy"))) 188 { 189 float newVal = sty.getFloatValueWithUnits(); 190 if (newVal != cy) 191 { 192 cy = newVal; 193 shapeChange = true; 194 } 195 } 196 197 if (getPres(sty.setName("fx"))) 198 { 199 float newVal = sty.getFloatValueWithUnits(); 200 if (newVal != fx) 201 { 202 fx = newVal; 203 shapeChange = true; 204 } 205 } 206 207 if (getPres(sty.setName("fy"))) 208 { 209 float newVal = sty.getFloatValueWithUnits(); 210 if (newVal != fy) 211 { 212 fy = newVal; 213 shapeChange = true; 214 } 215 } 216 217 if (getPres(sty.setName("r"))) 218 { 219 float newVal = sty.getFloatValueWithUnits(); 220 if (newVal != r) 221 { 222 r = newVal; 223 shapeChange = true; 224 } 225 } 226 227 return changeState; 228 } 229}