1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > apache 的batik生成svg文件和通过swing界面查看效果

apache 的batik生成svg文件和通过swing界面查看效果

时间:2023-02-26 00:24:47

相关推荐

apache 的batik生成svg文件和通过swing界面查看效果

apache的batik官网

Apache(tm) Batik SVG Toolkit - a Java-based toolkit for applications or applets that want to use images in the Scalable Vector Graphics (SVG)/batik/

maven依赖

<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.burns.batik.svg</groupId><artifactId>batik_svg_test1</artifactId><version>1.0-SNAPSHOT</version><properties><piler.source>8</piler.source><piler.target>8</piler.target></properties><dependencies><dependency><groupId>org.apache.xmlgraphics</groupId><artifactId>batik-all</artifactId><version>1.14</version></dependency><dependency><groupId>org.apache.xmlgraphics</groupId><artifactId>fop</artifactId><version>2.7</version></dependency></dependencies></project>

通过batik生成svg内容并通过界面展示生成效果

代码

package com.burns.batik.svg.test0527_1;import org.apache.batik.anim.dom.SVGDOMImplementation;import org.apache.batik.svggen.SVGGraphics2D;import org.apache.batik.swing.JSVGCanvas;import org.w3c.dom.DOMImplementation;import org.w3c.dom.Element;import org.w3c.dom.svg.SVGDocument;import javax.swing.*;import java.awt.*;import java.awt.geom.Line2D;import java.io.OutputStreamWriter;import java.io.Writer;public class ViewGeneratedSVGDemo1 {public static void main(String[] args) throws Exception{// Create an SVG document.DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;SVGDocument doc = (SVGDocument) impl.createDocument(svgNS, "svg", null);// Create a converter for this document.SVGGraphics2D g = new SVGGraphics2D(doc);// Do some drawing.// Shape circle = new Ellipse2D.Double(0, 0, 50, 50);// g.setPaint(Color.red);// g.fill(circle);// g.translate(60, 0);// g.setPaint(Color.green);// g.fill(circle);// g.translate(60, 0);// g.setPaint(Color.blue);// g.fill(circle);// g.setPaint(Color.YELLOW);g.setPaint(Color.BLACK);// g.fill(new Rectangle(10, 10, 100, 100));g.draw(new Rectangle(10, 10, 100, 100));g.draw(new Line2D.Float(110, 55, 200, 55));g.draw(new Rectangle(200, 10, 100, 100));g.setSVGCanvasSize(new Dimension(800, 300));// Populate the document root with the generated SVG content.Element root = doc.getDocumentElement();g.getRoot(root);//输出svg内容boolean useCSS = true; // we want to use CSS style attributesWriter out = new OutputStreamWriter(System.out, "UTF-8");g.stream(out, useCSS);// Display the document.JSVGCanvas canvas = new JSVGCanvas();JFrame f = new JFrame();f.getContentPane().add(canvas);canvas.setSVGDocument(doc);f.pack();f.setVisible(true);}}

运行后效果

通过batik生成svg文件内容,并查看内容,我这里是输出到控制台,然后把内容复制放到svg文件中查看的效果

代码

package com.burns.batik.svg.test0527_1;import org.apache.batik.anim.dom.SVGDOMImplementation;import org.apache.batik.dom.GenericDOMImplementation;import org.apache.batik.svggen.SVGGraphics2D;import org.apache.batik.swing.JSVGCanvas;import org.w3c.dom.DOMImplementation;import org.w3c.dom.Document;import org.w3c.dom.svg.SVGDocument;import javax.swing.*;import java.awt.*;import java.awt.geom.Line2D;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.Writer;public class TestSVGGen {public void paint(Graphics2D g) {// g2d.setPaint(Color.red);// g2d.fill(new Rectangle(10, 10, 100, 100));g.setPaint(Color.BLACK);// g.fill(new Rectangle(10, 10, 100, 100));g.draw(new Rectangle(10, 10, 100, 100));g.draw(new Line2D.Float(110, 55, 200, 55));g.draw(new Rectangle(200, 10, 100, 100));g.draw(new Line2D.Float(155, 55, 155, 155));g.draw(new Rectangle(105, 155, 100, 100));}public static void main(String[] args) throws IOException {// Get a DOMImplementation.DOMImplementation domImpl =GenericDOMImplementation.getDOMImplementation();DOMImplementation domImpl1 =SVGDOMImplementation.getDOMImplementation();// Create an instance of org.w3c.dom.Document.String svgNS = "/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 attributesWriter out = new OutputStreamWriter(System.out, "UTF-8");svgGenerator.stream(out, useCSS);// Display the document.// JSVGCanvas canvas = new JSVGCanvas();// JFrame f = new JFrame();// f.getContentPane().add(canvas);// canvas.setSVGDocument((SVGDocument) document);// f.pack();// f.setVisible(true);}}

运行后控制台输出

把内容复制放到svg文件

用浏览器查看svg文件

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。