First of all, thanks Thomas for your reply.
I am using SVGLocatable to get the bounding box of the svg elements as follows:
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
Document doc = f.createDocument(uri); //******** create document from svg file
Element mainmap = doc.getElementById("mainmap");
NodeList svgNodes = mainmap.getElementsByTagName("path"); //******** retrieve needed svg elements from the document
for (int i=0; i<svgNodes.getLength(); i++)
{
Element subElemNode = (Element)svgNodes.item(i);// Get element
if (subElemNode instanceof SVGLocatable)
{
SVGRect bb = ((SVGLocatable)subElemNode).getBBox(); //********** get the bounding box of the elements
System.out.println(bb.getX()); //*********** ---> Line 45
}
}
When I print the Id of the elements it shows the correct output but when I get the BBox of that element and try to print it out it gives the following run-time error(line45):
exception: null
java.lang.NullPointerException
at org.apache.batik.dom.svg.SVGLocatableSupport$1.getX(Unknown Source)
at CreateSpatial.main(CreateSpatial.java:45)
Any tips on this??
Thanks,
Riyaz.
*********************
Hi Jan, Riyaz,
So the first question to ask when you say 'bounding box' is the
bounding box in what coordinate system? And how important is it that
the bounding box be 'tight'?
So Jan gives one way of doing it which seems to try and get the
bounds in screen coords, (but the code assumes there are no transforms
between the element and the root of the SVG tree - this can be fixed
by including the results of node.getGlobalTransform()).
You could also just use the SVGLocatable interface on the DOM
Element (see the SVG specification). This would generally be
preferable but if you need tight bounds in any coordinate system other
than the user coordinate system you will have to use the various
GraphicsNode methods (like getTransformedBounds).
- RE: AW: svg element's bounding box error Riyaz
- RE: AW: svg element's bounding box error Thomas E Deweese