KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > bridge > SVGRadialGradientElementBridge


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.bridge;
19
20 import java.awt.Color JavaDoc;
21 import java.awt.Paint JavaDoc;
22 import java.awt.geom.AffineTransform JavaDoc;
23 import java.awt.geom.Point2D JavaDoc;
24
25 import org.apache.batik.ext.awt.MultipleGradientPaint;
26 import org.apache.batik.ext.awt.RadialGradientPaint;
27 import org.apache.batik.gvt.GraphicsNode;
28 import org.w3c.dom.Element JavaDoc;
29
30 /**
31  * Bridge class for the <radialGradient> element.
32  *
33  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
34  * @version $Id: SVGRadialGradientElementBridge.java,v 1.11 2004/08/18 07:12:35 vhardy Exp $
35  */

36 public class SVGRadialGradientElementBridge
37     extends AbstractSVGGradientElementBridge {
38
39
40     /**
41      * Constructs a new SVGRadialGradientElementBridge.
42      */

43     public SVGRadialGradientElementBridge() {}
44
45     /**
46      * Returns 'radialGradient'.
47      */

48     public String JavaDoc getLocalName() {
49         return SVG_RADIAL_GRADIENT_TAG;
50     }
51
52     /**
53      * Builds a radial gradient according to the specified parameters.
54      *
55      * @param paintElement the element that defines a Paint
56      * @param paintedElement the element referencing the paint
57      * @param paintedNode the graphics node on which the Paint will be applied
58      * @param spreadMethod the spread method
59      * @param colorSpace the color space (sRGB | LinearRGB)
60      * @param transform the gradient transform
61      * @param colors the colors of the gradient
62      * @param offsets the offsets
63      * @param ctx the bridge context to use
64      */

65     protected
66         Paint JavaDoc buildGradient(Element JavaDoc paintElement,
67                             Element JavaDoc paintedElement,
68                             GraphicsNode paintedNode,
69                             MultipleGradientPaint.CycleMethodEnum spreadMethod,
70                             MultipleGradientPaint.ColorSpaceEnum colorSpace,
71                             AffineTransform JavaDoc transform,
72                             Color JavaDoc [] colors,
73                             float [] offsets,
74                             BridgeContext ctx) {
75
76         // 'cx' attribute - default is 50%
77
String JavaDoc cxStr = SVGUtilities.getChainableAttributeNS
78             (paintElement, null, SVG_CX_ATTRIBUTE, ctx);
79         if (cxStr.length() == 0) {
80             cxStr = SVG_RADIAL_GRADIENT_CX_DEFAULT_VALUE;
81         }
82
83         // 'cy' attribute - default is 50%
84
String JavaDoc cyStr = SVGUtilities.getChainableAttributeNS
85             (paintElement, null, SVG_CY_ATTRIBUTE, ctx);
86         if (cyStr.length() == 0) {
87             cyStr = SVG_RADIAL_GRADIENT_CY_DEFAULT_VALUE;
88         }
89
90         // 'r' attribute - default is 50%
91
String JavaDoc rStr = SVGUtilities.getChainableAttributeNS
92             (paintElement, null, SVG_R_ATTRIBUTE, ctx);
93         if (rStr.length() == 0) {
94             rStr = SVG_RADIAL_GRADIENT_R_DEFAULT_VALUE;
95         }
96
97         // 'fx' attribute - default is same as cx
98
String JavaDoc fxStr = SVGUtilities.getChainableAttributeNS
99             (paintElement, null, SVG_FX_ATTRIBUTE, ctx);
100         if (fxStr.length() == 0) {
101             fxStr = cxStr;
102         }
103
104         // 'fy' attribute - default is same as cy
105
String JavaDoc fyStr = SVGUtilities.getChainableAttributeNS
106             (paintElement, null, SVG_FY_ATTRIBUTE, ctx);
107         if (fyStr.length() == 0) {
108             fyStr = cyStr;
109         }
110
111         // 'gradientUnits' attribute - default is objectBoundingBox
112
short coordSystemType;
113         String JavaDoc s = SVGUtilities.getChainableAttributeNS
114             (paintElement, null, SVG_GRADIENT_UNITS_ATTRIBUTE, ctx);
115         if (s.length() == 0) {
116             coordSystemType = SVGUtilities.OBJECT_BOUNDING_BOX;
117         } else {
118             coordSystemType = SVGUtilities.parseCoordinateSystem
119                 (paintElement, SVG_GRADIENT_UNITS_ATTRIBUTE, s);
120         }
121
122         // additional transform to move to objectBoundingBox coordinate system
123
if (coordSystemType == SVGUtilities.OBJECT_BOUNDING_BOX) {
124             transform = SVGUtilities.toObjectBBox(transform,
125                                                   paintedNode);
126         }
127         UnitProcessor.Context uctx
128             = UnitProcessor.createContext(ctx, paintElement);
129
130         float r = SVGUtilities.convertLength(rStr,
131                                              SVG_R_ATTRIBUTE,
132                                              coordSystemType,
133                                              uctx);
134     // A value of zero will cause the area to be painted as a single color
135
// using the color and opacity of the last gradient stop.
136
if (r == 0) {
137             return colors[colors.length-1];
138         } else {
139             Point2D JavaDoc c = SVGUtilities.convertPoint(cxStr,
140                                                   SVG_CX_ATTRIBUTE,
141                                                   cyStr,
142                                                   SVG_CY_ATTRIBUTE,
143                                                   coordSystemType,
144                                                   uctx);
145
146             Point2D JavaDoc f = SVGUtilities.convertPoint(fxStr,
147                                                   SVG_FX_ATTRIBUTE,
148                                                   fyStr,
149                                                   SVG_FY_ATTRIBUTE,
150                                                   coordSystemType,
151                                                   uctx);
152
153             // <!> FIXME: colorSpace ignored for radial gradient at this time
154
return new RadialGradientPaint(c,
155                                            r,
156                                            f,
157                                            offsets,
158                                            colors,
159                                            spreadMethod,
160                                            RadialGradientPaint.SRGB,
161                                            transform);
162         }
163     }
164 }
165
Popular Tags