JavaFx-实现在面板上移动的小球


0.题目描述

《Java语言程序设计与数据结构(基础篇)》编程练习题15.3

15.3(移动小球)编写一个程序,在面板上移动小球。需定义一个面板类来显示小球,并提供向左、向右、向上和向下移动小球的方法。请进行边界检查以防止球完全移到视线之外。

1.UML类图

球类Ball

 

画板类Board

 

框架类Frame

 

 

 

 2.源代码

import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.layout.BorderPane; import javafx.scene.layout.Pane; import javafx.scene.shape.Circle; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.scene.layout.Background; import javafx.scene.layout.BackgroundFill; public class Test1 extends Application { Ball ball = new Ball(200, 100, 30); @Override public void start(Stage primaryStage) { Board board = new Board(ball); //Pane  Frame frame = new Frame(); //HBox  BorderPane borderPane = new BorderPane(); borderPane.setCenter(board); borderPane.setBottom(frame); Scene scene = new Scene(borderPane, 390, 240); board.setBackground(new Background(new BackgroundFill(Color.RED,null,null))); primaryStage.setResizable(false); primaryStage.setTitle("MoveBall"); primaryStage.setScene(scene); primaryStage.show(); frame.btLeft.setOnAction(new LeftHandler()); frame.btRight.setOnAction(new RightHandler()); frame.btUp.setOnAction(new UpHandler()); frame.btDown.setOnAction(new DownHandler()); } class LeftHandler implements EventHandler <ActionEvent> { @Override public void handle(ActionEvent e) { ball.Left(); ball.checkBall(); } } class RightHandler implements EventHandler <ActionEvent> { @Override public void handle(ActionEvent e) { ball.Right(); ball.checkBall(); } } class UpHandler implements EventHandler <ActionEvent> { @Override public void handle(ActionEvent e) { ball.Up(); ball.checkBall(); } } class DownHandler implements EventHandler <ActionEvent> { @Override public void handle(ActionEvent e) { ball.Down(); ball.checkBall(); } } public static void main(String[] args) { Application.launch(args); } } //球类 class Ball extends Circle { public Ball() { } public Ball(double x, double y, double r) { super(x,y,r); setStroke(Color.BLACK); //设置颜色 setFill(Color.WHITE); //设置填充  } public void Left() { setCenterX(getCenterX() - 10); } public void Right() { setCenterX(getCenterX() + 10); } public void Up() { setCenterY(getCenterY() - 10); } public void Down() { setCenterY(getCenterY() + 10); } public void checkBall() { System.out.println("X:"+getCenterX()+" Y:"+getCenterY()); if (getCenterX() < 0) { Right(); } if (getCenterX() > 400) { Left(); } if (getCenterY() < 0) { Down(); } if (getCenterY() > 220) { Up(); } System.out.println("X:"+getCenterX()+" Y:"+getCenterY()+"\n"); } } //画板类 class Board extends Pane { public Board() {} public Board(Ball ball) { getChildren().add(ball); } } //框架类 class Frame extends HBox { public Button btLeft = new Button("Left"); public Button btRight = new Button("Right"); public Button btUp = new Button("Up"); public Button btDown = new Button("Down"); public Frame() { this.getChildren().add(btLeft); this.getChildren().add(btRight); this.getChildren().add(btUp); this.getChildren().add(btDown); setAlignment(Pos.CENTER); } }

3.运行结果

 

 

 边界情况:

 

 

 

 

 

 4.技术总结

  将四个Button放在一个HBox中,使用BorderPane将画板和HBox分别设置在上(中)部和下部。

  难点主要在于按钮调动函数的格式写法。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM