【问题标题】:JSVGCanvas does not update the graphic after new elements being addedJSVGCanvas 在添加新元素后不更新图形
【发布时间】: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 等)不起作用。我现在不知道该怎么办...

提前谢谢你。

问候 托比亚斯

【问题讨论】:

    标签: java apache svg batik


    【解决方案1】:

    SVG 元素必须在 SVG 命名空间中创建。因此它们不能使用 createElement 创建,必须使用 createElementNS 创建

    Element createElement = svgDocument.createElement("g");
    

    不正确,需要这样写……

    Element createElement = svgDocument.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "g");
    

    我不确定 SVGUtilities 是做什么的,因为您没有提供该实现,但这也可能是错误的。

    【讨论】:

    • 但是当我将文档打印到控制台时,为什么我会得到正确的结果。我在资源管理器中显示它,它显示了正确的图形。感谢您的提示,我将其更改为正确的命名空间,但这并没有改变任何事情。
    • 你也修复了 SVGUtilities 吗?
    • SVGUtilities 只是创建一个元素。我将方法从“createElement”更改为“createElementNS”
    • 还没有,我是否也必须为“element.setAttribute(..)”添加一个命名空间?如果是,有哪些名字?是否还有像 SVGDOMImplementation.SVG_NAMESPACE_URI 这样的预先实现的命名空间?
    • 你不需要改变 setAttribute。
    猜你喜欢
    • 2018-01-06
    • 2020-04-16
    • 2012-03-09
    • 1970-01-01
    • 2014-12-29
    • 2021-04-04
    • 2014-09-10
    • 2019-12-20
    • 1970-01-01
    相关资源
    最近更新 更多