栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

用java实现跳动的小球示例代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用java实现跳动的小球示例代码

实现效果为一个小球接触左右侧时,会反向的运动。

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;

import java.util.Timer;
import java.util.TimerTask;


public class BallMove extends Application {
  //x记录小球的横坐标,默认值为初始位置
  static int x = 200;
  //e为小球
  static Ellipse e = new Ellipse();
  //temp记录小球的移动情况:当temp为left时,左移;temp为right时,右移
  static String temp = "left";
  //创建计时器
  static Timer t = new Timer();
  //创建记录器,当多次点击过“确定”按钮后,只有第一次点击有效
  static boolean record = true;
  public static void main(String[] args) {
    launch(args);
  }

  public void start(Stage s) {
    //创建Group面板
    Group root = new Group();
    //创建场景
    Scene scene = new Scene(root, 400, 250, Color.WHITE);
    //创建按钮
    Button start = new Button("开始");
    Button stop = new Button("停止");
    //创造一个小球
    e.setCenterX(x);
    e.setCenterY(90);
    e.setRadiusX(50);
    e.setRadiusY(50);
    e.setFill(Color.RED);
    //放置开始按钮
    start.setLayoutX(100);
    start.setLayoutY(180);
    //放置停止按钮
    stop.setLayoutX(250);
    stop.setLayoutY(180);
    //为开始按钮添加事件
    start.setonAction(new EventHandler() {
      public void handle(ActionEvent event) {
 System.out.println("开始按钮被触发");
 if(record==true) {
   t = new Timer();
   t.schedule(new TimerTask() {
     public void run() {
e.setFill( Color.rgb((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
//小球的半径为50,场景的宽度为400,那么小球横坐标达到50或者350时,转向移动
if (x < 50) { temp = "right"; }
if (x > 350) { temp = "left"; }
if (temp.equals("left")) { e.setCenterX(x -= 5);
} else { e.setCenterX(x += 5); }
     }
   }, 0, 25);
 }
 //“开始"按钮被点击且事件被触发,record=false;
 record=false;
      }
    });
    //为停止按钮添加事件
    stop.setonAction(new EventHandler() {
      public void handle(ActionEvent event) {
 System.out.println("停止按钮被触发");
 record = true;
 t.cancel();
      }
    });

    root.getChildren().add(e);
    root.getChildren().add(start);
    root.getChildren().add(stop);
    s.setTitle("移动小球");
    s.setScene(scene);
    s.show();
  }
}

我还做了一个升级版,扩展到上下左右一起移动,代码如下

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

import java.util.Timer;
import java.util.TimerTask;


public class BallMove2 extends Application {
  //x记录小球的横坐标,默认值为初始位置
  static int x = 200;
  //y记录小球的纵坐标,默认值为初始位置
  static int y = 90;
  //distance_x记录小球每次横向移动的距离,取1到4之间的随机数
  static double distance_x = Math.random()*4+1;
  //distance_y记录小球每次纵向移动的距离,由于每次移动的距离为5,由distance_x可求出distance_y
  static double distance_y = Math.sqrt(25-distance_x*distance_x);
  //e为小球
  static Ellipse e = new Ellipse();
  //temp1记录小球的横向移动情况:当temp为left时,左移;temp为right时,右移
  static String temp1 = "left";
  //temp2记录小球的纵向移动情况:当temp为up时,上移;temp为down时,下移
  static String temp2 = "down";
  //创建计时器
  static Timer t = new Timer();
  //创建记录器,当多次点击过“确定”按钮后,只有第一次点击有效
  static boolean record_start = true;
  static boolean record_stop = false;

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

  public void start(Stage s) {
    

    //创建Grooup面板
    Group root = new Group();
    //创建场景
    Scene scene = new Scene(root, 400, 250, Color.WHITE);
    //创建按钮
    Button start = new Button("开始");
    Button stop = new Button("停止");
    //创建一条分割线,分割小球和按钮
    Line l = new Line();
    //放置线条
    l.setStartX(0);
    l.setStartY(160);
    l.setEndY(160);
    l.setEndX(400);
    //放置小球
    e.setCenterX(x);
    e.setCenterY(y);
    e.setRadiusX(20);
    e.setRadiusY(20);
    e.setFill(Color.RED);
    //放置开始按钮
    start.setLayoutX(100);
    start.setLayoutY(190);
    //放置停止按钮
    stop.setLayoutX(250);
    stop.setLayoutY(190);

    //为开始按钮添加事件
    start.setonAction(event -> {
 
      if(record_start) {
 t = new Timer();
 t.schedule(new TimerTask() {
   public void run() {
     //随机取颜色,just have a fun
     //e.setFill( Color.rgb((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
     //小球的半径为20,场景的宽度为400,那么小球横坐标达到20或者380时,转向移动
     if (x < 20) { temp1 = "right"; }
     if (x > 380) { temp1 = "left"; }
     //小球的半径为20,场景的高度为160,那么小球纵坐标达到20或者140时,转向移动
     if (y < 20) { temp2 = "up";}
     if (y > 140) { temp2 = "down"; }
     if (temp1.equals("left")) { e.setCenterX(x -= distance_x);
     } else { e.setCenterX(x += distance_x); }
     if (temp2.equals("down")) { e.setCenterY(y -= distance_y);
     } else { e.setCenterY(y += distance_y); }
   }
 }, 0, 20);
      }
      //“开始"按钮被点击且事件被触发,record=false;
      record_start = false;
      record_stop = false;
    });

    //为停止按钮添加事件
    stop.setonAction(event -> {
      
      //当第二次点击"停止"时,小球重置
      if(record_stop){
 //重置横向和纵向移动的距离
 distance_x = Math.random()*4+1;
 distance_y = Math.sqrt(25-distance_x*distance_x);
 //重置小球的位置
 e.setCenterX(x = 200);
 e.setCenterY(y = 90);
 record_stop = false;
      }
      record_stop = true;
      record_start = true;
      t.cancel();
    });

    root.getChildren().addAll(start,stop,l,e);
    s.setTitle("弹跳小球");
    s.setScene(scene);
    s.show();
  }
}

以上代码设置了个彩蛋,不知道你能不能找到!

总结

到此这篇关于用java实现跳动的小球示例代码的文章就介绍到这了,更多相关java 跳动的小球内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/134169.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号