source: josm/trunk/src/com/kitfox/svg/LinearGradient.java@ 10746

Last change on this file since 10746 was 8084, checked in by bastiK, 10 years ago

add svn:eol-style=native for svgsalamander

  • Property svn:eol-style set to native
File size: 6.5 KB
RevLine 
[8084]1/*
2 * SVG Salamander
3 * Copyright (c) 2004, Mark McKay
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
9 *
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.
17 *
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
33 *
34 * Created on January 26, 2004, 1:54 AM
35 */
36package com.kitfox.svg;
37
38import com.kitfox.svg.xml.StyleAttribute;
39import java.awt.Color;
40import java.awt.Paint;
41import java.awt.geom.AffineTransform;
42import java.awt.geom.Point2D;
43import java.awt.geom.Rectangle2D;
44
45/**
46 * @author Mark McKay
47 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
48 */
49public class LinearGradient extends Gradient
50{
51 public static final String TAG_NAME = "lineargradient";
52
53 float x1 = 0f;
54 float y1 = 0f;
55 float x2 = 1f;
56 float y2 = 0f;
57
58 /**
59 * Creates a new instance of LinearGradient
60 */
61 public LinearGradient()
62 {
63 }
64
65 public String getTagName()
66 {
67 return TAG_NAME;
68 }
69
70 protected void build() throws SVGException
71 {
72 super.build();
73
74 StyleAttribute sty = new StyleAttribute();
75
76 if (getPres(sty.setName("x1")))
77 {
78 x1 = sty.getFloatValueWithUnits();
79 }
80
81 if (getPres(sty.setName("y1")))
82 {
83 y1 = sty.getFloatValueWithUnits();
84 }
85
86 if (getPres(sty.setName("x2")))
87 {
88 x2 = sty.getFloatValueWithUnits();
89 }
90
91 if (getPres(sty.setName("y2")))
92 {
93 y2 = sty.getFloatValueWithUnits();
94 }
95 }
96
97 public Paint getPaint(Rectangle2D bounds, AffineTransform xform)
98 {
99 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}
Note: See TracBrowser for help on using the repository browser.