1 18 package org.apache.batik.svggen; 19 20 import java.awt.Polygon ; 21 import java.awt.geom.PathIterator ; 22 23 import org.w3c.dom.Element ; 24 25 32 public class SVGPolygon extends SVGGraphicObjectConverter { 33 36 public SVGPolygon(SVGGeneratorContext generatorContext) { 37 super(generatorContext); 38 } 39 40 43 public Element toSVG(Polygon polygon) { 44 Element svgPolygon = 45 generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI, 46 SVG_POLYGON_TAG); 47 StringBuffer points = new StringBuffer (" "); 48 PathIterator 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 (); 66 } 67 pi.next(); 68 } 70 svgPolygon.setAttributeNS(null, 71 SVG_POINTS_ATTRIBUTE, 72 points.substring(0, points.length() - 1)); 73 74 return svgPolygon; 75 } 76 77 80 private void appendPoint(StringBuffer 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 |