KickJava   Java API By Example, From Geeks To Geeks.

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


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 regular polygon element.
33  *
34  * @author <a HREF="mailto:thomas.deweese@kodak.com">Thomas Deweese</a>
35  */

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

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

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

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

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

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

135     protected static int convertSides(Element JavaDoc filterElement,
136                                         String JavaDoc attrName,
137                                         int defaultValue) {
138         String JavaDoc s = filterElement.getAttributeNS(null, attrName);
139         if (s.length() == 0) {
140             return defaultValue;
141         } else {
142             int ret = 0;
143             try {
144                 ret = SVGUtilities.convertSVGInteger(s);
145             } catch (NumberFormatException JavaDoc ex) {
146                 throw new BridgeException
147                     (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
148                      new Object JavaDoc[] {attrName, s});
149             }
150
151             if (ret <3)
152                 throw new BridgeException
153                     (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
154                      new Object JavaDoc[] {attrName, s});
155             return ret;
156         }
157     }
158 }
159
Popular Tags