JavaFX手眼协调小游戏(赋源代码)

(游戏:手眼协调)请编写一个程序,显示一个半径为10 像素的实心圆,该圆放置在面板上的随机位置,并填充随机的顔色,如图15-29b所示。单击这个圆时,它会消失,然后在另一个随机的位置显示新的随机颜色的圆。在单击了20 个圆之后,在面板上显示所用的时间。

package javaseniorprograme;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

/**
 * 游戏,手眼协调
 * @author 李安国
 */
public class Exercise15_19 extends Application{
    static int count = 0;
    @Override
    public void start(Stage stage){
        // 创建一个面板
        Pane pane = new Pane(); 
        // 创建一个半径为10像素的圆
        Circle circle = new Circle(10);
        pane.getChildren().add(circle);
        //  圆的初始位置设为随机
        Scene scene = new Scene(pane,400,300);
        circle.setCenterX(Math.random()*scene.getWidth()); 
        circle.setCenterY(Math.random()*scene.getHeight()); 
        circle.setFill(Color.color(Math.random(),Math.random(),Math.random()));
        
        stage.setTitle("Exercise15_19"); 
        stage.setScene(scene);
        stage.show();
        
        long time = System.currentTimeMillis();
        circle.setOnMouseClicked(e->{
            circle.setCenterX(Math.random()*scene.getWidth()-10); 
            circle.setCenterY(Math.random()*scene.getHeight()-10); 
            circle.setFill(Color.color(Math.random(),Math.random(),Math.random()));
            count++;
            if(count==20){
                System.out.println("共计用时"+(System.currentTimeMillis()-time)+"millseconds");
            }
        });
    }
    
    public static void main(String[] args){
        Application.launch(args); 
    }
}

 

posted @ 2020-04-14 14:26  AngoLi  阅读(778)  评论(1编辑  收藏  举报