2017-05-25 86 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。