用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)建場(chǎ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,場(chǎng)景的寬度為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è)升級(jí)版,擴(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)建場(chǎ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,場(chǎng)景的寬度為400,那么小球橫坐標(biāo)達(dá)到20或者380時(shí),轉(zhuǎn)向移動(dòng)
if (x < 20) { temp1 = "right"; }
if (x > 380) { temp1 = "left"; }
//小球的半徑為20,場(chǎng)景的高度為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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot整合Security安全框架實(shí)現(xiàn)控制權(quán)限
本文主要介紹了SpringBoot整合Security安全框架實(shí)現(xiàn)控制權(quán)限,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
聊聊@RequestParam,@PathParam,@PathVariable等注解的區(qū)別
這篇文章主要介紹了聊聊@RequestParam,@PathParam,@PathVariable等注解的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Java?synchornized與ReentrantLock處理并發(fā)出現(xiàn)的錯(cuò)誤
synchronized機(jī)制提供了對(duì)每個(gè)對(duì)象相關(guān)的隱式監(jiān)視器鎖,并強(qiáng)制所有鎖的獲取和釋放都必須在同一個(gè)塊結(jié)構(gòu)中。當(dāng)獲取了多個(gè)鎖時(shí),必須以相反的順序釋放。即synchronized對(duì)于鎖的釋放是隱式的2023-01-01
java讀取文件內(nèi)容,解析Json格式數(shù)據(jù)方式
這篇文章主要介紹了java讀取文件內(nèi)容,解析Json格式數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Java中關(guān)于Collections集合工具類的詳細(xì)介紹
Java提供了一個(gè)操作Set、List和Map等集合的工具類:Collections,該工具提供了大量方法對(duì)集合元素進(jìn)行排序、查詢和修改等操作,還提供了將集合對(duì)象設(shè)置為不可變、對(duì)集合對(duì)象實(shí)現(xiàn)同步控制等方法2021-09-09
使用JMX監(jiān)控Zookeeper狀態(tài)Java API
今天小編就為大家分享一篇關(guān)于使用JMX監(jiān)控Zookeeper狀態(tài)Java API,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03
基于<aop:aspect>與<aop:advisor>的區(qū)別
這篇文章主要介紹了<aop:aspect>與<aop:advisor>的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot接收請(qǐng)求參數(shù)的四種方式總結(jié)
這篇文章主要給大家介紹了關(guān)于SpringBoot接收請(qǐng)求參數(shù)的四種方式,文中通過代碼以及圖文介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用SpringBoot具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09

