亲宝软件园·资讯

展开

javaFX 移动小球 利用javaFX实现移动一个小球的代码实例

韦又又V 人气:0
想了解利用javaFX实现移动一个小球的代码实例的相关内容吗,韦又又V在本文为您仔细讲解javaFX 移动小球的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:javaFX,移动小球,下面大家一起来学习吧。

题目:编写一个程序,在面板上移动小球。应该定义一个面板类来显示小球,并提供向上下左右移动小球的方法。请进行边界检查以防止小球移动到视线之外。

问题:我写的程序可以运行但是无法显示小球的移动,如果将移动改为改变小球颜色则可以显示,检查许久也检查不到问题在哪,所以贴上来问问大佬们,问题出在哪里?应该如何改?

代码如下:

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());
  }
 }
}

加载全部内容

相关教程
猜你喜欢
用户评论