2012-12-28 49 views
0

我似乎有一個使用Java處理SVG的問題,使用batikSVG。我可以在JSVG Canvas上很好地顯示SVG,但是當我使用getSVGDocument嘗試畫布的SVGDocument時,它似乎返回null。爲什麼是這樣,我怎樣才能得到實際的文件?JSVGCanvas.getSVGDocument()返回null?

jSVGCanvas1.setURI(new File("circle.svg").toURI().toString()); 

    jSVGCanvas1.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC); 
    SVGDocument doc = jSVGCanvas1.getSVGDocument(); 
    if(doc==null)System.out.println("null"); 

最後一行的測試,其中文檔爲空,它總是打印空。請幫忙!

回答

1

您將需要等待文檔加載併發生異步。像這樣...

jSVGCanvas1.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() { 
     public void documentLoadingCompleted(SVGDocumentLoaderEvent e) { 
      SVGDocument doc = jSVGCanvas1.getSVGDocument(); 
      if(doc==null)System.out.println("null"); 
     } 
    }); 
+0

非常感謝! –

+0

嗨,我還有一個問題,如果你會如此友善的回答。如果我想更改「documentLoadingCompleted」方法之外的屬性,該怎麼辦?例如在按下按鈕時更改屬性。然後怎樣呢?只需更改元素的屬性不起作用。 –