边界更新,即使声明为最终的

Bounds updating even though declared as final

我正在尝试制作一个程序,其中的球(首先)在接近屏幕边界时会弹跳。

然而,当检查 bounds.getmaxY() 值时,我可以看到该值正在增加,我猜是因为从未使用过 if 循环。

public class bouncyFX extends Application {


public static void main(String[] args) {
    launch(args);
}
static Pane pane = new Pane();

@Override
public void start(final Stage primaryStage) {
    Scene scene = new Scene(pane, 500, 200);
    primaryStage.setScene(scene);
    primaryStage.show();
    pane.setOnMouseClicked(new EventHandler<MouseEvent>() {
        public void handle(final MouseEvent event) {
            final Ball ball = new Ball(event.getX(), event.getY(), 12, Color.AQUA);
            ball.relocate(event.getX(), event.getY());
            pane.getChildren().addAll(ball);
            final Timeline loop = new Timeline(new KeyFrame(Duration.millis(10), new EventHandler<ActionEvent>() {
                double deltaX = 2;
                double deltaY = 2;
                public void handle(final ActionEvent event) {
                    ball.setLayoutX(ball.getLayoutX() + deltaX);
                    ball.setLayoutY(ball.getLayoutY() + deltaY);
                    ball.Collision(deltaX, deltaY);

                    final Bounds bounds = pane.getBoundsInLocal();
                    final boolean atRightBorder = ball.getLayoutX() >= (bounds.getMaxX()-ball.getRadius());
                    final boolean atLeftBorder = ball.getLayoutX() <= (bounds.getMinX()+ball.getRadius());
                    final boolean atBottomBorder = ball.getLayoutY() >= (bounds.getMaxY()-ball.getRadius());
                    final boolean atTopBorder = ball.getLayoutY() <= (bounds.getMinY()+ball.getRadius());
                    if(atRightBorder || atLeftBorder)
                        deltaX *= -1;
                    if(atBottomBorder ||atTopBorder)
                        deltaY *= -1;
                }
            }));
            loop.setCycleCount(Timeline.INDEFINITE);
            loop.play();
        }
    });
}

将值声明为 final 不会使对象不可变。它仅仅意味着对对象的引用不会改变——即每次你引用 bounds,你将得到相同的对象。一旦分配,final 变量就不能更改为指向另一个 Bounds 对象。

无法分配 final 变量,但您可以更改它的属性,因为您不会使其不可变。

看看 this article 关于定义不可变对象的内容。

在您的情况下,如果您有权访问 Bounds 对象,则可以使用此解决方案实现它,但如果您无法修改 Bounds 对象以使其不可变,则需要编写一个包装器 class 为界限。

你的 Bounds 变量根本没有改变,你每次都只是得到一个新的实例。

认为 这里发生的事情是在查询窗格的边界之前更改球的布局。窗格正在增长以适应球位置的变化。所以试试

                final Bounds bounds = pane.getBoundsInLocal();
                final boolean atRightBorder = ball.getLayoutX() + deltaX >= (bounds.getMaxX()-ball.getRadius());
                final boolean atLeftBorder = ball.getLayoutX() + deltaX <= (bounds.getMinX()+ball.getRadius());
                final boolean atBottomBorder = ball.getLayoutY() + deltaY >= (bounds.getMaxY()-ball.getRadius());
                final boolean atTopBorder = ball.getLayoutY() + deltaY <= (bounds.getMinY()+ball.getRadius());
                if(atRightBorder || atLeftBorder)
                    deltaX *= -1;
                if(atBottomBorder ||atTopBorder)
                    deltaY *= -1;

                ball.setLayoutX(ball.getLayoutX() + deltaX);
                ball.setLayoutY(ball.getLayoutY() + deltaY);

                // not sure what this line does, so you will need to put it where it makes sense:
                ball.Collision(deltaX, deltaY);