Hi,

I searched through the mail archieves and I found several mails covering this topic but I wasn't able complete my task. The thing I want to do is getting a BoundingBox for elments such as g, rect, use and so on. I want to "zoom" to a specific element... but I do get a NullpointerException. I'm not sure what is wrong. I read about the updateMangaer issue and the manager seems not to be null, but the svgcontext is still null. I thought when there is an updatemanager there is a graphical version of the svg-Tree and I can get a bounding box. But it seems these things have nothing to do with each other. Now I'm really confused ;)

I made an example to show my approach to handle this. The code beneath loads a dom of the batik yinyang example, it assumes the svg file is in the curent directory. It creates a frame and JSVGCanvas, sets the canvas to dynamic und then sets the dom. After this, the code waits for the rendering to be completed and then gets the yinShape group by id. Then I use getBBox to get the bounding box with no error but the following float x = rect.getX(); causes a null pointer because svgcontext is null.

I hope you can help me with that.

public class BatikTest {

    static boolean documentLoaded = false;

public static void main(String[] args) throws Exception { String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser);
String uri = (new File("BatikYin.svg")).toURI().toString();
Document doc = factory.createDocument(uri);
JSVGCanvas svgCanvas = new JSVGCanvas(); svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);


        JFrame frame = new JFrame("batik document view");
        frame.getContentPane().add(svgCanvas);
        frame.setSize(200,200);
        frame.show();

svgCanvas.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {
public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
System.out.println("Document Loading...");
}
public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
System.out.println("Document Loaded.");
}
});
svgCanvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
public void gvtBuildStarted(GVTTreeBuilderEvent e) {
System.out.println("Build Started...");
}
public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
System.out.println("Build Done.");
}
});
svgCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
System.out.println("Rendering Started...");
}
public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
System.out.println("Redering completed.");
documentLoaded = true;
}
});
svgCanvas.setDocument(doc); while (!documentLoaded) {
Thread.sleep(100);
}
Node yinShapeNode = doc.getElementById("yinShape"); SVGGraphicsElement graphics = (SVGGraphicsElement) yinShapeNode;
SVGRect rect = graphics.getBBox();
float x = rect.getX();
float y = rect.getY();
float height = rect.getHeight();
float width = rect.getWidth();
System.out.println("x="+x+" y="+y+" width="+width+" height="+height);
}


}

bye blackdrag


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to