利用javaFX實(shí)現(xiàn)移動(dòng)一個(gè)小球的示例代碼
題目:編寫一個(gè)程序,在面板上移動(dòng)小球。應(yīng)該定義一個(gè)面板類來顯示小球,并提供向上下左右移動(dòng)小球的方法。請進(jìn)行邊界檢查以防止小球移動(dòng)到視線之外。
問題:我寫的程序可以運(yùn)行但是無法顯示小球的移動(dòng),如果將移動(dòng)改為改變小球顏色則可以顯示,檢查許久也檢查不到問題在哪,所以貼上來問問大佬們,問題出在哪里?應(yīng)該如何改?
代碼如下:
public class MoveBall_3 extends Application{
private CirclePane circlePane = new CirclePane(250,250);
public static void main(String[] args) {
Application.launch(args);
}
public void start(Stage primaryStage) throws Exception {
Button bt1 = new Button("Left");
Button bt2 = new Button("Right");
Button bt3 = new Button("Up");
Button bt4 = new Button("Down");
bt1.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
// TODO 自動(dòng)生成的方法存根
circlePane.moveLeft();
}
});
bt2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// TODO 自動(dòng)生成的方法存根
circlePane.moveRight();
}
});
bt3.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// TODO 自動(dòng)生成的方法存根
circlePane.moveUp();
}
});
bt4.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// TODO 自動(dòng)生成的方法存根
circlePane.moveDown();
}
});
FlowPane pane2 = new FlowPane();
pane2.getChildren().addAll(bt1,bt2,bt3,bt4);
circlePane.getChildren().addAll(pane2);
Scene scene = new Scene(circlePane,500,500);
primaryStage.setTitle("MoveBall");
primaryStage.setScene(scene);
primaryStage.show();
}
}
class CirclePane extends StackPane{
private Circle circle = new Circle(250,250,50);
public CirclePane() {
getChildren().add(circle);
circle.setStroke(Color.BLACK);
circle.setFill(Color.WHITE);
}
public CirclePane(double a,double b) {
getChildren().add(circle);
circle.setCenterX(a);
circle.setCenterY(b);
circle.setStroke(Color.BLACK);
circle.setFill(Color.WHITE);
}
public void moveLeft() {
if(circle.getCenterX()-50-15 >= 0) {
circle.setCenterX(circle.getCenterX()-15);
circle.setCenterY(circle.getCenterY());
}
else {
circle.setCenterX(50);
circle.setCenterY(circle.getCenterY());
}
}
public void moveRight() {
if(circle.getCenterX()+50+15 <= 500) {
circle.setCenterX(circle.getCenterX()+15);
circle.setCenterY(circle.getCenterY());
}
else {
circle.setCenterX(450);
circle.setCenterY(circle.getCenterY());
}
}
public void moveUp() {
if(circle.getCenterY()-50-15 >= 0) {
circle.setCenterY(circle.getCenterY()-15);
circle.setCenterX(circle.getCenterX());
}
else {
circle.setCenterY(50);
circle.setCenterX(circle.getCenterX());
}
}
public void moveDown() {
if(circle.getCenterY()+50+15 <= 500) {
circle.setCenterY(circle.getCenterY()+15);
circle.setCenterX(circle.getCenterX());
}
else {
circle.setCenterY(450);
circle.setCenterX(circle.getCenterX());
}
}
}
到此這篇關(guān)于利用javaFX實(shí)現(xiàn)移動(dòng)一個(gè)小球的示例代碼的文章就介紹到這了,更多相關(guān)javaFX 移動(dòng)小球內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)Excel百萬級數(shù)據(jù)導(dǎo)入功能的示例代碼
這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)Excel百萬級數(shù)據(jù)導(dǎo)入功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下2024-04-04
Java語言實(shí)現(xiàn)簡單FTP軟件 FTP協(xié)議分析(1)
這篇文章主要介紹了Java語言實(shí)現(xiàn)簡單FTP軟件的第一篇,針對FTP協(xié)議進(jìn)行分析,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Java實(shí)現(xiàn)生成pdf并解決表格分割的問題
這篇文章主要為大家詳細(xì)介紹了如何利用Java實(shí)現(xiàn)生成pdf,并解決表格分割的問題,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11
Java下載遠(yuǎn)程服務(wù)器文件到本地(基于http協(xié)議和ssh2協(xié)議)
這篇文章主要介紹了Java下載遠(yuǎn)程服務(wù)器文件到本地的方法(基于http協(xié)議和ssh2協(xié)議),幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2021-01-01
Struts2開發(fā) 基本配置與類型轉(zhuǎn)換
本篇文章,小編將為大家介紹關(guān)于Struts2開發(fā) 基本配置與類型轉(zhuǎn)換,有需要的朋友可以參考一下2013-04-04
詳解context root修改無效web修改項(xiàng)目路徑(eclipse)
這篇文章主要介紹了詳解context root修改無效web修改項(xiàng)目路徑(eclipse)的相關(guān)資料,需要的朋友可以參考下2017-04-04
SpringCloud LoadBalancer自定義負(fù)載均衡器使用解析
LoadBalancerClient 是 SpringCloud 提供的一種負(fù)載均衡客戶端,Ribbon 負(fù)載均衡組件內(nèi)部也是集成了 LoadBalancerClient 來實(shí)現(xiàn)負(fù)載均衡,本文給大家深入解析 LoadBalancerClient 接口源碼,感興趣的朋友跟隨小編一起看看吧2023-04-04
java向mysql插入數(shù)據(jù)亂碼問題的解決方法
這篇文章主要為大家詳細(xì)介紹了java向mysql插入數(shù)據(jù)亂碼問題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
SpringSecurity6自定義JSON登錄的實(shí)現(xiàn)
目前最新版的Spring Boot已經(jīng)到了3.0.5了,隨之而來Spring Security 目前的版本也到了6.0.2了,Spring Security寫法的變化特別多,本文就來介紹下,感興趣的可以了解一下2023-12-12

