You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Jochen Theodorou <th...@blackdrag.homedns.org> on 2004/06/26 00:16:03 UTC
Bounding boxes again....
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: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org
Re: Bounding boxes again....
Posted by Jochen Theodorou <th...@blackdrag.homedns.org>.
Thomas DeWeese schrieb:
> Hi Jochen,
>
> The code all looks fine, but the element you looked up
> 'yinShape' is in the 'defs' section of the document. Elements
> in the defs section (or with display:none) have no geometry
> associated with them and hence have no bounding box.
how stupid I am ;) I thought I had taken care of that... Ok thx. Now it
works fine.
Bye blackdrag
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org
Re: Bounding boxes again....
Posted by Thomas DeWeese <Th...@Kodak.com>.
Jochen Theodorou wrote:
> one additional comment...
>
> why do I get an invalid boundig box instead of null? If I get a null
> BoundingBox or a Box of size 0 may be I perceive the problem.
This is tricky, on the one hand it might be nice to get a 'null'
BBox, however in the case of display:none the BBox could go from
valid to invalid (setting display to none) then back to valid (setting
display to inline). The return of '0' could be misleading since zero
is a valid width/height. I suppose we could return -1 for w/h.
> At last an additional comment in the getBBox-Method would be nice. Or
> is there such a comment and I was not able to see it?
Well the documentation for these methods comes from the W3C. They
are part of the standard - so the Standard should really cover these
cases.
> In feature code I will check the context first
You might be best wrapping the code in a try/catch block.
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org
Re: Bounding boxes again....
Posted by Jochen Theodorou <th...@blackdrag.homedns.org>.
one additional comment...
why do I get an invalid boundig box instead of null? If I get a null
BoundingBox or a Box of size 0 may be I perceive the problem. At last an
additional comment in the getBBox-Method would be nice. Or is there such
a comment and I was not able to see it? In feature code I will check the
context first
bye
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org
Re: Bounding boxes again....
Posted by Thomas DeWeese <Th...@Kodak.com>.
Hi Jochen,
The code all looks fine, but the element you looked up
'yinShape' is in the 'defs' section of the document. Elements
in the defs section (or with display:none) have no geometry
associated with them and hence have no bounding box.
Jochen Theodorou wrote:
> 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: batik-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: batik-users-help@xml.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org