简体   繁体   English

Apache Batik:修改SVG Dom后如何强制更新JSVGComponent视图

[英]Apache Batik: How to force JSVGComponent view to update when SVG Dom modified

Using Apache Batik, 使用Apache Batik,

Trying to get the JSVGComponent to update (repaint) after adding a new svg element to the DOM. 将新的svg元素添加到DOM后,尝试使JSVGComponent更新(重新绘制)。 I know about ALWAYS_DYNAMIC, and that is set. 我知道ALWAYS_DYNAMIC,并且已经设定。 The new element(s) are successfully added to the document, but the only way I can get the component to repaint is to resize the view. 新元素已成功添加到文档中,但是我可以使组件重新绘制的唯一方法是调整视图的大小。 Upon resize, the new elements pop (appear) into place. 调整大小后,新元素会弹出(显示)到位。 I have also tried to add the elements via a Runnable using the UpdateManager and the RunnableQueue. 我还尝试使用UpdateManager和RunnableQueue通过Runnable添加元素。 Again, the elements are successfully added, but don't appear until the component is resized (by resizing the housing frame). 同样,元素已成功添加,但直到调整组件大小(通过调整外壳框架的大小)后才显示。 I have also tried invoking the repaint via a RunQ Runnable, but that also does not work (tho the repaint is actually called). 我也尝试过通过RunQ Runnable调用重绘,但这也行不通(实际上称为重绘)。

I think I may have to actually go into the GVT bridge, but I'd sure rather not. 我认为我可能必须实际进入GVT桥梁,但我肯定不会。

Help extraordinarily appreciated. 帮助非常感谢。 Thx. 谢谢。

You can do 你可以做

svgCanvas.setDocument(doc);

It's works for an JSVGCanvas, may be it will works for you. 它适用于JSVGCanvas,也许对您有用。

You can also try method : 您也可以尝试方法:

setSVGDocument(doc);

I found this : 我找到了这个 :

There are two common causes for this. 有两个常见原因。 The first is that the JSVGCanvas doesn't know it's a dynamic document. 首先是JSVGCanvas不知道它是动态文档。 Normally Batik detects this by looking for script elements but when you modify the document from Java it can't tell. 通常,Batik通过查找脚本元素来检测到此错误,但是当您从Java修改文档时却无法分辨。 So call myJSVGCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC); 因此调用myJSVGCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC); before loading the document (with setURI, setDocument, setSVGDocument, etc.). 在加载文档之前(使用setURI,setDocument,setSVGDocument等)。

The second common reason is that the changes aren't made in the UpdateManager 's thread. 第二个常见原因是未在UpdateManager的线程中进行更改。 You can run code in the UpdateManager 's thread with the following: UpdateManager um = JSVGCanvas.getUpdateManager(); 您可以使用以下命令在UpdateManager的线程中运行代码:UpdateManager um = JSVGCanvas.getUpdateManager(); um.getUpdateRunnableQueue().invokeLater(Runnable); um.getUpdateRunnableQueue()。invokeLater(Runnable); NOTE: The update manager only becomes available after the first rendering completes. 注意:更新管理器仅在第一个渲染完成后才可用。 You can be notified when this happens by registering a GVTTreeRendererListener. 通过注册GVTTreeRendererListener可以在发生这种情况时收到通知。

But i don't try this yet. 但是我还没有尝试。

Almost same problem for me : I had to dynamically modifiy the background of boxes (polygon node). 对我来说几乎是相同的问题:我不得不动态修改框的背景(多边形节点)。

According to MelodyBibi, it works with setSVGDocument(doc); 据MelodyBibi说,它与setSVGDocument(doc); but the SVG flashes during refresh. 但SVG在刷新期间会闪烁。 It's not very appreciable. 这不是很明显。

My (better) solution is to call svgCanvas.getCanvasGraphicsNode().fireGraphicsNodeChangeCompleted(); 我(更好)的解决方案是调用svgCanvas.getCanvasGraphicsNode().fireGraphicsNodeChangeCompleted(); after modifying the DOM model using polygonElement.setAttribute("fill", "red"); 在使用polygonElement.setAttribute("fill", "red");修改DOM模型之后polygonElement.setAttribute("fill", "red"); on the appropriate Node. 在适当的节点上。

I also added JSVGCanvas.ALWAYS_DYNAMIC before the first loading of the document. 在首次加载文档之前,我还添加了JSVGCanvas.ALWAYS_DYNAMIC

Now my SVG refreshes in realtime, without blinking. 现在,我的SVG实时刷新,而不会闪烁。

Hoping this will help you. 希望对您有帮助。

I had some unexplainable visual artifacts that would only disappear by doing a slight drag, zoom etc. In other words a transformation. 我有一些无法解释的视觉伪像,这些视觉伪像只会通过进行轻微的拖动,缩放等操作而消失。换句话说,就是一种转换。

The solution was to set the rendering transform to the existing one, thus forcing the canvas to re-render. 解决方案是将渲染转换设置为现有转换,从而迫使画布重新渲染。

canvas.setRenderingTransform(canvas.getRenderingTransform());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM