歡迎您光臨本站 註冊首頁

用java實現跳動的小球示例代碼

←手機掃碼閱讀     sl_ivan @ 2020-05-13 , reply:0

實現效果為一個小球接觸左右側時,會反向的運動。
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) { /*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(); } }
以上代碼設置了個彩蛋,不知道你能不能找到!

[sl_ivan ] 用java實現跳動的小球示例代碼已經有303次圍觀

http://coctec.com/docs/java/show-post-234322.html