in order for an instance of SVGGraphics2D to build the SVG content (the DOM tree), an instance of a Document class is needed. The DOM tree is an in-memory representation of the SVG document, which can be further manipulated by the user using DOM API or be streamed out by a Writer object.
The following example program demonstrates how to generate SVG content from Java graphics.
import java.awt.Rectangle;
import java.awt.Graphics2D;
import java.awt.Color;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.IOException;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.dom.GenericDOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.DOMImplementation;
public class TestSVGGen {
public void paint(Graphics2D g2d) {
g2d.setPaint(Color.red);
g2d.fill(new Rectangle(10, 10, 100, 100));
}
public static void main(String[] args) throws IOException {
// Get a DOMImplementation.
DOMImplementation domImpl =
GenericDOMImplementation.getDOMImplementation();
// Create an instance of org.w3c.dom.Document.
String svgNS = "http://www.w3.org/2000/svg";
Document document = domImpl.createDocument(svgNS, "svg", null);
// Create an instance of the SVG Generator.
SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
// Ask the test to render into the SVG Graphics2D implementation.
TestSVGGen test = new TestSVGGen();
test.paint(svgGenerator);
// Finally, stream out SVG to the standard output using
// UTF-8 encoding.
boolean useCSS = true; // we want to use CSS style attributes
Writer out = new OutputStreamWriter(System.out, "UTF-8");
svgGenerator.stream(out, useCSS);
}
} We can see that generating SVG content from our TestSVGGen instance is a three step process:
Create an instance of org.w3c.dom.Document that the generator will use to build its XML content, and create an SVG generator using the Document instance. // Get a DOMImplementation. DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
// Create an instance of org.w3c.dom.Document.
String svgNS = "http://www.w3.org/2000/svg";
Document document = domImpl.createDocument(svgNS, "svg", null);
// Create an instance of the SVG Generator.
SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
Invoke the rendering code on our SVG generator. In our example, we invoke TestSVGGen 's paint method: // Ask the test to render into the SVG Graphics2D implementation. TestSVGGen test = new TestSVGGen(); test.paint(svgGenerator);
Stream out the SVG content. The SVG generator can stream its content into any java.io.Writer . In our example, we stream the content to the standard output stream: // Finally, stream out SVG to the standard output using // UTF-8 encoding. boolean useCSS = true; // we want to use CSS style attributes Writer out = new OutputStreamWriter(System.out, "UTF-8"); svgGenerator.stream(out, useCSS);
SVG has two ways to specify styling properties , such as the fill color: presentation attributes (one XML attribute per property) or the CSS style attribute (any number of properties in one CSS inline stylesheet). The useCss parameter allows the user to control that option.
0 件のコメント:
コメントを投稿