KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > extension > svg > BatikStarElementBridge


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.extension.svg;
19
20 import java.awt.geom.GeneralPath JavaDoc;
21
22 import org.apache.batik.bridge.Bridge;
23 import org.apache.batik.bridge.BridgeContext;
24 import org.apache.batik.bridge.BridgeException;
25 import org.apache.batik.bridge.SVGDecoratedShapeElementBridge;
26 import org.apache.batik.bridge.SVGUtilities;
27 import org.apache.batik.bridge.UnitProcessor;
28 import org.apache.batik.gvt.ShapeNode;
29 import org.w3c.dom.Element JavaDoc;
30
31 /**
32  * Bridge class for a star element.
33  *
34  * @author <a HREF="mailto:thomas.deweese@kodak.com">Thomas Deweese</a>
35  * @version $Id: BatikStarElementBridge.java,v 1.5 2004/08/18 07:14:21 vhardy Exp $
36  */

37 public class BatikStarElementBridge
38     extends SVGDecoratedShapeElementBridge
39     implements BatikExtConstants {
40
41     /**
42      * Constructs a new bridge for the &lt;rect> element.
43      */

44     public BatikStarElementBridge() { /* nothing */ }
45
46     /**
47      * Returns the SVG namespace URI.
48      */

49     public String JavaDoc getNamespaceURI() {
50         return BATIK_EXT_NAMESPACE_URI;
51     }
52
53     /**
54      * Returns 'rect'.
55      */

56     public String JavaDoc getLocalName() {
57         return BATIK_EXT_STAR_TAG;
58     }
59
60     /**
61      * Returns a new instance of this bridge.
62      */

63     public Bridge getInstance() {
64         return new BatikStarElementBridge();
65     }
66
67     /**
68      * Constructs a star polygone according to the specified parameters.
69      *
70      * @param ctx the bridge context to use
71      * @param e the element that describes a rect element
72      * @param shapeNode the shape node to initialize
73      */

74     protected void buildShape(BridgeContext ctx,
75                               Element JavaDoc e,
76                               ShapeNode shapeNode) {
77
78         UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, e);
79         String JavaDoc s;
80
81         // 'cx' attribute - default is 0
82
s = e.getAttributeNS(null, SVG_CX_ATTRIBUTE);
83         float cx = 0;
84         if (s.length() != 0) {
85             cx = UnitProcessor.svgHorizontalCoordinateToUserSpace
86                 (s, SVG_CX_ATTRIBUTE, uctx);
87         }
88
89         // 'cy' attribute - default is 0
90
s = e.getAttributeNS(null, SVG_CY_ATTRIBUTE);
91         float cy = 0;
92         if (s.length() != 0) {
93             cy = UnitProcessor.svgVerticalCoordinateToUserSpace
94                 (s, SVG_CY_ATTRIBUTE, uctx);
95         }
96
97         // 'r' attribute - required
98
s = e.getAttributeNS(null, SVG_R_ATTRIBUTE);
99         float r;
100         if (s.length() == 0)
101             throw new BridgeException(e, ERR_ATTRIBUTE_MISSING,
102                                       new Object JavaDoc[] {SVG_R_ATTRIBUTE, s});
103         r = UnitProcessor.svgOtherLengthToUserSpace
104             (s, SVG_R_ATTRIBUTE, uctx);
105
106         // 'ir' attribute - required
107
s = e.getAttributeNS(null, BATIK_EXT_IR_ATTRIBUTE);
108         float ir;
109         if (s.length() == 0)
110             throw new BridgeException
111                 (e, ERR_ATTRIBUTE_MISSING,
112                  new Object JavaDoc[] {BATIK_EXT_IR_ATTRIBUTE, s});
113
114         ir = UnitProcessor.svgOtherLengthToUserSpace
115             (s, BATIK_EXT_IR_ATTRIBUTE, uctx);
116
117         // 'sides' attribute - default is 3
118
int sides = convertSides(e, BATIK_EXT_SIDES_ATTRIBUTE, 3);
119         
120         GeneralPath JavaDoc gp = new GeneralPath JavaDoc();
121         double angle, x, y;
122         for (int i=0; i<sides; i++) {
123             angle = (i)*(2*Math.PI/sides) - (Math.PI/2);
124             x = cx + ir*Math.cos(angle);
125             y = cy - ir*Math.sin(angle);
126             if (i==0)
127                 gp.moveTo((float)x, (float)y);
128             else
129                 gp.lineTo((float)x, (float)y);
130
131             angle = (i+0.5)*(2*Math.PI/sides) - (Math.PI/2);
132             x = cx + r*Math.cos(angle);
133             y = cy - r*Math.sin(angle);
134             gp.lineTo((float)x, (float)y);
135         }
136         gp.closePath();
137
138         shapeNode.setShape(gp);
139     }
140
141     /**
142      * Stolen from AbstractSVGFilterPrimitiveElementBridge.
143      * Converts on the specified filter primitive element, the specified
144      * attribute that represents an integer and with the specified
145      * default value.
146      *
147      * @param filterElement the filter primitive element
148      * @param attrName the name of the attribute
149      * @param defaultValue the default value of the attribute
150      */

151     protected static int convertSides(Element JavaDoc filterElement,
152                                         String JavaDoc attrName,
153                                         int defaultValue) {
154         String JavaDoc s = filterElement.getAttributeNS(null, attrName);
155         if (s.length() == 0) {
156             return defaultValue;
157         } else {
158             int ret = 0;
159             try {
160                 ret = SVGUtilities.convertSVGInteger(s);
161             } catch (NumberFormatException JavaDoc ex) {
162                 throw new BridgeException
163                     (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
164                      new Object JavaDoc[] {attrName, s});
165             }
166
167             if (ret <3)
168                 throw new BridgeException
169                     (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
170                      new Object JavaDoc[] {attrName, s});
171             return ret;
172         }
173     }
174 }
175
Popular Tags