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