如何在转动javafx形状后找到矩形的坐标

如何在转动javafx形状后找到矩形的坐标,java,javafx,Java,Javafx,我用坐标(x=100,y=100,宽度=200,高度=100)画了一个矩形。然后我围绕中心旋转这个矩形的形状 this.rotation.addListener((obs, old, fresh) -> { Rotate rotate = new Rotate(); rotate.setAngle((double) fresh - (double) old); rotate.setPivotX(x.getValue().doubleValue() + (width.

我用坐标(x=100,y=100,宽度=200,高度=100)画了一个矩形。然后我围绕中心旋转这个矩形的形状

this.rotation.addListener((obs, old, fresh) -> {
    Rotate rotate = new Rotate();
    rotate.setAngle((double) fresh - (double) old);
    rotate.setPivotX(x.getValue().doubleValue() + (width.getValue().doubleValue() / 2));
    rotate.setPivotY(y.getValue().doubleValue() + (height.getValue().doubleValue() / 2));
    shape.getTransforms().addAll(rotate);
});

现在如何找到形状的坐标?

您可以使用
localToParent
方法将点从
矩形的坐标系转换为父坐标系

Point2D topLeftInParent = shape.localToParent(shape.getX(), shape.getY());
如果您只需要显示
矩形的父级中的x/y范围,也可以使用
getBoundsInParent

Bounds parentBounds = shape.getBoundsInParent();
顺便说一句:我不建议每次更改都添加一个新的转换。相反,我建议调整现有的旋转:

Rotate rotate = new Rotate(0, shape.getX() + shape.getWidth()/2, shape.getX() + shape.getHeight()/2);
shape.getTransforms().add(rotate);
rotate.angleProperty().bind(this.rotation);