這篇文章主要講解了java如何實現(xiàn)跳動的小球,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。

實現(xiàn)效果為一個小球接觸左右側(cè)時,會反向的運(yù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記錄小球的橫坐標(biāo),默認(rèn)值為初始位置
static int x = 200;
//e為小球
static Ellipse e = new Ellipse();
//temp記錄小球的移動情況:當(dāng)temp為left時,左移;temp為right時,右移
static String temp = "left";
//創(chuàng)建計時器
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)造一個小球
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時,轉(zhuǎn)向移動
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("移動小球");
s.setScene(scene);
s.show();
}
}另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站chinadenli.net,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
名稱欄目:java如何實現(xiàn)跳動的小球-創(chuàng)新互聯(lián)
當(dāng)前地址:http://chinadenli.net/article48/piehp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、網(wǎng)站維護(hù)、ChatGPT、域名注冊、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容