我按照這裏的指導:http://xmlgraphics.apache.org/batik/faq.html並閱讀其郵件列表頁面。我試過了我能想到的一切。這是我想要完成的一個非常簡單的例子。佈局管理器是:http://java.sun.com/products/jfc/tsc/articles/tablelayout/或者你可以使用gridbag或任何你喜歡的。 XmlHelper是內部庫,你可以用你自己的替換。不包括在內,以減少代碼的大小。如何在DOM更改後通過Batik刷新/顯示SVG?
基本上,當你點擊「向上」按鈕(更新的簡稱)後,它會修改DOM並添加一個新元素,但是我沒有看到SVG更新。
..
public class SvgRefresh extends JFrame
{
private static final NAMESPACE = "http://www.w3.org/2000/svg";
private JSVGCanvas canvas;
private Document document;
private JButton button;
public static void main(String[] args)
{
SvgRefresh svgRefresh = new SvgRefresh();
svgRefresh.pack();
svgRefresh.setVisible(true);
}
public SvgRefresh()
{
canvas = new JSVGCanvas();
initialize();
}
private void initialize()
{
double[][] size = {{ TableLayout.FILL, 0.1, TableLayout.FILL}, { TableLayout.FILL, 0.1 }};
getContentPane().setLayout(new TableLayout(size));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
document = XmlHelper.readDocumentFromFile("F:/SVG/svg01.svg");
canvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
canvas.setDocument(document);
document = canvas.getSVGDocument();
getContentPane().add(canvas, "0, 0, 2, 0");
getContentPane().add(getButton(), "1, 1");
}
private JButton getButton()
{
if(button == null)
{
button = new JButton("Up");
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
UpdateManager updateManager = canvas.getUpdateManager();
updateManager.getUpdateRunnableQueue().invokeLater(new Runnable()
{
@Override
public void run()
{
Element root = document.getDocumentElement();
Element text = document.createElementNS(NAMESPACE, "text");
text.setAttributeNS(NAMESPACE, "x", "100");
text.setAttributeNS(NAMESPACE, "y", "100");
text.setTextContent("It worked!!!");
root.appendChild(text);
System.out.println("ran");
}
});
}
});
}
return button;
}
}
原始SVG(人工智能做出的,我編輯的所有內容了,除了文本元素):
<?xml version="1.0" encoding="UTF-8"?><svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" contentScriptType="application/ecmascript" zoomAndPan="magnify" contentStyleType="text/css" enable-background="new 0 0 400 200" version="1.1" xml:space="preserve" width="400px" preserveAspectRatio="xMidYMid meet" viewBox="0 0 400 200" height="200px" x="0px" y="0px">
<text x="40" y="40">Default text</text>
</svg>
以上代碼不包括寫入文件 - 我排除了空間。按下UP按鈕並寫入文件(檢查命名空間)後的SVG。當按下「UP」按鈕後寫入文件時,這會生成 - 這對我來說看起來很完美。
<?xml version="1.0" encoding="UTF-8"?><svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" contentScriptType="application/ecmascript" zoomAndPan="magnify" contentStyleType="text/css" enable-background="new 0 0 400 200" version="1.1" xml:space="preserve" width="400px" preserveAspectRatio="xMidYMid meet" viewBox="0 0 400 200" height="200px" x="0px" y="0px">
<text x="40" y="40">Default text</text>
<text x="100" y="100">It worked!!!</text>
</svg>
感謝您的幫助。哦,爲了鼓勵研究,並且人們花時間回答他們的問題,我可能會等待24小時左右,然後再給出答案。所以,如果你願意的話,花點時間並運行代碼。
當你按上,然後調整窗口,你會看到在左上角一些噪音。這裏是你的新文本。 我相信這個問題在變換或渲染 – yggdraa 2012-03-13 11:01:17