JavaFX 获取旋转 imageView 的角坐标?

JavaFX get corner coordinates of rotated imageView?

我正在做一个简单的模拟,在找到一个旋转的、大小奇怪的 imageView 节点的 X 和 Y 坐标时遇到了很多麻烦。 (蓝色位是前面)

目标是找出相对于 imageView 旋转后指向的方向的 XY 坐标。我可以找到 imageView 相对于其起始位置的角度,但我无法弄清楚如何获得 imageView 相对于该角度的 XY 坐标。由于 .setRotate(angle) 方法不会改变 imageView 的 X 和 Y 位置,我应该如何找到 imageView 面对的点?

我正在使用的旋转和 imageView 的最小示例:

Main.java

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();

        primaryStage.setScene(new Scene(root, 500, 500));

        Image robotImage = null;

        try {
            robotImage = new Image(new FileInputStream("res\robot.png"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        ImageView robot = new ImageView(robotImage);
        robot.setLayoutX(125);
        robot.setLayoutY(125);

        System.out.println("PreRotate X: " + robot.getLayoutX() + "PreRotate Y: " + robot.getLayoutY());
        robot.setRotate(45);
        System.out.println("PostRotate X: " + robot.getLayoutX() + "PostRotate Y: " + robot.getLayoutY());
        root.getChildren().add(robot);

        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

我已经尝试使用 imageView 的边界以及位于 imageView 顶部的线条,但这需要我在每次 imageView 更改时找到新的 max/min x/y它的 max/min x/y.

例如:

        if (turnAngle < 35) {
            directionLine.setStartX(robotLeftRightAngle.getBoundsInParent().getMaxX());
            directionLine.setStartY(robotLeftRightAngle.getBoundsInParent().getMinY());
            directionLine.setEndX(robotRightLeftAngle.getBoundsInParent().getMinX() + ((robotLeftRightAngle.getBoundsInParent().getMaxX() - robotRightLeftAngle.getBoundsInParent().getMinX()) / 2));
            directionLine.setEndY(robotRightLeftAngle.getBoundsInParent().getMinY() + ((robotLeftRightAngle.getBoundsInParent().getMinY() - robotRightLeftAngle.getBoundsInParent().getMinY()) / 2));
        }
        else if (turnAngle < 55) {
            directionLine.setStartX(robotLeftRightAngle.getBoundsInParent().getMaxX());
            directionLine.setStartY(robotLeftRightAngle.getBoundsInParent().getMaxY());
            directionLine.setEndX(robotRightLeftAngle.getBoundsInParent().getMinX() + ((robotLeftRightAngle.getBoundsInParent().getMaxX() - robotRightLeftAngle.getBoundsInParent().getMinX()) / 2));
            directionLine.setEndY(robotRightLeftAngle.getBoundsInParent().getMinY() + ((robotLeftRightAngle.getBoundsInParent().getMaxY() - robotRightLeftAngle.getBoundsInParent().getMinY()) / 2));
        }

等等一直到 360。DRY yikes。

我应该如何处理这个问题?我使用了错误的转换吗?我没有看到可以用于此的方法吗?我知道必须有更好的方法。谢谢阅读。

我不确定我是否 100% 理解了这个问题。坐标系之间的转换 但是从你的描述中很难说出你需要转换的坐标系,所以我假设你想在robot坐标系和group坐标系之间转换。

可以使用 localToParent 将节点的坐标系转换为父节点的坐标系,以适应所有变换。 (parentToLocal 可以实现逆变换,但在这种情况下,这似乎不是所需的转换。)

以下示例将线的起点和终点修改为左上角的坐标和 Rectangle 坐标系中 Rectangle 上方 100 像素的点:

@Override
public void start(Stage primaryStage) {
    Group root = new Group();

    primaryStage.setScene(new Scene(root, 500, 500));

    Rectangle robot = new Rectangle(100, 20, Color.RED);
    robot.setLayoutX(125);
    robot.setLayoutY(125);
    Line line = new Line(125, 125, 125, 25);

    robot.rotateProperty().addListener(o -> {
        Point2D start = robot.localToParent(0, 0);
        Point2D end = robot.localToParent(0, -100);
        line.setStartX(start.getX());
        line.setStartY(start.getY());
        line.setEndX(end.getX());
        line.setEndY(end.getY());
    });

    RotateTransition rotateTransition = new RotateTransition(Duration.seconds(5), robot);
    rotateTransition.setCycleCount(Animation.INDEFINITE);
    rotateTransition.setFromAngle(0);
    rotateTransition.setToAngle(360);
    rotateTransition.setInterpolator(Interpolator.LINEAR);
    rotateTransition.play();

    root.getChildren().addAll(robot, line);

    primaryStage.show();
}