在Java平臺上,通過java.awt.Graphics2D抽象類來實現圖形功能,該類提供了諸如
drawRect,fillRect,以及drawString等方法。而Batik的SVGGraphics2D是一個專門的工具
,用來生成SVG文本內容而不是在屏幕上顯示或打印。
SVGGraphics2D提供了如下特點的功能
1、允許應用程序將圖形輸出成SVG格式
2、輸出為SVG的過程不需要修改任何圖形代碼
3、提供給用戶DOM API功能來處理產生的文檔
生成SVG的三個步驟:
1、創建org.w3c.dom.Document的實例,生成器利用它建立XML內容。創建一個SVG生
成器則使用Document實例。
// Get a DOMImplementation
DOMImplementation domImpl =
??? GenericDOMImplementation.getDOMImplementation();
// Create an instance of org.w3c.dom.Document
Document document = domImpl.createDocument(null, "svg", null);
// Create an instance of the SVG Generator
SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
2、在SVG生成器上調用表現代碼,如paint方法
// Ask the test to render into the SVG Graphics2D implementation
TestSVGGen test = new TestSVGGen();
test.paint(svgGenerator);
3、輸出SVG內容,生成器可以通過任何一個java.io.Writer把內容stream出來。
// Finally, stream out SVG to the standard output using UTF-8
// character to byte encoding
boolean useCSS = true; // we want to use CSS style attribute
Writer out = new OutputStreamWriter(System.out, "UTF-8");
svgGenerator.stream(out, useCSS);
注:SVG在表達屬性時可以由兩種方式,XML屬性和CSS方式。
文章來源:
http://cynest.cn/drupal/?q=node/612