调整含有JSVGCanvas在JComponent时蜡染IllegalStateException异

2019-09-28 06:34发布

我的计划似乎运作得很好,但我不断收到“IllegalStateExceptions:RunnableQueue未启动或已退出”不时,当我尝试调整我的组件。 我已经设置了documentState到ALWAYS_DYNAMIC,我已阅读,你应该使用JSVGCanvas' UpdateManager和调用的invokeLater()。 据我所知,这是第一个可用的时间之后

gvtBuildCompleted(GVTTreeBuilderEvent e)

是所谓的,所以我检查之前,我使用它,但我仍然得到异常是否正在运行。 下面的方法是通过反复线程调用,似乎会导致异常:

private void updateDomTree(final SVGComponent component, final Document doc)
{
if(component.getSvgCanvas().getUpdateManager() != null && component.getSvgCanvas().getUpdateManager().isRunning())
{
    component.getSvgCanvas().getUpdateManager().getUpdateRunnableQueue().invokeLater(new Runnable() 
    {
        public void run() 
        {
            final Node newNode = doc.getChildNodes().item(0).getFirstChild();
            //could be easier to get this value, but ... it works.
            String newNodeId = newNode.getAttributes().getNamedItem("id").getFirstChild().getNodeValue();

            NodeList nodes = component.getSvgDocument().getDocumentElement().getChildNodes();
            Node updateNode = findElementById(nodes, newNodeId);
            resizeComponent(component, doc);
            component.getSvgCanvas().getSVGDocument().adoptNode(newNode);
            component.getSvgCanvas().getSVGDocument().getDocumentElement().replaceChild(newNode, updateNode);
            component.refreshSVGCanvas();
        }
    });
}
}

实际调整大小都是在这里完成:

protected void resizeComponent(SVGComponent component, Document doc)
{
    Element svgRoot = doc.getDocumentElement();
    final int svgWidth = Integer.parseInt(svgRoot.getAttribute("width"));
    final int svgHeight = Integer.parseInt(svgRoot.getAttribute("height"));
    String[] viewBox = svgRoot.getAttribute("viewBox").split(" ");
    int viewBoxLeft = Integer.parseInt(viewBox[0]);
    int viewBoxTop = Integer.parseInt(viewBox[1]);
    final float factor = component.getScaleFactor();
    String[] viewBoxOld = component.getSvgDocument().getDocumentElement().getAttribute("viewBox").split(" ");
    int viewBoxLeftOld = Integer.parseInt(viewBoxOld[0]);
    int viewBoxTopOld = Integer.parseInt(viewBoxOld[1]);
    int xDiff = (int) ((viewBoxLeftOld - viewBoxLeft)*factor);
    int yDiff = (int) ((viewBoxTopOld - viewBoxTop)*factor);

    if ( viewBoxLeftOld != viewBoxLeft ) //If there is additional content left
    {
        component.setLocation(component.getLocation().x - xDiff, component.getLocation().y);
    }
    if ( viewBoxTopOld != viewBoxTop ) //If there is additional content right)
    {
        component.setLocation(component.getLocation().x, component.getLocation().y - yDiff);
    }

    component.getSvgDocument().getDocumentElement().setAttribute("width",""+svgWidth);
    component.getSvgDocument().getDocumentElement().setAttribute("height",""+svgHeight);
    component.getSvgDocument().getDocumentElement().setAttribute("viewBox", ""+viewBoxLeft+" "+viewBoxTop+" "+svgWidth+" "+svgHeight);
    component.setSize((int)(svgWidth*factor),(int)(svgHeight*factor));
}

方法

refreshJSVGCanvas()

电话

JSVGCanvas.setDocument(Document);
JSVGCanvas.setSize(int, int);

下面是完整的堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: RunnableQueue not started or has exited
at org.apache.batik.util.RunnableQueue.invokeLater(RunnableQueue.java:277)
at org.apache.batik.swing.svg.AbstractJSVGComponent.updateRenderingTransform(AbstractJSVGComponent.java:1057)
at org.apache.batik.swing.gvt.AbstractJGVTComponent$1.componentResized(AbstractJGVTComponent.java:237)
at java.awt.AWTEventMulticaster.componentResized(Unknown Source)
at java.awt.Component.processComponentEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

在此先感谢,我已经找遍和尝试了很多,但无法找到一个解决方案。

编辑:这是蜡染的invokeLater-方法,其中唯一的例外是实际抛出:

public void invokeLater(Runnable r) {
    if (runnableQueueThread == null) {
        throw new IllegalStateException
            ("RunnableQueue not started or has exited");
    }
    synchronized (list) {
        list.push(new Link(r));
        list.notify();
    }
}

runnableQueueThrad设置该类的run()的内部 - 的方法和在端部设置为空。 所以我想我必须做某种同步。

Answer 1:

大胆猜测,“公共无效的run()”的代码不应该是内部的另一种方法,真的是一个线程类/界面对象的所谓的构造函数(接口版本实际上)。

它删除其自己的类(如嵌套子类保护范围),并实现类上的“线程可运行”界面发出“运行()”方法,它用。

堆栈跟踪说,因为它实际上并没有这样的方法(或至少不正确申报),所以它是一个“非法状态” run方法不可用。



文章来源: Batik IllegalStateException when resizing the JComponent containing the JSVGCanvas