我正在尝试画一棵家谱树。在我的树中,我存储了有关前伴侣的信息。所以人的面板(区域)应该看起来像这样
Z * * * Z * * * Z * * * X --- Y
其中Z代表前伴侣,X代表波斯人,Y代表现任妻子/丈夫
现在我想画一条线来连接当前与孩子的关系。以及与孩子的关系。 (图形上 Z 和 * 之间会有一条线)
但是当人位于其他区域时,LayoutX 和 LayoutX 属性返回相对值。
我该如何做到这一点,以及如何动态调整该面板的大小?我希望 Z 应该始终位于连接孩子的水平线的中间。
给定任何节点,您可以使用以下命令获取其在同一场景图中任何其他节点的坐标系中的边界:
Node nodeOfInterest = ... ;
Node anotherNode = ... ;
// ...
Bounds boundsInScene = nodeOfInterest.localToScene(nodeOfInterest.getBoundsInLocal());
Bounds boundRelativeToAnotherNode = anotherNode.sceneToLocal(boundsInScene);
因此,假设您有某种窗格,它是您想要连接的两个节点的共同祖先,您可以这样做:
Pane commonAncestor = ... ;
Node n1 = ... ;
Node n2 = ... ;
Bounds n1InCommonAncestor = getRelativeBounds(n1, commonAncestor);
Bounds n2InCommonAncestor = getRelativeBounds(n2, commonAncestor);
Point2D n1Center = getCenter(n1InCommonAncestor);
Point2D n2Center = getCenter(n2InCommonAncestor);
Line connector = new Line(n1Center.getX(), n1Center.getY(), n2Center.getX(), n2Center.getY());
commonAncestor.getChildren().add(connector);
// ...
private Bounds getRelativeBounds(Node node, Node relativeTo) {
Bounds nodeBoundsInScene = node.localToScene(node.getBoundsInLocal());
return relativeTo.sceneToLocal(nodeBoundsInScene);
}
private Point2D getCenter(Bounds b) {
return new Point2D(b.getMinX() + b.getWidth() / 2, b.getMinY() + b.getHeight() / 2);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)