如何在放置在不同窗格/区域中的两个节点之间画线
How to draw line between two nodes placed in different Panes / Regions
我正在尝试绘制家谱树。在我的树中,我存储了有关前合作伙伴的信息。所以人的面板(区域)应该看起来像这样
Z * * * Z * * * Z * * * X --- Y
其中Z代表前伴侣,X代表Persion,Y代表现任妻子/丈夫
现在我想画一条线将当前关系与 children 连接起来。与ec关系children。 (图形上 Z 和 * 之间会有一条线)
但是当人在其他区域时LayoutX和LayoutX属性return相对值。
我该怎么做,以及如何动态调整此面板的大小?我希望 Z 应该始终位于连接 children.
的水平线的中间
给定任何节点,您可以通过以下方式在同一场景图中的任何其他节点的坐标系中获取其边界:
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);
}
我正在尝试绘制家谱树。在我的树中,我存储了有关前合作伙伴的信息。所以人的面板(区域)应该看起来像这样
Z * * * Z * * * Z * * * X --- Y
其中Z代表前伴侣,X代表Persion,Y代表现任妻子/丈夫
现在我想画一条线将当前关系与 children 连接起来。与ec关系children。 (图形上 Z 和 * 之间会有一条线)
但是当人在其他区域时LayoutX和LayoutX属性return相对值。 我该怎么做,以及如何动态调整此面板的大小?我希望 Z 应该始终位于连接 children.
的水平线的中间给定任何节点,您可以通过以下方式在同一场景图中的任何其他节点的坐标系中获取其边界:
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);
}