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

Last change on this file since 5369 was 4256, checked in by bastiK, 13 years ago

see #6560 - basic svg support, includes kitfox svgsalamander, r 98, patched

File size: 6.6 KB
Line 
1/*
2 * LinearGradient.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:54 AM
26 */
27
28package com.kitfox.svg;
29
30import com.kitfox.svg.xml.StyleAttribute;
31import java.awt.geom.*;
32import java.awt.*;
33
34import com.kitfox.svg.xml.*;
35import org.xml.sax.*;
36
37//import org.apache.batik.ext.awt.*;
38import com.kitfox.svg.batik.*;
39
40
41/**
42 * @author Mark McKay
43 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
44 */
45public class LinearGradient extends Gradient {
46
47 float x1 = 0f;
48 float y1 = 0f;
49 float x2 = 1f;
50 float y2 = 0f;
51
52 /** Creates a new instance of LinearGradient */
53 public LinearGradient() {
54 }
55/*
56 public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
57 {
58 //Load style string
59 super.loaderStartElement(helper, attrs, parent);
60
61 String x1 = attrs.getValue("x1");
62 String x2 = attrs.getValue("x2");
63 String y1 = attrs.getValue("y1");
64 String y2 = attrs.getValue("y2");
65
66 if (x1 != null) this.x1 = (float)XMLParseUtil.parseRatio(x1);
67 if (y1 != null) this.y1 = (float)XMLParseUtil.parseRatio(y1);
68 if (x2 != null) this.x2 = (float)XMLParseUtil.parseRatio(x2);
69 if (y2 != null) this.y2 = (float)XMLParseUtil.parseRatio(y2);
70 }
71*/
72 /*
73 public void loaderEndElement(SVGLoaderHelper helper)
74 {
75 super.loaderEndElement(helper);
76
77 build();
78 }
79 */
80
81 protected void build() throws SVGException
82 {
83 super.build();
84
85 StyleAttribute sty = new StyleAttribute();
86
87 if (getPres(sty.setName("x1"))) x1 = sty.getFloatValueWithUnits();
88
89 if (getPres(sty.setName("y1"))) y1 = sty.getFloatValueWithUnits();
90
91 if (getPres(sty.setName("x2"))) x2 = sty.getFloatValueWithUnits();
92
93 if (getPres(sty.setName("y2"))) y2 = sty.getFloatValueWithUnits();
94 }
95
96 public Paint getPaint(Rectangle2D bounds, AffineTransform xform)
97 {
98 com.kitfox.svg.batik.MultipleGradientPaint.CycleMethodEnum method;
99 switch (spreadMethod)
100 {
101 default:
102 case SM_PAD:
103 method = com.kitfox.svg.batik.MultipleGradientPaint.NO_CYCLE;
104 break;
105 case SM_REPEAT:
106 method = com.kitfox.svg.batik.MultipleGradientPaint.REPEAT;
107 break;
108 case SM_REFLECT:
109 method = com.kitfox.svg.batik.MultipleGradientPaint.REFLECT;
110 break;
111 }
112
113 com.kitfox.svg.batik.LinearGradientPaint paint;
114 if (gradientUnits == GU_USER_SPACE_ON_USE)
115 {
116// paint = new LinearGradientPaint(x1, y1, x2, y2, getStopFractions(), getStopColors(), method);
117 paint = new com.kitfox.svg.batik.LinearGradientPaint(
118 new Point2D.Float(x1, y1),
119 new Point2D.Float(x2, y2),
120 getStopFractions(),
121 getStopColors(),
122 method,
123 com.kitfox.svg.batik.MultipleGradientPaint.SRGB,
124 gradientTransform);
125 }
126 else
127 {
128 AffineTransform viewXform = new AffineTransform();
129 viewXform.translate(bounds.getX(), bounds.getY());
130
131 //This is a hack to get around shapes that have a width or height of 0. Should be close enough to the true answer.
132 double width = bounds.getWidth();
133 double height = bounds.getHeight();
134 if (width == 0) width = 1;
135 if (height == 0) height = 1;
136 viewXform.scale(width, height);
137
138 viewXform.concatenate(gradientTransform);
139
140 paint = new com.kitfox.svg.batik.LinearGradientPaint(
141 new Point2D.Float(x1, y1),
142 new Point2D.Float(x2, y2),
143 getStopFractions(),
144 getStopColors(),
145 method,
146 com.kitfox.svg.batik.MultipleGradientPaint.SRGB,
147 viewXform);
148 }
149
150 return paint;
151 }
152
153 /**
154 * Updates all attributes in this diagram associated with a time event.
155 * Ie, all attributes with track information.
156 * @return - true if this node has changed state as a result of the time
157 * update
158 */
159 public boolean updateTime(double curTime) throws SVGException
160 {
161// if (trackManager.getNumTracks() == 0) return stopChange;
162 boolean changeState = super.updateTime(curTime);
163
164 //Get current values for parameters
165 StyleAttribute sty = new StyleAttribute();
166 boolean shapeChange = false;
167
168 if (getPres(sty.setName("x1")))
169 {
170 float newVal = sty.getFloatValueWithUnits();
171 if (newVal != x1)
172 {
173 x1 = newVal;
174 shapeChange = true;
175 }
176 }
177
178 if (getPres(sty.setName("y1")))
179 {
180 float newVal = sty.getFloatValueWithUnits();
181 if (newVal != y1)
182 {
183 y1 = newVal;
184 shapeChange = true;
185 }
186 }
187
188 if (getPres(sty.setName("x2")))
189 {
190 float newVal = sty.getFloatValueWithUnits();
191 if (newVal != x2)
192 {
193 x2 = newVal;
194 shapeChange = true;
195 }
196 }
197
198 if (getPres(sty.setName("y2")))
199 {
200 float newVal = sty.getFloatValueWithUnits();
201 if (newVal != y2)
202 {
203 y2 = newVal;
204 shapeChange = true;
205 }
206 }
207
208 return changeState || shapeChange;
209 }
210}
Note: See TracBrowser for help on using the repository browser.