为何在尝试获取窗口大小时对象为何不画圆

时间:2019-01-09 05:55:18

标签: javafx javafx-8

所以,我在一个班级中创建了一个窗格

@Override //Overridden nerd
public void start(Stage primaryStage) {

    //makes a ball
    Ball ball = new Ball();

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

    //increase/decrease speed
    ball.setOnKeyPressed(e-> {
    if(e.getCode() == KeyCode.UP) {
        ball.increaseSpeed();
    }
    else if (e.getCode() == KeyCode.DOWN) {
        ball.decreaseSpeed();
        }
    });

    //creating a scene and setting its background
    Scene scene = new Scene(ball, 500, 500);
    primaryStage.setTitle("Random Walker");
    primaryStage.setScene(scene);
    primaryStage.show();

    ball.requestFocus();
    }

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

我想让动画从窗格的中心开始,然后从墙壁弹起(我还没在弹跳部分,所以我知道那不是完美的,但是我正在尝试)

我的问题是,当我尝试使球从窗口的当前大小开始绘制时,它根本不会绘制任何内容,但是如果我手动设置默认屏幕大小,它将起作用。任何见解都会很棒,因为一段时间以来我一直在努力寻找任何方法来完成此任务,谢谢。

public class setup extends Application {

    @Override //Overridden nerd
    public void start(Stage primaryStage) {

    //makes a ball
    Ball ball = new Ball();

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

    //increase/decrease speed
    ball.setOnKeyPressed(e-> {
    if(e.getCode() == KeyCode.UP) {
        ball.increaseSpeed();
    }
    else if (e.getCode() == KeyCode.DOWN) {
        ball.decreaseSpeed();
        }
    });

    //creating a scene and setting its background
    Scene scene = new Scene(ball, 500, 500);
    primaryStage.setTitle("Random Walker");
    primaryStage.setScene(scene);
    primaryStage.show();

    ball.requestFocus();
    }

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

0 个答案:

没有答案