zoukankan      html  css  js  c++  java
  • 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); 
        }
    }
    爱我没结果!
  • 相关阅读:
    通俗易懂的讲解iphone视图控制器的生命周期
    HTML5学习(十一)---服务器发送事件
    HTML5学习(十)---Web Workers
    HTML5学习(九)----应用程序缓存
    HTML5学习(八)----Web存储
    HTML5学习(七)----地理定位
    HTML5学习(六)---------SVG 与Canvas
    HTML5学习(五)----SVG
    HTML5学习(四)---Canvas绘图
    第六章在U盘上运行openwrt(引导)--补
  • 原文地址:https://www.cnblogs.com/angoli/p/12697823.html
Copyright © 2011-2022 走看看