Hi,
I am trying to load an SVG document on JSVGCanvas and trying to update the attribute of some element. However, the changes are not shown on the canvas. From what I know, any modifications made to DOM should be in the Swing update thread of the application. Also, perhaps I need register a listener on the JSVGCanvas for the first 'gvtRenderingCompleted' event. I have tried to make all these changes in my application without any success. I don't know what are the correct places to make these changes. I am giving the code here(shamelessly copied from xml-batik page+some modifications done!!) . Please suggest how should I restructure it correctly to achieve what I want.
***SVG file***
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg" width="600" height="600">
<g>
<path id="curve" style="stroke-width:20; stroke:#00ffff; fill:none" d="M496 496 L200 496"/>
</g>
</svg>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg" width="600" height="600">
<g>
<path id="curve" style="stroke-width:20; stroke:#00ffff; fill:none" d="M496 496 L200 496"/>
</g>
</svg>
***SVGApplication***
public class SVGApplication {
public static void main(String[] args) {
new SVGApplication();
}
public static void main(String[] args) {
new SVGApplication();
}
JFrame frame;
JSVGCanvas canvas;
Document document;
Window window;
Element curve;
JSVGCanvas canvas;
Document document;
Window window;
Element curve;
public SVGApplication() {
frame = new JFrame();
canvas = new JSVGCanvas();
// Forces the canvas to always be dynamic even if the current
// document does not contain scripting or animation.
canvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
canvas.addSVGLoadEventDispatcherListener
(new SVGLoadEventDispatcherAdapter() {
public void svgLoadEventDispatchStarted
(SVGLoadEventDispatcherEvent e) {
// At this time the document is available...
document = canvas.getSVGDocument();
// ...and the window object too.
window = canvas.getUpdateManager().
getScriptingEnvironment().createWindow();
// Registers the listeners on the document
// just before the SVGLoad event is
// dispatched.
registerListeners();
// It is time to pack the frame.
frame.pack();
}
});
frame = new JFrame();
canvas = new JSVGCanvas();
// Forces the canvas to always be dynamic even if the current
// document does not contain scripting or animation.
canvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
canvas.addSVGLoadEventDispatcherListener
(new SVGLoadEventDispatcherAdapter() {
public void svgLoadEventDispatchStarted
(SVGLoadEventDispatcherEvent e) {
// At this time the document is available...
document = canvas.getSVGDocument();
// ...and the window object too.
window = canvas.getUpdateManager().
getScriptingEnvironment().createWindow();
// Registers the listeners on the document
// just before the SVGLoad event is
// dispatched.
registerListeners();
// It is time to pack the frame.
frame.pack();
}
});
frame.addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent e) {
// The canvas is ready to load the base document
// now, from the AWT thread.
canvas.setURI("doc.svg");
}
});
public void windowOpened(WindowEvent e) {
// The canvas is ready to load the base document
// now, from the AWT thread.
canvas.setURI("doc.svg");
}
});
//IS IT RIGHT??
canvas.getUpdateManager().getUpdateRunnableQueue().invokeLater(new
Runnable() { public void run() {
curve.setAttributeNS(null,"d","M10 296 L236 296");//as a sample change in attribute
}
});
Runnable() { public void run() {
curve.setAttributeNS(null,"d","M10 296 L236 296");//as a sample change in attribute
}
});
frame.getContentPane().add(canvas);
frame.setSize(800, 600);
frame.show();
}
public void registerListeners() {
// Gets an element from the loaded document.
curve = document.getElementById("curve");
EventTarget t = (EventTarget)curve;
// Gets an element from the loaded document.
curve = document.getElementById("curve");
EventTarget t = (EventTarget)curve;
// Adds a 'onload' listener
t.addEventListener("SVGLoad", new OnLoadAction(), false);
t.addEventListener("SVGLoad", new OnLoadAction(), false);
// Adds a 'onclick' listener
t.addEventListener("click", new OnClickAction(), false);
t.addEventListener("click", new OnClickAction(), false);
//IS IT RIGHT??
//register a listener on the JSVGCanvas for the first 'gvtRenderingCompleted' event.
canvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
}
public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
canvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
}
public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
}
});
});
}
public class OnLoadAction implements EventListener {
public void handleEvent(Event evt) {
// Make some actions here...
public void handleEvent(Event evt) {
// Make some actions here...
// ...for example start an animation loop:
window.setInterval(new Animation(), 50);
}
}
window.setInterval(new Animation(), 50);
}
}
public class OnClickAction implements EventListener {
public void handleEvent(Event evt) {
// Make some actions here...
public void handleEvent(Event evt) {
// Make some actions here...
// ...for example schedule an action for later:
window.setTimeout(new DelayedTask(), 500);
}
}
window.setTimeout(new DelayedTask(), 500);
}
}
public class Animation implements Runnable {
public void run() {
// Insert animation code here...
public void run() {
// Insert animation code here...
}
}
}
public class DelayedTask implements Runnable {
public void run() {
// Make some actions here...
public void run() {
// Make some actions here...
// ...for example displays an alert dialog:
window.alert("Delayed Action invoked!");
}
}
}
window.alert("Delayed Action invoked!");
}
}
}
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!