如何使用JavaFX将图像添加到数组?

时间:2018-03-24 14:04:01

标签: java javafx

我想将图像添加到数组中。当我使用Node它工作,但当我使用相同的方式添加图像时,它运行时不会出现。这是我的代码:

public class PlatForm extends Application {
    List<ImageView> platFormList = new ArrayList<>();
    private Pane layout;
    private Scene scene;
    private AnimationTimer platFormTimer;

    @Override
    public void start(Stage primaryStage) throws Exception {
        layout = new Pane();
        scene = new Scene(layout,800,500);
        primaryStage.setScene(scene);
        primaryStage.show();

        platFormTimer = new AnimationTimer() {
            @Override
            public void handle(long now) {
                uploadPlatForm();
            }
        };
        platFormTimer.start();
    }

    public void uploadPlatForm(){
        for(ImageView platform : platFormList)
            platform.setTranslateX(platform.getTranslateX() + Math.random() * 10);
        if (Math.random() < 0.075) {
            platFormList.add(newPlatForm());
        }
    }

    private ImageView newPlatForm(){
        ImageView platFormImg =
            new ImageView(
                new Image(getClass().getResourceAsStream("/image/platform.png")
            )
        );
        layout.getChildren().add(platFormImg);
        platFormImg.setTranslateY((int)(Math.random() * 14) * 40);
        return platFormImg;
    }
}

0 个答案:

没有答案