首页 > Java > java教程 > 正文

案例研究:弹跳球

王林
发布: 2024-07-16 22:12:01
原创
956 人浏览过

本节介绍一个显示弹跳球并允许用户添加和删除球的程序。

部分展示了一个显示一个弹跳球的程序。本节介绍一个显示多个弹跳球的程序。您可以使用两个按钮来暂停和恢复球的运动,使用滚动条来控制球的速度,以及+或-按钮添加或删除球,如下图所示。

Image description

第 1 节中的示例只需存储一个球。在这个例子中你如何存储多个球? PanegetChildren() 方法返回一个 ObservableList,它是 List 的子类型,用于存储窗格中的节点。最初,列表是空的。创建新球后,将其添加到列表末尾。要删除球,只需删除列表中的最后一个即可。

每个球都有其状态:x、y 坐标、颜色和移动方向。您可以定义一个名为 Ball 的类,它扩展 javafx.scene.shape.Circle。 x、y 坐标和颜色已在 Circle 中定义。当球被创建时,它从左上角开始向右下方移动。随机颜色被分配给新球。

MultiplBallPane 类负责显示球,MultipleBounceBall 类放置控件组件并实现控件。这些类的关系如下图所示。下面的代码给出了程序。

Image description

package application;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.util.Duration;

public class MultipleBounceBall extends Application {
    @Override // Override the start method in the Application class
    public void start(Stage primaryStage) {
        MultipleBallPane ballPane = new MultipleBallPane();
        ballPane.setStyle("-fx-border-color: yellow");

        Button btAdd = new Button("+");
        Button btSubtract = new Button("-");
        HBox hBox = new HBox(10);
        hBox.getChildren().addAll(btAdd, btSubtract);
        hBox.setAlignment(Pos.CENTER);

        // Add or remove a ball
        btAdd.setOnAction(e -> ballPane.add());
        btSubtract.setOnAction(e -> ballPane.subtract());

        // Pause and resume animation
        ballPane.setOnMousePressed(e -> ballPane.pause());
        ballPane.setOnMouseReleased(e -> ballPane.play());

        // Use a scroll bar to control animation speed
        ScrollBar sbSpeed = new ScrollBar();
        sbSpeed.setMax(20);
        sbSpeed.setValue(10);
        ballPane.rateProperty().bind(sbSpeed.valueProperty());

        BorderPane pane = new BorderPane();
        pane.setCenter(ballPane);
        pane.setTop(sbSpeed);
        pane.setBottom(hBox);

        // Create a scene and place the pane in the stage
        Scene scene = new Scene(pane, 250, 150);
        primaryStage.setTitle("MultipleBounceBall"); // Set the stage title
        primaryStage.setScene(scene); // Place the scene in the stage
        primaryStage.show(); // Display the stage
    }

    public static void main(String[] args) {
        Application.launch(args);
    }

    private class MultipleBallPane extends Pane {
        private Timeline animation;

        public MultipleBallPane() {
            // Create an animation for moving the ball
            animation = new Timeline(new KeyFrame(Duration.millis(50), e -> moveBall()));
            animation.setCycleCount(Timeline.INDEFINITE);
            animation.play(); // Start animation
        }

        public void add() {
            Color color = new Color(Math.random(), Math.random(), Math.random(), 0.5);
            getChildren().add(new Ball(30, 30, 20, color));
        }

        public void subtract() {
            if(getChildren().size() > 0) {
                getChildren().remove(getChildren().size() - 1);
            }
        }

        public void play() {
            animation.play();
        }

        public void pause() {
            animation.pause();
        }

        public void increaseSpeed() {
            animation.setRate(animation.getRate() + 0.1);
        }

        public void decreaseSpeed() {
            animation.setRate(animation.getRate() > 0 ? animation.getRate() - 0.1 : 0);
        }

        public DoubleProperty rateProperty() {
            return animation.rateProperty();
        }

        protected void moveBall() {
            for(Node node: this.getChildren()) {
                Ball ball = (Ball)node;
                // Check boundaries
                if(ball.getCenterX() < ball.getRadius() || ball.getCenterX() > getWidth() - ball.getRadius()) {
                    ball.dx *= -1; // Change ball move direction
                }
                if(ball.getCenterY() < ball.getRadius() || ball.getCenterY() > getHeight() - ball.getRadius()) {
                    ball.dy *= -1; // Change ball move direction
                }

                // Adjust ball position
                ball.setCenterX(ball.dx + ball.getCenterX());
                ball.setCenterY(ball.dy + ball.getCenterY());
            }
        }
    }

    class Ball extends Circle {
        private double dx = 1, dy = 1;

        Ball(double x, double y, double radius, Color color) {
            super(x, y, radius);
            setFill(color); // Set ball color
        }
    }
}

登录后复制

add() 方法创建一个具有随机颜色的新球并将其添加到窗格中(第 73 行)。该窗格将所有球存储在列表中。 subtract() 方法删除列表中的最后一个球(第 78 行)。

当用户单击 + 按钮时,一个新球将添加到窗格中(第 32 行)。当用户单击 - 按钮时,数组列表中的最后一个球将被删除(第 33 行)。

MultipleBallPane 类中的 moveBall() 方法获取窗格列表中的每个球并调整球的位置(第 114-115 行)。

以上是案例研究:弹跳球的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!