2017-05-25 34 views

回答

1

我发现用户templth开发batik users forum的答案,我不记得我只是在这里重新发布解决方案,所以它可以有更多的曝光。

public Element elementAtPosition(Document doc, Point2D point) { 
    UserAgent userAgent = new UserAgentAdapter(); 
    DocumentLoader loader = new DocumentLoader(userAgent); 
    BridgeContext context = new BridgeContext(userAgent, loader); 
    context.setDynamicState(BridgeContext.DYNAMIC); 
    GVTBuilder builder = new GVTBuilder(); 
    GraphicsNode rootGraphicsNode = builder.build(context, doc); 

    // rootGraphicsNode can be offseted relative to coordinate system 
    // that means calling this method on coordinates taken directly from the svg file might not work 
    // check the bounds of rootGraphicsNode to determine where the elements actually are 
    // System.out.println(rootGraphicsNode.getBounds()); 

    GraphicsNode graphicsNode = rootGraphicsNode.nodeHitAt(point); 
    if (graphicsNode != null) { 
     return context.getElement(graphicsNode); 
    } else { 
     // if graphicsNode is null there is no element at this position 
     return null; 
    } 
} 

在蜡染1.9测试。此方法仅返回指定位置上的最顶层元素。作为解决方法,您可以删除该元素并再次调用nodeHitAt。