JavaFX - 获取节点相对于其父节点的坐标

Iva*_*son 3 javafx parent coordinates

我正在制作一个简单的图形界面来保存以前生成的图像。所有图像对我来说都是方形的,但我想允许一些裁剪功能(更准确地说,从图像的底部和顶部切掉相等的部分)。我想通过允许用户在图像上拖动阴影区域来做到这一点,这将告诉用户该区域将被裁剪掉。详细信息请参见下图。为了启用此拖动功能,我添加了一些小三角形,我希望用户拖动它们,从而移动阴影区域。然而三角形的坐标都很奇怪而且看起来很荒谬。因此,我想知道最好的方法是什么,以 ImageView 边长的形式获取与 ImageView (或其第一个公共父节点)相关的三角形坐标。因此,如果三角形位于中心,则其坐标例如为 [0.5, 0.5]。

在此输入图像描述

图像视图将在场景内移动,并且也会改变大小,因此至关重要的是,我不仅可以获得相对于 ImageView 的坐标,还可以获得相对于 ImageView 大小的坐标。

如果有帮助的话,这里还有节点的周围层次结构。多边形是三角形,区域是矩形。

在此输入图像描述

感谢各种形式的帮助!

fab*_*ian 6

Node.getBoundsInParent返回节点在其父坐标中的边界。例如 polygon.getBoundsInParent()将返回 中的边界VBox

如果您需要“上升”一个额外的步骤,您可以使用parent.localToParent来执行此操作。vBox.localToParent(boundsInVbox)返回 的坐标系中的边界AnchorPane

要获得相对于图像大小的值,您只需除以图像的大小即可。

以下示例仅允许您向一个方向移动覆盖区域,并且不检查区域是否相交,但它应该足以演示该方法。

有趣的部分是按钮的事件处理程序。它将第二个图像的视口限制为第一个图像未被覆盖的部分。

private static void setSideAnchors(Node node) {
    AnchorPane.setLeftAnchor(node, 0d);
    AnchorPane.setRightAnchor(node, 0d);
}

@Override
public void start(Stage primaryStage) {
    // create covering area
    Region topRegion = new Region();
    topRegion.setStyle("-fx-background-color: white;");
    Polygon topArrow = new Polygon(0, 0, 20, 0, 10, 20);
    topArrow.setFill(Color.WHITE);
    VBox top = new VBox(topRegion, topArrow);
    top.setAlignment(Pos.TOP_CENTER);
    topArrow.setOnMouseClicked(evt -> {
        topRegion.setPrefHeight(topRegion.getPrefHeight() + 10);
    });

    // create bottom covering area
    Region bottomRegion = new Region();
    bottomRegion.setStyle("-fx-background-color: white;");
    Polygon bottomArrow = new Polygon(0, 20, 20, 20, 10, 0);
    bottomArrow.setFill(Color.WHITE);
    VBox bottom = new VBox(bottomArrow, bottomRegion);
    bottom.setAlignment(Pos.BOTTOM_CENTER);
    bottomArrow.setOnMouseClicked(evt -> {
        bottomRegion.setPrefHeight(bottomRegion.getPrefHeight() + 10);
    });

    Image image = new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/402px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg");
    ImageView imageView = new ImageView(image);

    setSideAnchors(top);
    setSideAnchors(bottom);
    setSideAnchors(imageView);
    AnchorPane.setTopAnchor(top, 0d);
    AnchorPane.setBottomAnchor(bottom, 0d);
    AnchorPane.setTopAnchor(imageView, 0d);
    AnchorPane.setBottomAnchor(imageView, 0d);

    AnchorPane container = new AnchorPane(imageView, top, bottom);

    ImageView imageViewRestricted = new ImageView(image);

    Button button = new Button("restrict");
    button.setOnAction(evt -> {
        // determine bouns of Regions in AnchorPane
        Bounds topBounds = top.localToParent(topRegion.getBoundsInParent());
        Bounds bottomBounds = bottom.localToParent(bottomRegion.getBoundsInParent());

        // set viewport accordingly
        imageViewRestricted.setViewport(new Rectangle2D(
                0,
                topBounds.getMaxY(),
                image.getWidth(),
                bottomBounds.getMinY() - topBounds.getMaxY()));
    });

    HBox root = new HBox(container, button, imageViewRestricted);
    root.setFillHeight(false);

    Scene scene = new Scene(root);

    primaryStage.setScene(scene);
    primaryStage.show();
}
Run Code Online (Sandbox Code Playgroud)