title: JavaFX, problem with Platform.runLater(), delayed rendering of Canvas graphic
body: <p>I am trying to make a simple event driven TicTacToe game using JavaFX. Currently I am struggling with termination once certain conditions are met. To put it into more details, the player can click on the <code>GridPane</code> elements, which are <code>Canvas</code> Objects, and then they are filled with "X" or "O" shapes respectively (I am using <code>strokeLine</code> and <code>strokeOval</code> methods of <code>GraphicsContext</code>). Code below:</p>
  
  <pre><code>private static void placeX(Canvas square){
      GraphicsContext gc = square.getGraphicsContext2D();
      gc.setLineWidth(10.0f);
      gc.setStroke(Color.CORNFLOWERBLUE);
      gc.strokeLine(square.getWidth()*0.2, square.getHeight()*0.2, square.getWidth()*0.8, square.getHeight()*0.8);
      gc.strokeLine(square.getWidth()*0.8, square.getHeight()*0.2, square.getWidth()*0.2, square.getHeight()*0.8);
  }
  </code></pre>
  
  <p>Once 3 of the same shapes appear in line or diagonally the program should terminate. I am doing this using <code>Platform.exit()</code>. Code below: </p>
  
  <pre><code>class HandleGame implements EventHandler&lt;MouseEvent&gt;{
  
      @Override
      public void handle(MouseEvent e){
  
  
          Canvas can = (Canvas)e.getTarget();
          //function to check if the canvas is clear 
               placeX(can);  
  
           if(game.isEnded()){  //checks if the same shape appears three times
  
              Platform.runLater(new Runnable() {
                  @Override
                  public void run(){
                      try {
                          Thread.sleep(5000);
                      }
                      catch(InterruptedException exc){
                          System.out.println("Got something: " + exc.getMessage());
                      }
                      Platform.exit();
                  }
              });
          }
      } 
  }
  </code></pre>
  
  <p>This event handler is attached to every <code>Canvas</code> object in <code>GridPane</code> and triggers on mouse release. The problem I am having is that once the last <code>Canvas</code> is clicked, before the shape appears on the <code>Canvas</code> the specified <code>Runnable</code> is executed and the rendering is unnaturally delayed (the "X" shape appears only for a second before closing). Strangely enough 1 out of 10 runs it executes as expected. How can I make the rendering trigger before the <code>Thread.sleep()</code> and following <code>Platform.exit()</code>? Why on rare occasions the rendering is actually performed before <code>Thread.sleep()</code>? I did a little research but could not find anything decisive, I am newbie when it comes to JavaFx. Appreciate your help.</p>