问题描述
我正在尝试使用 Java 开始使用基本的 SVG 可视化编辑器(您知道,拖动创建一个新的矩形或圆形、颜色、缩放手柄等)。 Batik 看起来很健壮而且功能齐全。我会考虑拆开 GLIPS,但它非常胖,我宁愿从头开始接受这方面的教育。 Batik 演示(允许打开和渲染任何格式良好的 SVG)似乎可以正常编译和运行。希望我可以将其中的一些用作渲染器。但是,我现在想弄清楚如何实例化 SVG DOM 以生成用于后续渲染的元素。
以下代码是从 https://xmlgraphics.apache.org/batik/using/dom-api.html 最少修改的:
// note SVGDOMImplementation does NOT appear in batik-svg-dom-1.14.jar! see:
// https://stackoverflow.com/questions/34178503/java-batik-classnotfoundexception-org-apache-batik-dom-svg-svgdomimplementatio
// https://stackoverflow.com/questions/30092651/where-has-org-apache-batik-dom-svg-svgdomimplementation-gone
//import org.apache.batik.dom.svg.SVGDOMImplementation; // does not exist
import org.apache.batik.anim.dom.SVGDOMImplementation; // oddly,located here
//import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class SVGApplication {
public static void main(String[] args) {
//https://xmlgraphics.apache.org/batik/using/dom-api.html
// someone wrote that it may be necessary to cast this,but it'ś not the case
DOMImplementation impl = /*(SVGDOMImplementation)*/ SVGDOMImplementation.getDOMImplementation();
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
Document doc = impl.createDocument(svgNS,"svg",null);
// Get the root element (the 'svg' element).
Element svgRoot = doc.getDocumentElement();
// Set the width and height attributes on the root 'svg' element.
svgRoot.setAttributeNS(null,"width","400");
svgRoot.setAttributeNS(null,"height","450");
// Create the rectangle.
Element rectangle = doc.createElementNS(svgNS,"rect");
rectangle.setAttributeNS(null,"x","10");
rectangle.setAttributeNS(null,"y","20");
rectangle.setAttributeNS(null,"100");
rectangle.setAttributeNS(null,"50");
rectangle.setAttributeNS(null,"fill","red");
// Attach the rectangle to the root 'svg' element.
svgRoot.appendChild(rectangle);
}
}
代码编译到矩形的创建。通常,当缺少某些调用时,编译器会非常主动。但在这种情况下,每当我取消对单个元素创建行或其他任何内容的注释时,它只会显示“在课堂中发现错误”。它没有提供更多细节。当我注释掉矩形创建和附加时,它编译没有错误。
请注意,SVGDOMImplementation 库所在的位置似乎是一个长期存在的问题(1,2),但我似乎已经解决了这个问题(它莫名其妙地被塞进了 -动画库:请参阅 -import- 命令)。在引用的页面上也有一些关于需要或能够转换为 (SVGDOMImplementation) 的讨论,尽管在这种情况下这样做似乎没有区别,至少在两者都编译的程度上。这些点可能是也可能不是问题所在(我模糊地记得有人暗示编译器在这种情况下无法识别错误可能是由于某些库冲突。)但无论如何我都看不到如何解决。有人吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)