用java實(shí)現(xiàn)跳動(dòng)的小球示例代碼
實(shí)現(xiàn)效果為一個(gè)小球接觸左右側(cè)時(shí),會(huì)反向的運(yùn)動(dòng)。
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記錄小球的橫坐標(biāo),默認(rèn)值為初始位置
static int x = 200;
//e為小球
static Ellipse e = new Ellipse();
//temp記錄小球的移動(dòng)情況:當(dāng)temp為left時(shí),左移;temp為right時(shí),右移
static String temp = "left";
//創(chuàng)建計(jì)時(shí)器
static Timer t = new Timer();
//創(chuàng)建記錄器,當(dāng)多次點(diǎn)擊過“確定”按鈕后,只有第一次點(diǎn)擊有效
static boolean record = true;
public static void main(String[] args) {
launch(args);
}
public void start(Stage s) {
//創(chuàng)建Group面板
Group root = new Group();
//創(chuàng)建場景
Scene scene = new Scene(root, 400, 250, Color.WHITE);
//創(chuàng)建按鈕
Button start = new Button("開始");
Button stop = new Button("停止");
//創(chuàng)造一個(gè)小球
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("開始按鈕被觸發(fā)");
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,那么小球橫坐標(biāo)達(dá)到50或者350時(shí),轉(zhuǎn)向移動(dòng)
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);
}
//“開始"按鈕被點(diǎn)擊且事件被觸發(fā),record=false;
record=false;
}
});
//為停止按鈕添加事件
stop.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
System.out.println("停止按鈕被觸發(fā)");
record = true;
t.cancel();
}
});
root.getChildren().add(e);
root.getChildren().add(start);
root.getChildren().add(stop);
s.setTitle("移動(dòng)小球");
s.setScene(scene);
s.show();
}
}
我還做了一個(gè)升級版,擴(kuò)展到上下左右一起移動(dòng),代碼如下
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記錄小球的橫坐標(biāo),默認(rèn)值為初始位置
static int x = 200;
//y記錄小球的縱坐標(biāo),默認(rèn)值為初始位置
static int y = 90;
//distance_x記錄小球每次橫向移動(dòng)的距離,取1到4之間的隨機(jī)數(shù)
static double distance_x = Math.random()*4+1;
//distance_y記錄小球每次縱向移動(dòng)的距離,由于每次移動(dòng)的距離為5,由distance_x可求出distance_y
static double distance_y = Math.sqrt(25-distance_x*distance_x);
//e為小球
static Ellipse e = new Ellipse();
//temp1記錄小球的橫向移動(dòng)情況:當(dāng)temp為left時(shí),左移;temp為right時(shí),右移
static String temp1 = "left";
//temp2記錄小球的縱向移動(dòng)情況:當(dāng)temp為up時(shí),上移;temp為down時(shí),下移
static String temp2 = "down";
//創(chuàng)建計(jì)時(shí)器
static Timer t = new Timer();
//創(chuàng)建記錄器,當(dāng)多次點(diǎn)擊過“確定”按鈕后,只有第一次點(diǎn)擊有效
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);*/
//創(chuàng)建Grooup面板
Group root = new Group();
//創(chuàng)建場景
Scene scene = new Scene(root, 400, 250, Color.WHITE);
//創(chuàng)建按鈕
Button start = new Button("開始");
Button stop = new Button("停止");
//創(chuàng)建一條分割線,分割小球和按鈕
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 -> {
/*創(chuàng)建一個(gè)小球隨機(jī)角度移動(dòng)的思路:
假設(shè)小球每次移動(dòng)的距離為5,當(dāng)橫坐標(biāo)或者縱坐標(biāo)其中一個(gè)確定時(shí),另外可以根據(jù)三角函數(shù)求出
現(xiàn)在可以用隨機(jī)函數(shù),令橫坐標(biāo)為1到4之間隨機(jī)的數(shù)字,那么橫坐標(biāo)也可以由此求出
如何驗(yàn)證每次角度不同?
當(dāng)點(diǎn)擊“停止”按鈕之后,再次點(diǎn)擊“停止”按鈕即可重置小球位置和移動(dòng)的角度
* */
if(record_start) {
t = new Timer();
t.schedule(new TimerTask() {
public void run() {
//隨機(jī)取顏色,just have a fun
//e.setFill( Color.rgb((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
//小球的半徑為20,場景的寬度為400,那么小球橫坐標(biāo)達(dá)到20或者380時(shí),轉(zhuǎn)向移動(dòng)
if (x < 20) { temp1 = "right"; }
if (x > 380) { temp1 = "left"; }
//小球的半徑為20,場景的高度為160,那么小球縱坐標(biāo)達(dá)到20或者140時(shí),轉(zhuǎn)向移動(dòng)
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);
}
//“開始"按鈕被點(diǎn)擊且事件被觸發(fā),record=false;
record_start = false;
record_stop = false;
});
//為停止按鈕添加事件
stop.setOnAction(event -> {
/*System.out.println("停止按鈕被觸發(fā)");*/
//當(dāng)?shù)诙吸c(diǎn)擊"停止"時(shí),小球重置
if(record_stop){
//重置橫向和縱向移動(dòng)的距離
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();
}
}
以上代碼設(shè)置了個(gè)彩蛋,不知道你能不能找到!
總結(jié)
到此這篇關(guān)于用java實(shí)現(xiàn)跳動(dòng)的小球示例代碼的文章就介紹到這了,更多相關(guān)java 跳動(dòng)的小球內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java jvm兩種存儲區(qū)的類型知識點(diǎn)講解
在本篇文章里小編給大家整理的是一篇關(guān)于java jvm兩種存儲區(qū)的類型知識點(diǎn)講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-03-03
Spring中ContextLoaderListener監(jiān)聽詳解
這篇文章主要介紹了Spring中ContextLoaderListener監(jiān)聽詳解,SpringMVC啟動(dòng)時(shí)會(huì)啟動(dòng)WebApplicationContext類型的容器,并且會(huì)調(diào)用之前分析的refresh方法,需要的朋友可以參考下2024-01-01
SpringBoot2.x配置多數(shù)據(jù)源方式
這篇文章主要介紹了SpringBoot2.x配置多數(shù)據(jù)源方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
SpringBoot 整合 RabbitMQ 的使用方式(代碼示例)
本文詳細(xì)介紹了使用RabbitTemplate進(jìn)行消息傳遞的幾種模式,包括點(diǎn)對點(diǎn)通信、發(fā)布/訂閱模式、工作隊(duì)列模式、路由模式和主題模式,每種模式都通過代碼示例展示了生產(chǎn)者和消費(fèi)者的實(shí)現(xiàn),幫助開發(fā)者理解和運(yùn)用RabbitMQ進(jìn)行高效的消息處理2024-10-10
java實(shí)現(xiàn)小型局域網(wǎng)群聊功能(C/S模式)
這篇文章主要介紹了java利用TCP協(xié)議實(shí)現(xiàn)小型局域網(wǎng)群聊功能(C/S模式) ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
MyBatis?SQL映射文件的作用和結(jié)構(gòu)詳解
MyBatisSQL映射文件定義了SQL語句和參數(shù)映射規(guī)則,用于將Java代碼與數(shù)據(jù)庫操作解耦,實(shí)現(xiàn)SQL語句的靈活配置和動(dòng)態(tài)生成2025-03-03
eclipse實(shí)現(xiàn)DSA數(shù)字簽名
這篇文章主要為大家詳細(xì)介紹了eclipse實(shí)現(xiàn)DSA數(shù)字簽名算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06

