KickJava   Java API By Example, From Geeks To Geeks.

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


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.Ellipse2D JavaDoc;
21 import java.awt.geom.Line2D JavaDoc;
22
23 import org.w3c.dom.Element JavaDoc;
24
25 /**
26  * Utility class that converts an Ellipse2D object into
27  * a corresponding SVG element, i.e., a circle or an ellipse.
28  *
29  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
30  * @version $Id: SVGEllipse.java,v 1.10 2004/10/30 16:54:53 deweese Exp $
31  */

32 public class SVGEllipse extends SVGGraphicObjectConverter {
33     /**
34      * Line converter used for degenerate cases
35      */

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

41     public SVGEllipse(SVGGeneratorContext generatorContext) {
42         super(generatorContext);
43     }
44
45     /**
46      * @param ellipse the Ellipse2D object to be converted
47      */

48     public Element JavaDoc toSVG(Ellipse2D JavaDoc ellipse) {
49         if(ellipse.getWidth() < 0 || ellipse.getHeight() < 0){
50             return null;
51         }
52
53         if(ellipse.getWidth() == ellipse.getHeight())
54             return toSVGCircle(ellipse);
55         else
56             return toSVGEllipse(ellipse);
57     }
58
59     /**
60      * @param ellipse the Ellipse2D object to be converted to a circle
61      */

62     private Element JavaDoc toSVGCircle(Ellipse2D JavaDoc ellipse){
63         Element JavaDoc svgCircle =
64             generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI,
65                                                         SVG_CIRCLE_TAG);
66         svgCircle.setAttributeNS(null, SVG_CX_ATTRIBUTE,
67                                  doubleString(ellipse.getX() +
68                                               ellipse.getWidth()/2));
69         svgCircle.setAttributeNS(null, SVG_CY_ATTRIBUTE,
70                                  doubleString(ellipse.getY() +
71                                               ellipse.getHeight()/2));
72         svgCircle.setAttributeNS(null, SVG_R_ATTRIBUTE,
73                                  doubleString(ellipse.getWidth()/2));
74         return svgCircle;
75     }
76
77     /**
78      * @param ellipse the Ellipse2D object to be converted to an ellipse
79      */

80     private Element JavaDoc toSVGEllipse(Ellipse2D JavaDoc ellipse){
81         if(ellipse.getWidth() > 0 && ellipse.getHeight() > 0){
82             Element JavaDoc svgCircle =
83                 generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI,
84                                                             SVG_ELLIPSE_TAG);
85             svgCircle.setAttributeNS(null, SVG_CX_ATTRIBUTE,
86                                      doubleString(ellipse.getX() +
87                                                   ellipse.getWidth()/2));
88             svgCircle.setAttributeNS(null, SVG_CY_ATTRIBUTE,
89                                      doubleString(ellipse.getY() +
90                                                   ellipse.getHeight()/2));
91             svgCircle.setAttributeNS(null, SVG_RX_ATTRIBUTE,
92                                      doubleString(ellipse.getWidth()/2));
93             svgCircle.setAttributeNS(null, SVG_RY_ATTRIBUTE,
94                                      doubleString(ellipse.getHeight()/2));
95             return svgCircle;
96         }
97         else if(ellipse.getWidth() == 0 && ellipse.getHeight() > 0){
98             // Degenerate to a line
99
Line2D JavaDoc line = new Line2D.Double JavaDoc(ellipse.getX(), ellipse.getY(), ellipse.getX(),
100                                             ellipse.getY() + ellipse.getHeight());
101             if (svgLine == null)
102                 svgLine = new SVGLine(generatorContext);
103             return svgLine.toSVG(line);
104         }
105         else if(ellipse.getWidth() > 0 && ellipse.getHeight() == 0){
106             // Degenerate to a line
107
Line2D JavaDoc line = new Line2D.Double JavaDoc(ellipse.getX(), ellipse.getY(),
108                                             ellipse.getX() + ellipse.getWidth(),
109                                             ellipse.getY());
110             if (svgLine == null)
111                 svgLine = new SVGLine(generatorContext);
112             return svgLine.toSVG(line);
113         }
114         return null;
115     }
116 }
117
Popular Tags