使用 Apache Batik 绘制 SVG 路径元素
Draw a SVG Path element with Apache Batik
目前我正在学习 SVG,现在我想通过 Java API 绘制 SVG。目前我正在查看 Apache Batik,但请随时提出不同的解决方案。
我现在要做的是绘制一个路径元素来描述一个填充的四分之一圆。使用纯 SVG 和文本编辑器,这没问题,看起来像这样:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<path d="M50,50 L30,50 A20,20 0 0,1 50,30 z"
style="fill:black;"
/>
我怎样才能对 Batik 做同样的事情。在文档中,我只能找到 drawArc 方法,但这只会绘制四分之一圆的方舟部分,如下所示:
public class SoExampleMain {
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 g = new SVGGraphics2D(document);
g.drawArc(30, 30, 40, 40, 90, 90);
// we want to use CSS style attributes
boolean useCSS = true;
// Finally, stream out SVG to the standard output using
// UTF-8 encoding.
Writer out = new OutputStreamWriter(new FileOutputStream(new File("C:\Users\Scyla101\Desktop\batik-test.svg")), "UTF-8");
g.stream(out, useCSS);
}
}
有没有办法获得与我在带有路径元素的常规 SVG 中获得的结果相同的结果。
作为辅助节点,有没有关于蜡染的好教程API?
问候
看:
http://www.svgopen.org/2002/papers/deweese_hardy__batik_intro/
http://www.svgopen.org/2002/papers/kormann__developing_svg_apps_with_batik/
http://www.udel.edu/CIS/software/dist/batik-1.6/docs/javaScripting.html
http://xmlgraphics.apache.org/batik/
http://mcc.id.au/2007/09/batik-course/
http://wiki.apache.org/xmlgraphics-batik/
http://people.apache.org/~deweese/ac2003/ApacheCON2003.pdf
我会使用 JSVGCanvas:
public class SVGApplication {
public static void main(final String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@SuppressWarnings("unused")
@Override
public void run() {
new SVGApplication();
}
});
}
public SVGApplication() {
final JFrame frame = new JFrame();
final JPanel panel = new JPanel();
final JSVGCanvas svgCanvas = new JSVGCanvas();
frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(panel, java.awt.BorderLayout.CENTER);
svgCanvas.setDocumentState(AbstractJSVGComponent.ALWAYS_DYNAMIC);
svgCanvas.setDocument(buildDocument());
panel.add(svgCanvas);
frame.pack();
frame.setVisible(true);
}
private Document buildDocument() {
final DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
final String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
final Document doc = impl.createDocument(svgNS, "svg", null);
// get the root element (the svg element)
final Element svgRoot = doc.getDocumentElement();
// create the path element
final Element element = doc.createElementNS(svgNS, "path");
element.setAttribute("d", "M50,50 L30,50 A20,20 0 0,1 50,30 z");
element.setAttribute("style", "fill:black;");
svgRoot.appendChild(element);
return doc;
}
}
要显示 svg 路径,您可以使用 JavaFX:
public class Main extends Application {
@Override
public void start(final Stage primaryStage) {
final VBox root = new VBox(createPath());
final Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
private SVGPath createPath() {
final SVGPath arc = new SVGPath();
arc.setContent("M50,50 L30,50 A20,20 0 0,1 50,30 z");
arc.setFill(Color.BLACK);
return arc;
}
public static void main(final String[] args) {
launch(args);
}
}
问候
目前我正在学习 SVG,现在我想通过 Java API 绘制 SVG。目前我正在查看 Apache Batik,但请随时提出不同的解决方案。
我现在要做的是绘制一个路径元素来描述一个填充的四分之一圆。使用纯 SVG 和文本编辑器,这没问题,看起来像这样:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<path d="M50,50 L30,50 A20,20 0 0,1 50,30 z"
style="fill:black;"
/>
我怎样才能对 Batik 做同样的事情。在文档中,我只能找到 drawArc 方法,但这只会绘制四分之一圆的方舟部分,如下所示:
public class SoExampleMain {
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 g = new SVGGraphics2D(document);
g.drawArc(30, 30, 40, 40, 90, 90);
// we want to use CSS style attributes
boolean useCSS = true;
// Finally, stream out SVG to the standard output using
// UTF-8 encoding.
Writer out = new OutputStreamWriter(new FileOutputStream(new File("C:\Users\Scyla101\Desktop\batik-test.svg")), "UTF-8");
g.stream(out, useCSS);
}
}
有没有办法获得与我在带有路径元素的常规 SVG 中获得的结果相同的结果。
作为辅助节点,有没有关于蜡染的好教程API?
问候
看:
http://www.svgopen.org/2002/papers/deweese_hardy__batik_intro/
http://www.svgopen.org/2002/papers/kormann__developing_svg_apps_with_batik/
http://www.udel.edu/CIS/software/dist/batik-1.6/docs/javaScripting.html
http://xmlgraphics.apache.org/batik/
http://mcc.id.au/2007/09/batik-course/
http://wiki.apache.org/xmlgraphics-batik/
http://people.apache.org/~deweese/ac2003/ApacheCON2003.pdf
我会使用 JSVGCanvas:
public class SVGApplication {
public static void main(final String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@SuppressWarnings("unused")
@Override
public void run() {
new SVGApplication();
}
});
}
public SVGApplication() {
final JFrame frame = new JFrame();
final JPanel panel = new JPanel();
final JSVGCanvas svgCanvas = new JSVGCanvas();
frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(panel, java.awt.BorderLayout.CENTER);
svgCanvas.setDocumentState(AbstractJSVGComponent.ALWAYS_DYNAMIC);
svgCanvas.setDocument(buildDocument());
panel.add(svgCanvas);
frame.pack();
frame.setVisible(true);
}
private Document buildDocument() {
final DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
final String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
final Document doc = impl.createDocument(svgNS, "svg", null);
// get the root element (the svg element)
final Element svgRoot = doc.getDocumentElement();
// create the path element
final Element element = doc.createElementNS(svgNS, "path");
element.setAttribute("d", "M50,50 L30,50 A20,20 0 0,1 50,30 z");
element.setAttribute("style", "fill:black;");
svgRoot.appendChild(element);
return doc;
}
}
要显示 svg 路径,您可以使用 JavaFX:
public class Main extends Application {
@Override
public void start(final Stage primaryStage) {
final VBox root = new VBox(createPath());
final Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
private SVGPath createPath() {
final SVGPath arc = new SVGPath();
arc.setContent("M50,50 L30,50 A20,20 0 0,1 50,30 z");
arc.setFill(Color.BLACK);
return arc;
}
public static void main(final String[] args) {
launch(args);
}
}
问候