JavaFX 获取旋转 imageView 的角坐标?

标签 java javafx rotation line

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

robot.png

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

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

主.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 更改其最大/最小 x/时找到新的最大/最小 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));
        }

Yikes

等等一直到 360。DRY yikes。

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

最佳答案

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

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

以下示例将一条线的起点和终点修改为 RectangleRectangle 左上角和上方 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();
}

关于JavaFX 获取旋转 imageView 的角坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51939903/

相关文章:

java - 普通 Spring Batch 示例中非作业方法的 Bean 注释的目的

java - 限制用户删除Android应用程序

java - 我如何在同一窗口/阶段中的前一个 Pane 的右侧立即在 java FX 中启动一个新 Pane ?

java - JavaFX 是否有办法创建分组堆积条形图?

javafx - 如何调整 Javafx 未装饰阶段的大小?

html - 使用 -webkit-transform 时固定位置不起作用

java - 使网站适应iphone

java - 强制将对象放入 Hazelcast 本地 map

math - 四元数和数值稳定性

java - 如何在不更改其位置的情况下旋转图形对象?