2017-05-12 6 views
0

を設定した後、私はImageViewのを設定した後に待機する必要がありますが、私はImageViewのにアプリケーション全体を設定した後JavaFXのは、ImageViewの

Thread.sleep(1000) 

を使用する場合、1秒間スリープしますがImageViewのは、この期間中に設定されることはありません待ちます時間。

イメージビューを設定した後、アプリケーション全体を一時停止する方法を知りたいと思います。

例えば、それはカードゲーム(briscola)であり、コンピュータがカードをプレイした後、またはカードをプレイした後、アプリケーションが一時停止して、どのカードがプレート上にあり、誰が勝ったかを示します。

ありがとうございます。

UPDATE: これは、私が仕事をしようとしたものですがない画像などの成功

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(); 
    } 
} 

第二UPDATEで:

これは、より良い仕事するように見える(私はランダムに数ミリ秒のための画像を見ますクリアする前に)、どうすれば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); 
    } 

} 
+0

あなたのアプリケーションでタスクを使用する必要があります –

+0

タスクを使用してイメージを設定するか、待機しますか? –

+0

イメージを設定してからplatform.runlaterを使用して、アプリケーションのUIがフリーズしないようにします。 –

答えて

0

私は解決策を見つけました.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); 
    } 

} 
+0

'Timeline'を使用している場合、' Task'は必要ありません。 'call()'メソッドにあるコードを 'if(entries == 2)'ブロックに入れてください。 –

関連する問題