1 18 package org.apache.batik.svggen; 19 20 import java.awt.geom.Ellipse2D ; 21 import java.awt.geom.Line2D ; 22 23 import org.w3c.dom.Element ; 24 25 32 public class SVGEllipse extends SVGGraphicObjectConverter { 33 36 private SVGLine svgLine; 37 38 41 public SVGEllipse(SVGGeneratorContext generatorContext) { 42 super(generatorContext); 43 } 44 45 48 public Element toSVG(Ellipse2D 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 62 private Element toSVGCircle(Ellipse2D ellipse){ 63 Element 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 80 private Element toSVGEllipse(Ellipse2D ellipse){ 81 if(ellipse.getWidth() > 0 && ellipse.getHeight() > 0){ 82 Element 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 Line2D line = new Line2D.Double (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 Line2D line = new Line2D.Double (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 |