title: javafx animation: displaying circles
body: <p>I want to display 5 randomly positioned and colored circles. It was easy part. Now I want to adjust this code to be an animation. This application should generate random circles endlessly but the condition is that it should keep only last five circles on the screen. This is where I got stuck. JavaFx provides ListChangeListener. I think it is what I should use. But how? 
  The following is my unfinished code:<br></p>
  
  <pre><code>import java.util.Random;
  
  import javafx.application.Application;
  import javafx.collections.ListChangeListener;
  import javafx.scene.Group;
  import javafx.scene.Scene;
  import javafx.scene.paint.Color;
  import javafx.scene.shape.Circle;
  import javafx.stage.Stage;
  
  public class RandomColorTest extends Application {
  
      int radius = 20;
      int sceneWidth = 300;
      int sceneHeight = 300;
  
      private void init(Stage primaryStage) {
          Group root = new Group();
          primaryStage.setResizable(false);
          primaryStage.setScene(new Scene(root, sceneWidth,sceneHeight));
  
          for (int i = root.getChildren().size(); i &lt; 5; i++) {
              root.getChildren().add(createCircle());
              // the following should convey the idea:
              // if the collection holds 5 elements then keep least recently generated element for 1 second and then delete it
              // add one new element 
              // if the collection holds 5 elements then keep least recently generated element for 1 second and then delete it
              // add one new element 
              // and so on
              root.getChildren().addListener(new ListChangeListener&lt;E&gt;() {
                  @Override
                  public void onChanged(
                          javafx.collections.ListChangeListener.Change&lt;? extends E&gt; arg0) {
                      // TODO Auto-generated method stub
                  }
              });
  
          }
      }
  
      // Create randomly positioned and colored circle
      private Circle createCircle() {
          final Circle circle = new Circle();
          circle.setRadius(radius);
  
          Random r = new Random();
          int rCol1 = r.nextInt(256);
          int rCol2 = r.nextInt(256);
          int rCol3 = r.nextInt(256);
          int rX = radius+r.nextInt(sceneWidth);
          if (rX&gt;sceneWidth-radius) {
              rX=rX-2*radius;
          }
          int rY = radius+r.nextInt(sceneHeight);
          if (rY&gt;sceneHeight-radius) {
              rY=rY-2*radius;
          }
          circle.setLayoutX(rX);
          circle.setLayoutY(rY);
  
          circle.setStroke(Color.BLACK);
          circle.setFill(Color.rgb(rCol1,rCol2,rCol3));
          System.out.println(rCol1+"-"+rCol2+"-"+rCol3+"-"+rX+"-"+rY);
          return circle;
      }
  
      @Override public void start(Stage primaryStage) throws Exception {
          init(primaryStage);
          primaryStage.show();
      }
      public static void main(String[] args) { launch(args); }
  }
  </code></pre>
  
  <p>After having managed to make ListChangeListener compile without errors it doesn't still work the way expected. Changes made to for loop: <br></p>
  
  <pre><code>for (int i = root.getChildren().size();;i++) {
              final ObservableList&lt;Node&gt; ol = root.getChildren();
              // the following should convey the idea:
              // if the collection holds 5 elements then keep least recently generated element for 1 second and then delete it
              // add one new element 
              // if the collection holds 5 elements then keep least recently generated element for 1 second and then delete it
              // add one new element 
              // and so on
              ol.add(createCircle());
              ol.addListener( new ListChangeListener&lt;Node&gt;(){
                  @Override
                  public void onChanged(
                      javafx.collections.ListChangeListener.Change&lt;? extends Node&gt; arg0) {
                      // TODO Auto-generated method stub
                      System.out.println("one new element added, size:"+ol.size());
                      if (ol.size()==5) {
                          ol.remove(0);
                      }
                  }   
              });
          } 
  </code></pre>
  
  <p>For loop is defined to loop infinitely (probably not the right way to solve this problem also) and I can see from console that circles are removed and added during the program run. Alas, I can't see GUI anymore. </p>