代码之家  ›  专栏  ›  技术社区  ›  NotZack

javafx获取旋转图像视图的角坐标?

  •  1
  • NotZack  · 技术社区  · 6 年前

    我正在做一个简单的模拟,并且很难找到一个旋转的、大小怪异的ImageView节点的X和Y坐标。(蓝色的钻头在前面)

    目标是在图像视图旋转后找出相对于其指向的方向的xy坐标。我可以找到图像视图相对于它的起始位置的角度,但我不知道如何获得图像视图相对于这个角度的xy坐标。由于.setrotate(angle)方法不会更改ImageView的X和Y位置,我应该如何查找ImageView所面对的点?

    我使用的旋转和图像视图的最小示例:

    <强>主。Java < /强>

    公共类主扩展应用程序{ @重写 公共空间开始(阶段一级){ group root=new group(); 第一阶段。第二阶段(新场景(根,500,500)); image robotimage=空; 尝试{ robotImage=新图像(new fileinputstream(“res\\robot.png”)); }catch(fileNotFoundException E){ } ImageView Robot=新的ImageView(RobotImage); 机器人设置布局(125); 机器人设置(125); system.out.println(“prerotate x:”+robot.getlayoutx()+“prerotate y:”+robot.getlayouty()); 机器人。设置旋转(45); system.out.println(“postrotate x:”+robot.getlayoutx()+“postrotate y:”+robot.getlayouty()); root.getchildren().add(robot); primarystage.show(); } 公共静态void main(string[]args){ 发射(ARGS); } } < /代码>

    我已经尝试使用ImageView的边界以及ImageView顶部的线条,但这要求每次ImageView更改其最大/最小X/Y时,我都要查找新的最大/最小X/Y。

    例如:

    if(转角<35){
    directionline.setStartX(robotLeftRightAngle.getBoundsInParent().getMaxX());
    directionline.setStarty(robotLeftRightAngle.getBoundsInParent().getMiny());
    directionline.setendx(robotrightleftangle.getboundsinparent().getminx()+((robotleftrightangle.getboundsinparent().getmaxx()-robotrightleftangle.getboundsinparent().getminx())/2));
    }
    否则,如果(转角<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度。干YKES。

    我该如何处理?我是否使用了错误的转换?我有没有看到一个方法可以用于这个?我知道一定有更好的方法。谢谢你的阅读。

    robot.png

    目标是在图像视图旋转后找出相对于其指向的方向的xy坐标。我可以找到图像视图相对于它的起始位置的角度,但我不知道如何获得图像视图相对于这个角度的xy坐标。由于.setrotate(angle)方法不会更改ImageView的X和Y位置,我应该如何查找ImageView所面对的点?

    我使用的旋转和图像视图的最小示例:

    主类

    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/Y时,我都要查找新的最大/最小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度。干燥干燥。

    我该如何处理?我是否使用了错误的转换?我有没有看到一个方法可以用于这个?我知道一定有更好的方法。谢谢你的阅读。

    1 回复  |  直到 6 年前
        1
  •  3
  •   fabian    6 年前

    我不确定我完全理解这个问题。坐标系之间的转换,但是很难从描述中区分需要在其中转换的坐标系,因此我假设您希望在 robot 到的坐标系 group .

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

    下面的示例将一条线的起点和终点修改为左上角的坐标以及 Rectangle 矩形 的坐标系:

    @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();
    }