3

是否可以使用 Timeline 来摇动初级阶段,因此使用 XTimeline 和 YTimeLine?

    final Timeline Xtimeline = new Timeline();//for Animate On X'-Stage
    Xtimeline.setCycleCount(Timeline.INDEFINITE);
    Xtimeline.setAutoReverse(true);
    final Timeline Ytimeline = new Timeline();//for Animate On Y'-Stage
    Ytimeline.setCycleCount(Timeline.INDEFINITE);
    Ytimeline.setAutoReverse(true);
4

2 回答 2

3

尝试这个..

int x = 0;
int y = 0;

public void shakeStage() {
        Timeline timelineX = new Timeline(new KeyFrame(Duration.seconds(0.1), new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                if (x == 0) {
                    primaryStage.setX(primaryStage.getX() + 10);
                    x = 1;
                } else {
                    primaryStage.setX(primaryStage.getX() - 10);
                    x = 0;
                }
            }
        }));

        timelineX.setCycleCount(Timeline.INDEFINITE);
        timelineX.setAutoReverse(false);
        timelineX.play();


        Timeline timelineY = new Timeline(new KeyFrame(Duration.seconds(0.1), new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                if (y == 0) {
                    primaryStage.setY(primaryStage.getY() + 10);
                    y = 1;
                } else {
                    primaryStage.setY(primaryStage.getY() - 10);
                    y = 0;
                }
            }
        }));

        timelineY.setCycleCount(Timeline.INDEFINITE);
        timelineY.setAutoReverse(false);
        timelineY.play();
    }
于 2013-05-20T07:08:21.890 回答
3

此演示的完整代码(也可作为gist提供):

在此处输入图像描述

public class ShakingStage extends Application {

    public Stage primaryStage;
    boolean xyState= true;

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

    @Override
    public void start(Stage stage) throws Exception {
        primaryStage= stage;
        Button shakeButton= ButtonBuilder.create()
                .text("shake it")
                .onAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent actionEvent) {
                        TimelineBuilder.create()
                                .keyFrames(new KeyFrame(Duration.seconds(0.2), new EventHandler<ActionEvent>() {
                                    @Override
                                    public void handle(ActionEvent actionEvent) {
                                        if (xyState) {
                                            primaryStage.setX(primaryStage.getX() + 10);
                                            primaryStage.setY(primaryStage.getY() + 10);
                                        } else {
                                            primaryStage.setX(primaryStage.getX() - 10);
                                            primaryStage.setY(primaryStage.getY() - 10);
                                        }
                                        xyState= !xyState;
                                    }
                                }))
                                .autoReverse(true)
                                .cycleCount(Timeline.INDEFINITE)
                                .build().play();
                   }
                })
                .build();

        Scene scene= SceneBuilder.create()
                .root(StackPaneBuilder.create()
                        .children(shakeButton)
                        .build()
                )
                .width(300)
                .height(400)
                .build();

        stage.setScene(scene);
        stage.show();
    }
}
于 2013-05-20T19:06:30.177 回答