0

I am showing a gridpane to user and I am updating it for couple of times in a for loop. What I need to do is, before each update, I need to create a second of delay in order to let the user see the stage for a second.

VisualizationThread visualizationThread = new VisualizationThread(matrix, bestIndividual, centerX, centerY);
for(int i=0; i<bestIndividual.size(); i++){
  switch (bestIndividual.get(i)){
    case 1:
        visualizationThread.setCurrentX(visualizationThread.getCurrentX()-1);
        break;
    case 2:
        visualizationThread.setCurrentY(visualizationThread.getCurrentY()+1);
        break;
    case 3:
        visualizationThread.setCurrentX(visualizationThread.getCurrentX()+1);
        break;
    case 4:
        visualizationThread.setCurrentY(visualizationThread.getCurrentY()-1);
        break;
  }

  visualizationThread.run();
  try {
      Thread.sleep(1000);
  }catch (Exception e){
      e.printStackTrace();
  }

}

VisualizationThread Class:

public class VisualizationThread implements Runnable{

    private ArrayList<ArrayList<Integer>> matrix;
    private ArrayList<Integer> solution;
    private int currentX;
    private int currentY;
    private Stage primaryStage;

    public VisualizationThread(ArrayList<ArrayList<Integer>> matrix, ArrayList<Integer> solution, int currentX, int currentY){
        this.matrix   = matrix;
        this.solution = solution;
        this.currentX = currentX;
        this.currentY = currentY;
        this.primaryStage = new Stage();
    }

    public void run() {

        if(primaryStage.isShowing()){
            primaryStage.close();
        }

        BorderPane root = new BorderPane();

        GridPane grid = new GridPane();
        grid.setPadding(new Insets(10));
        grid.setHgap(10);
        grid.setVgap(10);

        StackPane[][] screen_buttons = new StackPane[matrix.size()][matrix.size()];

        for (int y=0;y<matrix.size();y++) {
            for (int x=0;x<matrix.get(y).size();x++) {
                screen_buttons[y][x] = new StackPane();
                Rectangle rec = new Rectangle(30,30);
                if(x == currentX && y == currentY){
                    rec.setFill(Color.GREEN);
                }else {
                    rec.setFill(matrix.get(x).get(y) == 0 ? Color.YELLOW : Color.RED);
                }
                rec.setStyle("-fx-arc-height: 10; -fx-arc-width: 10;");
                screen_buttons[y][x].getChildren().addAll(rec);
                grid.add(screen_buttons[y][x], x, y);
            }
        }

        //container for controls
        GridPane controls = new GridPane();

        root.setCenter(grid);
        root.setBottom(controls);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public int getCurrentX() {
        return currentX;
    }

    public void setCurrentX(int currentX) {
        this.currentX = currentX;
    }

    public int getCurrentY() {
        return currentY;
    }

    public void setCurrentY(int currentY) {
        this.currentY = currentY;
    }
}

My problem is, the stage looks empty until all the iterations are done. But I want to show each iterations for a while and I tried to implement this with using Thread.sleep(). Obviously it did not worked. Why is that? How can I achieve this? Best Regards,

JollyRoger
  • 737
  • 1
  • 12
  • 38
  • It is hard to answer without [mcve]. You may want to use [Task](https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm) – c0der Mar 27 '19 at 14:47
  • @c0der Could you pleaae tell me what is missing or unclear in the question? – JollyRoger Mar 27 '19 at 15:06
  • 2
    An mcve is missing. Please follow the link. If it is not clear, I'll be happy to try and clarify. – c0der Mar 27 '19 at 15:58
  • @c0der Thank you for your answer. What I want to do is simply just creating delay after the line primaryStage.show(). – JollyRoger Mar 27 '19 at 16:47
  • 2
    Possible duplicate of [JavaFX periodic background task](https://stackoverflow.com/questions/9966136/javafx-periodic-background-task) – Slaw Mar 27 '19 at 17:21
  • @Slaw That helped me a lot. Thank you! – JollyRoger Mar 27 '19 at 17:40

0 Answers0