欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用javaFX實(shí)現(xiàn)移動一個小球的示例代碼

 更新時間:2020年09月27日 10:59:04   作者:韋又又V  
這篇文章主要介紹了利用javaFX實(shí)現(xiàn)移動一個小球的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

題目:編寫一個程序,在面板上移動小球。應(yīng)該定義一個面板類來顯示小球,并提供向上下左右移動小球的方法。請進(jìn)行邊界檢查以防止小球移動到視線之外。

問題:我寫的程序可以運(yùn)行但是無法顯示小球的移動,如果將移動改為改變小球顏色則可以顯示,檢查許久也檢查不到問題在哪,所以貼上來問問大佬們,問題出在哪里?應(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 自動生成的方法存根
  circlePane.moveLeft();
  }
 });
 bt2.setOnAction(new EventHandler<ActionEvent>() {
  @Override
  public void handle(ActionEvent event) {
  // TODO 自動生成的方法存根
  circlePane.moveRight();
  }
 });
 bt3.setOnAction(new EventHandler<ActionEvent>() {
  @Override
  public void handle(ActionEvent event) {
  // TODO 自動生成的方法存根
  circlePane.moveUp();
  }  
 });
 bt4.setOnAction(new EventHandler<ActionEvent>() {
  @Override
  public void handle(ActionEvent event) {
  // TODO 自動生成的方法存根
  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)移動一個小球的示例代碼的文章就介紹到這了,更多相關(guān)javaFX 移動小球內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論