shaking Stage in javaFX

2019-07-07 02:00发布

is it possible to shake primary stage with Timeline and so Use XTimeline and 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);

2条回答
够拽才男人
2楼-- · 2019-07-07 02:26

The complete code of this demonstration (also available as a gist):

enter image description here

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();
    }
}
查看更多
Anthone
3楼-- · 2019-07-07 02:28

Try This..

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();
    }
查看更多
登录 后发表回答