KickJava   Java API By Example, From Geeks To Geeks.

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


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

34 public class SVGRectangle 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 SVGRectangle(SVGGeneratorContext generatorContext) {
44         super(generatorContext);
45         svgLine = new SVGLine(generatorContext);
46     }
47
48     /**
49      * @param rect rectangle object to convert to SVG
50      */

51     public Element JavaDoc toSVG(Rectangle2D JavaDoc rect) {
52         return toSVG((RectangularShape JavaDoc)rect);
53     }
54
55
56     /**
57      * In the Java 2D API, arc width/height are used
58      * as absolute values.
59      *
60      * @param rect rectangle object to convert to SVG
61      */

62     public Element JavaDoc toSVG(RoundRectangle2D JavaDoc rect) {
63         Element JavaDoc svgRect = toSVG((RectangularShape JavaDoc)rect);
64         if(svgRect != null && svgRect.getTagName() == SVG_RECT_TAG){
65             svgRect.setAttributeNS(null, SVG_RX_ATTRIBUTE,
66                                    doubleString(Math.abs(rect.getArcWidth()/2)));
67             svgRect.setAttributeNS(null, SVG_RY_ATTRIBUTE,
68                                    doubleString(Math.abs(rect.getArcHeight()/2)));
69         }
70
71         return svgRect;
72     }
73
74
75     /**
76      * @param rect rectangle object to convert to SVG
77      */

78     private Element JavaDoc toSVG(RectangularShape JavaDoc rect) {
79         if(rect.getWidth() > 0 && rect.getHeight() > 0){
80             Element JavaDoc svgRect =
81                 generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI,
82                                                             SVG_RECT_TAG);
83             svgRect.setAttributeNS(null, SVG_X_ATTRIBUTE, doubleString(rect.getX()));
84             svgRect.setAttributeNS(null, SVG_Y_ATTRIBUTE, doubleString(rect.getY()));
85             svgRect.setAttributeNS(null, SVG_WIDTH_ATTRIBUTE,
86                                    doubleString(rect.getWidth()));
87             svgRect.setAttributeNS(null, SVG_HEIGHT_ATTRIBUTE,
88                                    doubleString(rect.getHeight()));
89             
90             return svgRect;
91         }
92         else{
93             // Handle degenerate cases
94
if(rect.getWidth() == 0 && rect.getHeight() > 0){
95                 // Degenerate to a line
96
Line2D JavaDoc line = new Line2D.Double JavaDoc(rect.getX(), rect.getY(), rect.getX(),
97                                                 rect.getY() + rect.getHeight());
98                 return svgLine.toSVG(line);
99             }
100             else if(rect.getWidth() > 0 && rect.getHeight() == 0){
101                 // Degenerate to a line
102
Line2D JavaDoc line = new Line2D.Double JavaDoc(rect.getX(), rect.getY(),
103                                                 rect.getX() + rect.getWidth(),
104                                                 rect.getY());
105                 return svgLine.toSVG(line);
106             }
107             return null;
108         }
109     }
110 }
111
Popular Tags