I'm having trouble getting the JSVGCanvas to update after I change the properties of an element. Answers to similar questions in the archives seem to involve subclasses of JSVGCanvas, overlays, or some other situations that don't seem to apply. Here's the relevant code:
DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
doc = (SVGDocument)impl.createDocument(svgNS, "svg", null);
svgCanvas.setSVGDocument(doc);
Once you give the document the the Canvas you may only safely modify it
in the Update thread. I suspect that if you move all your DOM manipulations
before setting the document or move them after the UpdateManager has started (gvtRenderCompleted event) and put then in an UpdateManager Runnable things
will start working for you. As it is you are mucking with the DOM at the same
time as Batik is trying to build the rendering tree. I'm not surprised you have
problems. BTW the following does work (equivilent code in ECMA script where
Batik can make sure things are always done in the proper thread :)
<?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="450" height="500" viewBox="0 0 450 500">
<script type="text/ecmascript">
function click(evt) {
var tgt = evt.target;
var style = tgt.getAttributeNS(null, "style");
if (style == "fill:red" ) {
tgt.setAttributeNS(null, "style", "fill:blue");
} else {
tgt.setAttributeNS(null, "style", "fill:red");
}
}
</script><rect x="10" y="20" width="100" height="50" style="fill:red"
onclick="click(evt)"/>
</svg>
svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
Element svgRoot = doc.getDocumentElement();
// set the width and height attribute on the root svg element svgRoot.setAttributeNS(null, "width", "400"); svgRoot.setAttributeNS(null, "height", "450"); Element rectangle = doc.createElementNS(svgNS, "rect"); rectangle.setAttributeNS(null, "x", "10"); rectangle.setAttributeNS(null, "y", "20"); rectangle.setAttributeNS(null, "width", "100"); rectangle.setAttributeNS(null, "height", "50"); rectangle.setAttributeNS(null, "style", "fill:red"); svgRoot.appendChild(rectangle);
((EventTarget)rectangle).addEventListener("click", new EventListener() {
public void handleEvent(org.w3c.dom.events.Event evt) {
Element target = (Element)evt.getTarget();
String style = target.getAttributeNS(null, "style");
if ("fill:red".equals(style)) {
target.setAttributeNS(null, "style", "fill:blue");
} else {
target.setAttributeNS(null, "style", "fill:red");
}
}}, false);
This starts out as a copy of one of the code samples. I inserted the "addEventListener" part.
handleEvent does get called each time I click the rectangle, and the "style" attribute does change, but the displayed color of the rectangle stays red. I've tried calling svgCanvas.paintComponent(svgCanvas.getGraphics( )) after resetting the style, but no effect.
Obviously I'm missing something basic here, or I'm on the wrong track altogether. Any help would be appreciated.
Thanks,
Steven Gollery [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
