แปลการเปลี่ยนแปลง JavaFX

ฉันกำลังพยายามใช้ TranslateTransition กับสี่เหลี่ยมผืนผ้าของฉัน แต่ไม่ได้ทำอะไรเลย นี่คือรหัสของฉัน:

primaryStage.setTitle("AnimationTest");
    Group group = new Group();

    Rectangle rect = new Rectangle(0,0,100,100);
    group.getChildren().add(rect);
    TranslateTransition transition =new TranslateTransition(Duration.millis(1000),rect);
        transition.setByX(100);

    Button button = new Button("StartAnimation");
        button.setOnAction((e)->{
            transition.play();
        });
    VBox layout = new VBox();
    layout.getChildren().addAll(group, button);
    Scene scene = new Scene(layout, 600, 600);
    primaryStage.setScene(scene);
    primaryStage.show();

person XPenguen    schedule 18.10.2015    source แหล่งที่มา


คำตอบ (1)


ตามเอกสารประกอบสำหรับ Group:

กลุ่มจะยึดขอบเขตร่วมกันของลูกหลานของตน

กล่าวอีกนัยหนึ่ง เมื่อสี่เหลี่ยมเคลื่อนที่ ระบบพิกัดของกลุ่มจะปรับให้พอดีกับทุกสิ่งที่มีอยู่ เนื่องจากสิ่งเดียวที่มีอยู่คือสี่เหลี่ยมผืนผ้า สี่เหลี่ยมผืนผ้าจึงยังคงอยู่คงที่ตามคอนเทนเนอร์ของกลุ่ม

ใช้ Pane แทน Group:

primaryStage.setTitle("AnimationTest");
Pane pane = new Pane();

Rectangle rect = new Rectangle(0,0,100,100);
pane.getChildren().add(rect);
TranslateTransition transition =new TranslateTransition(Duration.millis(1000),rect);
    transition.setByX(100);

Button button = new Button("StartAnimation");
    button.setOnAction((e)->{
        transition.play();
    });
VBox layout = new VBox();
layout.getChildren().addAll(pane, button);
Scene scene = new Scene(layout, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
person James_D    schedule 18.10.2015