javaFX:使用translatetransition移动具有绝对坐标的形状 [英] javaFX:move shapes with absolute coordinates using translatetransition

查看:924
本文介绍了javaFX:使用translatetransition移动具有绝对坐标的形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用javaFX实现合并排序动画。我想使用绝对坐标来移动形状。我使用 setToX() setToY() Translatetransition()。但是,这些形状位于具有固定间距的HBox中。我尝试将它们移动到不同的地方,但它不起作用。 setToX() setToY()使用HBox作为参考。形状始终具有固定的间距,并且形状的顺序不会改变。我使用 getLayoutX() getLayoutY()来获取绝对坐标。谁可以帮助我?

I am implementing a merge sort animation using javaFX. I want to use absolute coordinates to move shapes. I use setToX() and setToY() of Translatetransition(). However, these shapes are in a HBox with fixed spacing. I try to move them into different place but it does not work. setToX() and setToY() use HBox as a reference. Shapes are always with a fixed spacing and the sequence of shapes do not change. I use getLayoutX() and getLayoutY() to get absolute coordinates. Who can help me??

这些是相关代码:

HBox hBox = new HBox(20);

    hBox.setAlignment(Pos.CENTER);
    hBox.setPadding(new Insets(0,0,0,0));
    ArrayList<StackPane> list = new ArrayList<>();
    Random random = new Random(5);
    for (int i = 0; i < 12; i++) {
        int num = random.nextInt(10);
        Rectangle rectangle = new Rectangle(40, (num * 10) + 50);
        rectangle.setFill(Color.valueOf("#0000FF"));
        Text text = new Text(String.valueOf(num));
        StackPane stackPane = new StackPane();
        stackPane.setPrefSize(rectangle.getWidth(), rectangle.getHeight());
        stackPane.setId(String.valueOf(num));
        stackPane.getChildren().addAll(rectangle, text);
        StackPane.setAlignment(text,Pos.TOP_CENTER);
        stackPane.setAlignment(Pos.TOP_CENTER);
        list.add(stackPane);
    }


    hBox.getChildren().addAll(list);

    BorderPane borderPane = new BorderPane();
    borderPane.setCenter(hBox);

这是转换函数

 private TranslateTransition AddtoOriginal(StackPane sp, double speed,int X,int Y){

    TranslateTransition t = new TranslateTransition();
    t.setNode(sp);
    t.setDuration(Duration.millis(speed));
    t.setToX(X);
    t.setToY(Y);
    return t;

}

这是为了保存绝对坐标

 Scene scene = new Scene(borderPane, 1000, 800);
    primaryStage.setTitle("Sorting");
    primaryStage.setResizable(false);
    primaryStage.setScene(scene);
    primaryStage.show();

    for (int i = 0; i < 12; i++) {
        int centerx = (int) list.get(i).getLayoutX();
        int centery = (int) list.get(i).getLayoutY();
        CenterX.add(i,centerx);
        CenterY.add(i,centery);
        System.out.println(centerx);
        System.out.println(centery);
    }


推荐答案

请使用组或窗格作为容器而不是Hbox。 HBox有布局输出策略,它不支持绝对坐标。

Pls Using the Group or Pane as the container instead of Hbox. HBox has its layout out policy and it doesn't support the absolute coordinate.

这篇关于javaFX:使用translatetransition移动具有绝对坐标的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆