用java實現(xiàn)跳動的小球示例代碼
更新時間:2020年05月12日 14:19:02 作者:飛宇千虹
這篇文章主要介紹了用java實現(xiàn)跳動的小球,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
實現(xiàn)效果為一個小球接觸左右側時,會反向的運動。
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";
//創(chuàng)建計時器
static Timer t = new Timer();
//創(chuàng)建記錄器,當多次點擊過“確定”按鈕后,只有第一次點擊有效
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)造一個小球
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,那么小球橫坐標達到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);
}
//“開始"按鈕被點擊且事件被觸發(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("移動小球");
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之間的隨機數(shù)
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";
//創(chuàng)建計時器
static Timer t = new Timer();
//創(chuàng)建記錄器,當多次點擊過“確定”按鈕后,只有第一次點擊有效
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)建一個小球隨機角度移動的思路:
假設小球每次移動的距離為5,當橫坐標或者縱坐標其中一個確定時,另外可以根據(jù)三角函數(shù)求出
現(xiàn)在可以用隨機函數(shù),令橫坐標為1到4之間隨機的數(shù)字,那么橫坐標也可以由此求出
如何驗證每次角度不同?
當點擊“停止”按鈕之后,再次點擊“停止”按鈕即可重置小球位置和移動的角度
* */
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);
}
//“開始"按鈕被點擊且事件被觸發(fā),record=false;
record_start = false;
record_stop = false;
});
//為停止按鈕添加事件
stop.setOnAction(event -> {
/*System.out.println("停止按鈕被觸發(fā)");*/
//當?shù)诙吸c擊"停止"時,小球重置
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實現(xiàn)跳動的小球示例代碼的文章就介紹到這了,更多相關java 跳動的小球內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring中ContextLoaderListener監(jiān)聽詳解
這篇文章主要介紹了Spring中ContextLoaderListener監(jiān)聽詳解,SpringMVC啟動時會啟動WebApplicationContext類型的容器,并且會調用之前分析的refresh方法,需要的朋友可以參考下2024-01-01
SpringBoot2.x配置多數(shù)據(jù)源方式
這篇文章主要介紹了SpringBoot2.x配置多數(shù)據(jù)源方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
SpringBoot 整合 RabbitMQ 的使用方式(代碼示例)
本文詳細介紹了使用RabbitTemplate進行消息傳遞的幾種模式,包括點對點通信、發(fā)布/訂閱模式、工作隊列模式、路由模式和主題模式,每種模式都通過代碼示例展示了生產者和消費者的實現(xiàn),幫助開發(fā)者理解和運用RabbitMQ進行高效的消息處理2024-10-10

