KickJava   Java API By Example, From Geeks To Geeks.

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


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.geom.Arc2D JavaDoc;
21 import java.awt.geom.Line2D JavaDoc;
22 import java.awt.geom.Point2D JavaDoc;
23
24 import org.w3c.dom.Element JavaDoc;
25
26 /**
27  * Utility class that converts an Arc2D object into
28  * a corresponding SVG element, i.e., a path with an elliptical arc
29  * and optionally lines..
30  *
31  * @author <a HREF="mailto:thomas.deweese@kodak.com">Thomas DeWeese</a>
32  * @version $Id: SVGArc.java,v 1.1 2004/10/31 11:21:30 deweese Exp $
33  */

34 public class SVGArc extends SVGGraphicObjectConverter {
35     /**
36      * Line converter used for degenerate cases
37      */

38     private SVGLine svgLine;
39
40     /**
41      * @param generatorContext used to build Elements
42      */

43     public SVGArc(SVGGeneratorContext generatorContext) {
44         super(generatorContext);
45     }
46
47     /**
48      * @param arc the Arc2D object to be converted
49      */

50     public Element JavaDoc toSVG(Arc2D JavaDoc arc) {
51         if ((arc.getWidth() == 0) || (arc.getHeight() == 0)) {
52             Line2D JavaDoc line = new Line2D.Double JavaDoc
53                 (arc.getX(), arc.getY(),
54                  arc.getX() + arc.getWidth(),
55                  arc.getY() + arc.getHeight());
56             if (svgLine == null)
57                 svgLine = new SVGLine(generatorContext);
58             return svgLine.toSVG(line);
59         }
60
61         Element JavaDoc svgPath = generatorContext.domFactory.createElementNS
62             (SVG_NAMESPACE_URI, SVG_PATH_TAG);
63         StringBuffer JavaDoc d = new StringBuffer JavaDoc("");
64
65         Point2D JavaDoc startPt = arc.getStartPoint();
66         Point2D JavaDoc endPt = arc.getEndPoint();
67         double ext = arc.getAngleExtent();
68         int type = arc.getArcType();
69
70         d.append(PATH_MOVE);
71         d.append(doubleString(startPt.getX()));
72         d.append(SPACE);
73         d.append(doubleString(startPt.getY()));
74         d.append(SPACE);
75
76         d.append(PATH_ARC);
77         d.append(doubleString(arc.getWidth()/2));
78         d.append(SPACE);
79         d.append(doubleString(arc.getHeight()/2));
80         d.append(SPACE);
81         d.append("0"); // no rotation with J2D arc.
82
d.append(SPACE);
83         if (ext > 180) d.append("1"); // use large arc.
84
else d.append("0"); // use small arc.
85
d.append(SPACE);
86         if (ext > 0) d.append("0"); // sweep ccw
87
else d.append("1"); // sweep cw
88

89         d.append(SPACE);
90         d.append(doubleString(endPt.getX()));
91         d.append(SPACE);
92         d.append(doubleString(endPt.getY()));
93
94         if (type == Arc2D.CHORD) {
95             d.append(PATH_CLOSE);
96         } else if (type == Arc2D.PIE) {
97             double cx = arc.getX()+arc.getWidth()/2;
98             double cy = arc.getY()+arc.getHeight()/2;
99             d.append(PATH_LINE_TO);
100             d.append(SPACE);
101             d.append(doubleString(cx));
102             d.append(SPACE);
103             d.append(doubleString(cy));
104             d.append(SPACE);
105             d.append(PATH_CLOSE);
106         }
107         svgPath.setAttributeNS(null, SVG_D_ATTRIBUTE, d.toString());
108         return svgPath;
109     }
110 }
111
Popular Tags