KickJava   Java API By Example, From Geeks To Geeks.

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


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.LinearGradientPaint;
26 import org.apache.batik.ext.awt.MultipleGradientPaint;
27 import org.apache.batik.gvt.GraphicsNode;
28 import org.w3c.dom.Element JavaDoc;
29
30 /**
31  * Bridge class for the <linearGradient> element.
32  *
33  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
34  * @version $Id: SVGLinearGradientElementBridge.java,v 1.11 2004/08/18 07:12:35 vhardy Exp $
35  */

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

42     public SVGLinearGradientElementBridge() {}
43
44     /**
45      * Returns 'linearGradient'.
46      */

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

64     protected
65         Paint JavaDoc buildGradient(Element JavaDoc paintElement,
66                             Element JavaDoc paintedElement,
67                             GraphicsNode paintedNode,
68                             MultipleGradientPaint.CycleMethodEnum spreadMethod,
69                             MultipleGradientPaint.ColorSpaceEnum colorSpace,
70                             AffineTransform JavaDoc transform,
71                             Color JavaDoc [] colors,
72                             float [] offsets,
73                             BridgeContext ctx) {
74
75         // 'x1' attribute - default is 0%
76
String JavaDoc x1Str = SVGUtilities.getChainableAttributeNS
77             (paintElement, null, SVG_X1_ATTRIBUTE, ctx);
78         if (x1Str.length() == 0) {
79             x1Str = SVG_LINEAR_GRADIENT_X1_DEFAULT_VALUE;
80         }
81
82         // 'y1' attribute - default is 0%
83
String JavaDoc y1Str = SVGUtilities.getChainableAttributeNS
84             (paintElement, null, SVG_Y1_ATTRIBUTE, ctx);
85         if (y1Str.length() == 0) {
86             y1Str = SVG_LINEAR_GRADIENT_Y1_DEFAULT_VALUE;
87         }
88
89         // 'x2' attribute - default is 100%
90
String JavaDoc x2Str = SVGUtilities.getChainableAttributeNS
91             (paintElement, null, SVG_X2_ATTRIBUTE, ctx);
92         if (x2Str.length() == 0) {
93             x2Str = SVG_LINEAR_GRADIENT_X2_DEFAULT_VALUE;
94         }
95
96         // 'y2' attribute - default is 0%
97
String JavaDoc y2Str = SVGUtilities.getChainableAttributeNS
98             (paintElement, null, SVG_Y2_ATTRIBUTE, ctx);
99         if (y2Str.length() == 0) {
100             y2Str = SVG_LINEAR_GRADIENT_Y2_DEFAULT_VALUE;
101         }
102
103         // 'gradientUnits' attribute - default is objectBoundingBox
104
short coordSystemType;
105         String JavaDoc s = SVGUtilities.getChainableAttributeNS
106             (paintElement, null, SVG_GRADIENT_UNITS_ATTRIBUTE, ctx);
107         if (s.length() == 0) {
108             coordSystemType = SVGUtilities.OBJECT_BOUNDING_BOX;
109         } else {
110             coordSystemType = SVGUtilities.parseCoordinateSystem
111                 (paintElement, SVG_GRADIENT_UNITS_ATTRIBUTE, s);
112         }
113
114         // additional transform to move to objectBoundingBox coordinate system
115
if (coordSystemType == SVGUtilities.OBJECT_BOUNDING_BOX) {
116             transform = SVGUtilities.toObjectBBox(transform, paintedNode);
117         }
118         UnitProcessor.Context uctx
119             = UnitProcessor.createContext(ctx, paintElement);
120
121         Point2D JavaDoc p1 = SVGUtilities.convertPoint(x1Str,
122                                                SVG_X1_ATTRIBUTE,
123                                                y1Str,
124                                                SVG_Y1_ATTRIBUTE,
125                                                coordSystemType,
126                                                uctx);
127
128         Point2D JavaDoc p2 = SVGUtilities.convertPoint(x2Str,
129                                                SVG_X2_ATTRIBUTE,
130                                                y2Str,
131                                                SVG_Y2_ATTRIBUTE,
132                                                coordSystemType,
133                                                uctx);
134
135     // If x1 = x2 and y1 = y2, then the area to be painted will be painted
136
// as a single color using the color and opacity of the last gradient
137
// stop.
138
if (p1.getX() == p2.getX() && p1.getY() == p2.getY()) {
139             return colors[colors.length-1];
140     } else {
141         return new LinearGradientPaint(p1,
142                        p2,
143                        offsets,
144                        colors,
145                        spreadMethod,
146                        colorSpace,
147                        transform);
148     }
149     }
150 }
151
Popular Tags