KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > svggen > SVGPolygon


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.svggen;
19
20 import java.awt.Polygon JavaDoc;
21 import java.awt.geom.PathIterator JavaDoc;
22
23 import org.w3c.dom.Element JavaDoc;
24
25 /**
26  * Utility class that converts a Polygon object into
27  * an SVG element.
28  *
29  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
30  * @version $Id: SVGPolygon.java,v 1.10 2004/08/18 07:15:08 vhardy Exp $
31  */

32 public class SVGPolygon extends SVGGraphicObjectConverter {
33     /**
34      * @param generatorContext used to build Elements
35      */

36     public SVGPolygon(SVGGeneratorContext generatorContext) {
37         super(generatorContext);
38     }
39
40     /**
41      * @param polygon polygon object to convert to SVG
42      */

43     public Element JavaDoc toSVG(Polygon JavaDoc polygon) {
44         Element JavaDoc svgPolygon =
45             generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI,
46                                                         SVG_POLYGON_TAG);
47         StringBuffer JavaDoc points = new StringBuffer JavaDoc(" ");
48         PathIterator JavaDoc pi = polygon.getPathIterator(null);
49         float seg[] = new float[6];
50         int segType = 0;
51         while(!pi.isDone()){
52             segType = pi.currentSegment(seg);
53             switch(segType){
54             case PathIterator.SEG_MOVETO:
55                 appendPoint(points, seg[0], seg[1]);
56                 break;
57             case PathIterator.SEG_LINETO:
58                 appendPoint(points, seg[0], seg[1]);
59                 break;
60             case PathIterator.SEG_CLOSE:
61                 break;
62             case PathIterator.SEG_QUADTO:
63             case PathIterator.SEG_CUBICTO:
64             default:
65                 throw new Error JavaDoc();
66             }
67             pi.next();
68         } // while !isDone
69

70         svgPolygon.setAttributeNS(null,
71                                   SVG_POINTS_ATTRIBUTE,
72                                   points.substring(0, points.length() - 1));
73
74         return svgPolygon;
75     }
76
77     /**
78      * Appends a coordinate to the path data
79      */

80     private void appendPoint(StringBuffer JavaDoc points, float x, float y){
81         points.append(doubleString(x));
82         points.append(SPACE);
83         points.append(doubleString(y));
84         points.append(SPACE);
85     }
86 }
87
Popular Tags