简体   繁体   English

JavaFX设置imageview后等待

[英]JavaFX wait after setting imageview

I need to wait after setting an Imageview, but if I use 设置Imageview后,我需要等待,但是如果使用

Thread.sleep(1000)

after setting the imageView the whole application sleeps for one second but the imageView will not be set during this period of time. 设置imageView后,整个应用程序将休眠一秒钟,但在此期间将不会设置imageView。

I would like to know how can I pause the whole application after setting the imageview. 我想知道在设置imageview后如何暂停整个应用程序。

For instance, it's a game of cards (briscola), and after the computer played a card, or I played a card the application should pause to show which cards are on the plate and who won the hand. 例如,这是一场纸牌游戏(briscola),在计算机玩完一张纸牌后,或者我玩了一张纸牌后,应用程序应暂停以显示哪些牌在盘子上以及谁赢得了手。

Thanks in advance. 提前致谢。

UPDATE: This is what I tried with the tasks, with no success as the images 更新:这是我尝试执行的任务,但没有像图像那样成功

    private void setImages(BriscolaModel model) {
    Set<Map.Entry<Player, BriscolaCard>> entries = model.getPlate().entrySet();


    // set images

    Task<Integer> task = new Task<Integer>() {
        @Override protected Integer call() throws Exception {
            for(Map.Entry<Player, BriscolaCard> entry : entries){
                plate.getImageViews().add(new ImageView(new Image(getClass().getClassLoader().getResourceAsStream(entry.getValue().getRelativePath()),200,100,true, false)));
            }
            plate.getCards().getChildren().setAll(plate.getImageViews());
            return 0;
        }
    };

    Thread th = new Thread(task);
    th.start();

    // clear images 

    Task <Integer> clearTask = new Task<Integer>() {
        @Override
        protected Integer call() throws Exception {
            clearImages();
            return 0;
        }
    };
    Thread clearThread = new Thread(clearTask);
    if(entries.size()==2){
        Platform.runLater(clearTask);
    }else {
        clearThread.start();
    }
}

2nd UPDATE: 第二次更新:

This seem to work better (i randomly see the images for some milliseconds before being cleared), but how can I set the minimum time to wait before the Platform.runLater() ? 这似乎工作得更好(在清除之前,我会随机看到图像几毫秒),但是如何设置在Platform.runLater()之前等待的最短时间?

private void setImages(BriscolaModel model) {
    Set<Map.Entry<Player, BriscolaCard>> entries = model.getPlate().entrySet();

    for (Map.Entry<Player, BriscolaCard> entry : entries) {
        plate.getImageViews().add(new ImageView(new Image(getClass().getClassLoader().getResourceAsStream(entry.getValue().getRelativePath()), 200, 100, true, false)));
    }
    plate.getCards().getChildren().setAll(plate.getImageViews());

    Task<Void> waitTask = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            clearImages();
            return null;
        }
    };

    if(entries.size() == 2){
        Platform.runLater(waitTask);
    }

}

I found the solution, thanks to Rahul Singh for the suggestions! 感谢Rahul Singh的建议,我找到了解决方案!

private void setImages(BriscolaModel model) {
    Set<Map.Entry<Player, BriscolaCard>> entries = model.getPlate().entrySet();
    plate.getImageViews().clear();

    for (Map.Entry<Player, BriscolaCard> entry : entries) {
        plate.getImageViews().add(new ImageView(new Image(getClass().getClassLoader().getResourceAsStream(entry.getValue().getRelativePath()), 200, 100, true, false)));
    }
    plate.getCards().getChildren().setAll(plate.getImageViews());

    Task<Void> waitTask = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            Timeline timeline = new Timeline(new KeyFrame(
                    Duration.millis(2000),
                    ae -> clearImages()));
            timeline.play();
            return null;
        }
    };

    if(entries.size() == 2){
        Platform.runLater(waitTask);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM