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:54 AM 035 */ 036package com.kitfox.svg; 037 038import com.kitfox.svg.xml.StyleAttribute; 039import java.awt.Color; 040import java.awt.LinearGradientPaint; 041import java.awt.MultipleGradientPaint; 042import java.awt.Paint; 043import java.awt.geom.AffineTransform; 044import java.awt.geom.Point2D; 045import java.awt.geom.Rectangle2D; 046 047/** 048 * @author Mark McKay 049 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a> 050 */ 051public class LinearGradient extends Gradient 052{ 053 public static final String TAG_NAME = "lineargradient"; 054 055 float x1 = 0f; 056 float y1 = 0f; 057 float x2 = 1f; 058 float y2 = 0f; 059 060 /** 061 * Creates a new instance of LinearGradient 062 */ 063 public LinearGradient() 064 { 065 } 066 067 public String getTagName() 068 { 069 return TAG_NAME; 070 } 071 072 protected void build() throws SVGException 073 { 074 super.build(); 075 076 StyleAttribute sty = new StyleAttribute(); 077 078 if (getPres(sty.setName("x1"))) 079 { 080 x1 = sty.getFloatValueWithUnits(); 081 } 082 083 if (getPres(sty.setName("y1"))) 084 { 085 y1 = sty.getFloatValueWithUnits(); 086 } 087 088 if (getPres(sty.setName("x2"))) 089 { 090 x2 = sty.getFloatValueWithUnits(); 091 } 092 093 if (getPres(sty.setName("y2"))) 094 { 095 y2 = sty.getFloatValueWithUnits(); 096 } 097 } 098 099 public Paint getPaint(Rectangle2D bounds, AffineTransform xform) 100 { 101 MultipleGradientPaint.CycleMethod method; 102 switch (spreadMethod) 103 { 104 default: 105 case SM_PAD: 106 method = MultipleGradientPaint.CycleMethod.NO_CYCLE; 107 break; 108 case SM_REPEAT: 109 method = MultipleGradientPaint.CycleMethod.REPEAT; 110 break; 111 case SM_REFLECT: 112 method = MultipleGradientPaint.CycleMethod.REFLECT; 113 break; 114 } 115 116 Paint paint; 117 Point2D.Float pt1 = new Point2D.Float(x1, y1); 118 Point2D.Float pt2 = new Point2D.Float(x2, y2); 119 if (pt1.equals(pt2)) 120 { 121 Color[] colors = getStopColors(); 122 paint = colors.length > 0 ? colors[0] : Color.black; 123 } else if (gradientUnits == GU_USER_SPACE_ON_USE) 124 { 125 paint = new LinearGradientPaint( 126 pt1, 127 pt2, 128 getStopFractions(), 129 getStopColors(), 130 method, 131 MultipleGradientPaint.ColorSpaceType.SRGB, 132 gradientTransform == null 133 ? new AffineTransform() 134 : gradientTransform); 135 } else 136 { 137 AffineTransform viewXform = new AffineTransform(); 138 viewXform.translate(bounds.getX(), bounds.getY()); 139 140 //This is a hack to get around shapes that have a width or height of 0. Should be close enough to the true answer. 141 double width = Math.max(1, bounds.getWidth()); 142 double height = Math.max(1, bounds.getHeight()); 143 viewXform.scale(width, height); 144 145 if (gradientTransform != null) 146 { 147 viewXform.concatenate(gradientTransform); 148 } 149 150 paint = new LinearGradientPaint( 151 pt1, 152 pt2, 153 getStopFractions(), 154 getStopColors(), 155 method, 156 MultipleGradientPaint.ColorSpaceType.SRGB, 157 viewXform); 158 } 159 160 return paint; 161 } 162 163 /** 164 * Updates all attributes in this diagram associated with a time event. Ie, 165 * all attributes with track information. 166 * 167 * @return - true if this node has changed state as a result of the time 168 * update 169 */ 170 public boolean updateTime(double curTime) throws SVGException 171 { 172// if (trackManager.getNumTracks() == 0) return stopChange; 173 boolean changeState = super.updateTime(curTime); 174 175 //Get current values for parameters 176 StyleAttribute sty = new StyleAttribute(); 177 boolean shapeChange = false; 178 179 if (getPres(sty.setName("x1"))) 180 { 181 float newVal = sty.getFloatValueWithUnits(); 182 if (newVal != x1) 183 { 184 x1 = newVal; 185 shapeChange = true; 186 } 187 } 188 189 if (getPres(sty.setName("y1"))) 190 { 191 float newVal = sty.getFloatValueWithUnits(); 192 if (newVal != y1) 193 { 194 y1 = newVal; 195 shapeChange = true; 196 } 197 } 198 199 if (getPres(sty.setName("x2"))) 200 { 201 float newVal = sty.getFloatValueWithUnits(); 202 if (newVal != x2) 203 { 204 x2 = newVal; 205 shapeChange = true; 206 } 207 } 208 209 if (getPres(sty.setName("y2"))) 210 { 211 float newVal = sty.getFloatValueWithUnits(); 212 if (newVal != y2) 213 { 214 y2 = newVal; 215 shapeChange = true; 216 } 217 } 218 219 return changeState || shapeChange; 220 } 221}