【发布时间】:2016-08-13 01:20:19
【问题描述】:
使用:Apache batik (SVG)、NetBeans IDE、NetBeans 平台、Java
我有一个带有蜡染和 JSVGCanvas 组件的 SVG 应用程序。
我也调用了 svg.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
这是 SVG 文件:
<svg width="500" height="400" viewBox="0 0 250 200" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="hi" style="color:black; stroke:currentColor; stroke-width:0.4;">
<g id="ks">
<text id="textX" x="0" y="5" style="font-family:Arial; font-size:4.00pt; text-anchor:start">100%</text>
<text id="textY" x="5" y="105" style="font-family:Arial; font-size:4.00pt; text-anchor:start">0%</text>
</g>
</g>
这个简单的 SVG 首先像现在一样显示在应用程序中。现在我从应用程序中获得了新的输入,并且需要将新元素添加到 SVG 文档中。 我正在使用从 JSVGCanvas 获得的 SVGDocument 执行此操作。 这是一个例子:
Runnable run = new Runnable()
{
@Override
public void run()
{
Element elementById = SVGUtilities.getElementById(svgDocument, "g", "hi");
Element createElement = svgDocument.createElement("g");
createElement.setAttribute("id", "myID");
Element rect = SVGUtilities.createRect(
svgDocument, String.valueOf(xValue), yValue, String.valueOf(BARWIDTH),
String.valueOf(barHeight), color);
createElement.appendChild(rect);
elementById.appendChild(createElement);
}
};
为了执行更改,我现在将这些行包装在 Runnable 中并将其放入 UpdateManager 队列中。
RunnableQueue updateRunnableQueue = updateManager.getUpdateRunnableQueue();
updateRunnableQueue.invokeAndWait(run);
((JSVGCanvas)canvas).setSVGDocument((SVGDocument)doc);
JSVGCanvas 不更新图形。
一些附加信息:
- 我也尝试使用 invokeLater(run);这也不起作用
- 我不更改文件,只更改 SVGDocument。所以我不将更改写入文件。我只是更改了“文档”实例。
- 我将文档实例打印到控制台进行日志记录,结果正是我想要的,因此文档确实包含正确的新元素。
控制台中 SVG 的结果是:
<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" contentScriptType="text/ecmascript" width="500" zoomAndPan="magnify" contentStyleType="text/css" viewBox="0 0 250 200" height="400" preserveAspectRatio="xMidYMid meet" version="1.0">
<g style="color:black; stroke:currentColor; stroke-width:0.4;" id="hi">
<g id="ks">
<text x="0" id="textX" y="5" style="font-family:Arial; font-size:4.00pt; text-anchor:start">100%</text>
<text x="5" id="textY" y="105" style="font-family:Arial; font-size:4.00pt; text-anchor:start">0%</text>
</g>
<g id="25.0">
<rect x="20.0" width="20.0" y="5.0" height="7.142857142857142" style="fill:#ff0000"/>
</g>
</g>
您知道为什么我的图形没有更新吗?我搜索了几天,但我的更改(UpdateManager 队列、setSVGDocument 等)不起作用。我现在不知道该怎么办...
提前谢谢你。
问候 托比亚斯
【问题讨论】: