如何将SVG Path(SVGOMPathElement)转换为数组?

时间:2014-04-02 19:58:50

标签: java svg element shape batik

我尝试做的事情:将SVG路径转换为(x,y)点数组。

我现在有这个:

public Point[] strokeToPoints(Node n){

    SVGOMPathElement path = (SVGOMPathElement) n;

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

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

    return null;

}

节点n始终是从SVG文件中提取的路径元素,如下所示:

<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"/>

该行

System.out.println(path.getAttribute("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

哪个好,但行

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

返回

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)

导致此错误的原因是什么?我需要总长度,所以我可以遍历它将点收集到一个数组。

1 个答案:

答案 0 :(得分:0)

看起来应该是这样的:

PathLength  pathLenObj = new PathLength((Shape) n);
float  length = pathLenObj.pathLength;