用java实现跳动的小球示例代码
作者:飞宇千虹 时间:2021-09-13 11:41:34
实现效果为一个小球接触左右侧时,会反向的运动。
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<ActionEvent>() {
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<ActionEvent>() {
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) {
/*System.out.println(distance_x+"***"+distance_y);*/
//创建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 -> {
/*创建一个小球随机角度移动的思路:
假设小球每次移动的距离为5,当横坐标或者纵坐标其中一个确定时,另外可以根据三角函数求出
现在可以用随机函数,令横坐标为1到4之间随机的数字,那么横坐标也可以由此求出
如何验证每次角度不同?
当点击“停止”按钮之后,再次点击“停止”按钮即可重置小球位置和移动的角度
* */
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 -> {
/*System.out.println("停止按钮被触发");*/
//当第二次点击"停止"时,小球重置
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();
}
}
以上代码设置了个彩蛋,不知道你能不能找到!
来源:https://blog.csdn.net/weixin_45924250/article/details/105913909
标签:java,跳动,小球
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#如何防止程序多次运行的技巧
2022-11-10 01:18:59
![](https://img.aspxhome.com/file/2023/3/126063_0s.gif)
java的内部类和外部类用法讲解
2022-10-18 21:14:41
![](https://img.aspxhome.com/file/2023/2/110822_0s.jpg)
Java用 Rhino/Nashorn 代替第三方 JSON 转换库
2023-11-04 02:20:26
Android Studio进行APP图标更改的两种方式总结
2022-02-07 09:08:40
![](https://img.aspxhome.com/file/2023/7/111747_0s.png)
c#中WinForm用OpencvSharp实现ROI区域提取的示例
2023-03-22 13:54:10
![](https://img.aspxhome.com/file/2023/1/68071_0s.jpg)
Java基于JDBC实现事务,银行转账及货物进出库功能示例
2022-03-24 07:06:34
![](https://img.aspxhome.com/file/2023/4/60764_0s.png)
线程池中execute与submit的区别说明
2023-03-18 23:09:04
![](https://img.aspxhome.com/file/2023/6/64786_0s.png)
docker 的java编译环境构建详细介绍
2023-02-10 04:08:30
SpringMVC bean加载控制的实现分析
2023-08-08 18:46:19
![](https://img.aspxhome.com/file/2023/2/57912_0s.png)
Java 判断线程池所有任务是否执行完毕的操作
2021-10-03 09:43:55
springboot省去配置Tomcat的步骤问题
2023-03-13 18:58:44
![](https://img.aspxhome.com/file/2023/2/98112_0s.png)
Android实现拍照截图功能
2023-06-21 04:43:23
![](https://img.aspxhome.com/file/2023/6/91046_0s.gif)
java页面中文乱码的解决办法
2022-09-01 22:59:20
java 安全 ysoserial CommonsCollections6 分析
2021-06-04 01:58:14
![](https://img.aspxhome.com/file/2023/3/63083_0s.png)
Android实现拍照及图片裁剪(6.0以上权限处理及7.0以上文件管理)
2022-05-19 09:18:14
C#设置文件权限的方法
2022-08-31 22:35:42
c#数据绑定之linq使用示例
2022-07-17 11:07:22
![](https://img.aspxhome.com/file/2023/0/98220_0s.jpg)
DevExpress设置饼状图的Lable位置实例
2022-02-02 15:53:37
![](https://img.aspxhome.com/file/2023/0/75620_0s.png)
举例讲解Java的Hibernate框架中的多对一和一对多映射
2023-09-05 23:05:53
Java8 Optional优雅空值判断的示例代码
2021-09-20 09:16:26
![](https://img.aspxhome.com/file/2023/2/99922_0s.png)