W3C home > Mailing lists > Public > www-svg@w3.org > June 2000

Re: More on: How to create a SVG File in Java.

From: Dean Jackson <dean.jackson@cmis.csiro.au>
Date: Tue, 06 Jun 2000 08:54:04 +1000
Message-ID: <393C2F8C.73B509CA@cmis.csiro.au>
To: Bernd Grolig <Bernd.Grolig@ptv.de>
CC: Vincent Hardy <vincent.hardy@eng.sun.com>, www-svg@w3.org
Bernd,

I've attached a java program to create an SVG document in
Java (using the SVG DOM) and output it to an XML file.

As Vincent (for ProjectX) and Kelvin (for Xerces) have
suggested, the SVGDocument can be output as a typical
XML Document using the XML tools.

This file is org/csiro/svg/parser/CreateSVG.java in the
latest release of the CSIRO toolkit (which will happen
in about 2 hours!). It uses a file XMLWriter.java which
isn't attached to this message but included in the distribution.

Dean

--

package org.csiro.svg.parser;

import org.csiro.svg.dom.*;
import org.w3c.dom.svg.*;
import java.io.*;


// this is a sample file that shows how to create an svg document and
// write it to a file

public class CreateSVG {

  public static void main(String[] args) {

    if (args.length != 1) {
      System.out.println("usage: java org.csiro.svg.parser.CreateSVG
out.svg");
      System.exit(1);
    }
    try {
      SVGDocument svgDoc = new SVGDocumentImpl(true);  // the "true"
indicates that this is to be a stylable svg doc

      // create root <svg> element and add it to doc
      SVGSVGElement root =  (SVGSVGElement)svgDoc.createElement("svg");
      svgDoc.appendChild(root);

      // set width and height of <svg> element
      SVGLength width = root.createSVGLength();
      width.setValueAsString("15cm");
      SVGLength height = root.createSVGLength();
      height.setValue(600);
      root.setWidth(width);
      root.setHeight(height);

      // add a circle
      SVGCircleElement circle =
(SVGCircleElement)svgDoc.createElement("circle");
      SVGLength cx = root.createSVGLength();
      cx.setValue(100);
      circle.setCx(cx);
      SVGLength cy = root.createSVGLength();
      cy.setValue(100);
      circle.setCy(cy);
      circle.setAttribute("r", "50");
      circle.setAttribute("style", "fill:red; stroke:black;
stroke-width:5");
      root.appendChild(circle);

      // add a rectangle
      SVGRectElement rect =
(SVGRectElement)svgDoc.createElement("rect");
      SVGLength rectX = root.createSVGLength();
      rectX.setValueAsString("200px");
      rect.setX(rectX);
      SVGLength rectY = root.createSVGLength();
      rectY.setValueAsString("250px");
      rect.setY(rectY);
      rect.setAttribute("width", "200");
      rect.setAttribute("height", "150");
      rect.setAttribute("style", "fill:none; stroke:blue;
stroke-width:10");
      root.appendChild(rect);

      // write the svg doc to a file
      FileOutputStream out = new FileOutputStream(new File(args[0]));
      XmlWriter writer = new XmlWriter(out);
      // need to pass in the doctype declaration so that it can be put
at the top of the xml file
      writer.print(svgDoc, "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG
20000303 Stylable//EN\" \"svg-20000303-stylable.dtd\" >");
      out.close();

    } catch (IOException e) {
      System.out.println("IOException: " + e.getMessage());
      System.exit(1);
    }
    System.out.println("finished writing svg file: " + args[0]);
    System.exit(0);
  }
}
Received on Monday, 5 June 2000 18:54:28 GMT

This archive was generated by hypermail 2.3.1 : Friday, 8 March 2013 15:54:18 GMT