zoukankan      html  css  js  c++  java
  • javafx KeyFrame

    import javafx.animation.Animation;
    import javafx.animation.KeyFrame;
    import javafx.animation.Timeline;
    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Circle;
    import javafx.stage.Stage;
    import javafx.util.Duration;
    
    public class Main extends Application {
        static int dx = 1;
        static int dy = 1;
    
        public static void main(String[] args) {
            Application.launch(args);
        }
    
        @Override
        public void start(final Stage primaryStage) {
            primaryStage.setTitle("Animation");
            Group root = new Group();
            Scene scene = new Scene(root, 400, 300, Color.WHITE);
    
            primaryStage.setScene(scene);
            addBouncyBall(scene); 
            primaryStage.show();
        }
        private void addBouncyBall(final Scene scene) {
            final Circle ball = new Circle(100, 100, 20);
    
            final Group root = (Group) scene.getRoot();
            root.getChildren().add(ball);
    
            Timeline tl = new Timeline();
            tl.setCycleCount(Animation.INDEFINITE);
            KeyFrame moveBall = new KeyFrame(Duration.seconds(.0200),
                    new EventHandler<ActionEvent>() {
    
                        public void handle(ActionEvent event) {
    
                            double xMin = ball.getBoundsInParent().getMinX();
                            double yMin = ball.getBoundsInParent().getMinY();
                            double xMax = ball.getBoundsInParent().getMaxX();
                            double yMax = ball.getBoundsInParent().getMaxY();
    
                            if (xMin < 0 || xMax > scene.getWidth()) {
                                dx = dx * -1;
                            }
                            if (yMin < 0 || yMax > scene.getHeight()) {
                                dy = dy * -1;
                            }
    
                            ball.setTranslateX(ball.getTranslateX() + dx);
                            ball.setTranslateY(ball.getTranslateY() + dy);
    
                        }
                    });
    
            tl.getKeyFrames().add(moveBall);
            tl.play();
        }
    }
  • 相关阅读:
    GIT Bash 简单讲解git如何推/拉代码
    python os模块详细用法
    Python基础案例练习:制作学生管理系统
    Python函数中4种参数的使用
    python基础:try...except...的详细用法
    Python关于装饰器的练习题
    ELB HTTP监听器访问慢的问题
    花生壳 b.oray.com
    euler安装使用docker
    lvs配置会话超时时间
  • 原文地址:https://www.cnblogs.com/rojas/p/4724007.html
Copyright © 2011-2022 走看看