1

What I'm trying to do: convert an SVG path to an array of (x,y) points.

I have this at the moment:

public Point[] strokeToPoints(Node n){

    SVGOMPathElement path = (SVGOMPathElement) n;

    System.out.println(path.getAttribute("d")); 

    System.out.println(path.getTotalLength());

    return null;

}

Node n is always a path element extracted from an SVG file which looks something like this:

<path id="kvg:098df-s1" kvg:type="㇒" d="M52.75,10.5c0.11,0.98-0.19,2.67-0.97,3.93C45,25.34,31.75,41.19,14,51.5"/>

The line

System.out.println(path.getAttribute("d"));

returns

M52.75,10.5c0.11,0.98-0.19,2.67-0.97,3.93C45,25.34,31.75,41.19,14,51.5

which is fine, but the line

System.out.println(path.getTotalLength());

returns

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at org.apache.batik.dom.svg.SVGPathSupport.getTotalLength(SVGPathSupport.java:41)
    at org.apache.batik.dom.svg.SVGOMPathElement.getTotalLength(SVGOMPathElement.java:131)

What's causing this error? I need the total length so I can traverse it collecting the points to an array.

epeen
  • 13
  • 1
  • 7

1 Answers1

0

It looks like it should be something like:

PathLength  pathLenObj = new PathLength((Shape) n);
float  length = pathLenObj.pathLength;
Paul LeBeau
  • 97,474
  • 9
  • 154
  • 181
  • Thank you for your response. I followed your suggestion but unfortunately received the error: org.apache.batik.dom.svg.SVGOMPathElement cannot be cast to java.awt.Shape Could you possibly cast any further light on the situation? – epeen Apr 03 '14 at 02:34
  • Sorry. My bad. I didn't check the definition of Shape. It looks like it may be an internal class that is not intended for users to use. Maybe you'll have better luck getting help on the Batik mailing list. http://xmlgraphics.apache.org/batik/mailing-lists.html – Paul LeBeau Apr 03 '14 at 16:08
  • I'll do that. I made what I thought was some progress to my problem but have hit another road block. I've revised my question. I'd be grateful if you would be so kind as to take a look. – epeen Apr 03 '14 at 19:47