title: Generating images pixel by pixel in JavaFX with progress visible
body: <p>I'm writing an JavaFX application which generates abstract pattern images pixel by pixel. The result should look <a href="https://i.stack.imgur.com/u4bjX.png" rel="nofollow noreferrer">somewhat like this</a>. 
  Here is my main class:</p>
  
  <pre><code>package application;
  
  public class Main extends Application {
  
    private static final int WIDTH = 800; 
    private static final int HEIGHT = 600; 
  
      @Override
      public void start(Stage primaryStage) {
          BorderPane root = new BorderPane();
          Scene scene = new Scene(root, WIDTH, HEIGHT);
          scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
          primaryStage.setScene(scene);
          primaryStage.show();
  
      final Canvas canvas = new Canvas(WIDTH, HEIGHT);
  
          root.getChildren().add(canvas);
  
          final GraphicsContext gc = canvas.getGraphicsContext2D();
          final PixelWriter pw = gc.getPixelWriter();
  
          final PixelGenerator generator = new PixelGenerator(WIDTH, HEIGHT, pw);
          final Thread th = new Thread(generator);
          th.setDaemon(true);
          th.start();
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  
  <p>The PixelGenerator class is generating new pixels one by one, filling the canvas using the PixelWriter.setColor() method. It calculates the color for the new pixels based on some random numbers and on previously generated colors.</p>
  
  <p>If I run the PixelGenerator on the application thread, the GUI is blocked until the whole available space is filled and only then I see the complete picture. </p>
  
  <p>To avoid this I made my PixelGenerator class extend the javafx.concurrent.Task and its call() method generates all the pixels at once. Sometimes it works as expected and I can see how the image gets generated step by step, but sometimes the picture <a href="https://i.stack.imgur.com/gFZgV.png" rel="nofollow noreferrer">remains unfinished</a>, as if the task would not run to the end. Debugging shows that it always runs to the end, but the later PixelWriter.setColor() calls have no effect.</p>
  
  <p>I tried different approaches to fix it. E.g. I added an onSucceeded event handler, and tried to cause the Canvas to "refresh" since I thought that it just somehow 'skips' its last refresh iteration. No success. Strange, even coloring further pixels has no effect inside of the listener. </p>
  
  <p>I also tried using an AnimationTimer instead of utilizing the Task. It works, but my problem is that I can't predict how much pixels I am able to generate between its handle() calls. The generation algorithm complexity and though the CPU time needed for the pixel generation will change as the algorithm will evolve.</p>
  
  <p><strong>My ideal target would be to spend all available CPU time generating pixels, but at the same time to be able to see the generation progress in detail (60 or even 30 FPS would be OK).</strong></p>
  
  <p>Please help me out, what I'm doing wrong and what direction should I go given my target?</p>