title: How can I force a repaint() after every update() in a for loop in Java?
body: <p>I have written a small, basic, kaleidoscope type program that should gradually draw the same pattern (over time) at six different points and at different orientations.</p>
  
  <p>To do this I have created an array to store each pixel's colour (it's initial colour being black and represented by number 0) and then 6 starting points in the array have their colour changed to green (represented by number 1).  These points should appear on the screen and then, based on the previous 6 points' positions a further 6 points are created.  The updated screen should then be displayed.  Repeat, repeat, repeat...</p>
  
  <p>My problem is that all of the updates, for new pixels, are being carried out before painting the screen.  I have checked some other posts and web tutorials etc, and gather that AWT is kind enough to avoid wasting time repainting minor changes.  There seems also to be something called <code>paintManager</code> involved in this.  I believe the problem is that I am repainting within a for loop. I am finding this really frustrating as, in my view, it should be a simple thing to do.  Is there, indeed, a simple way to persuade java to plot these minor changes in the way I desire? </p>
  
  <p>I have included the code in its entirety below:</p>
  
  <pre><code>package paranoid;
  
  import javax.swing.JFrame;
  
  public class MasterFrame {
  
      public static void main(String[] args) {
              // TODO Auto-generated method stub
      new MasterFrame();
      }
  
  
      public MasterFrame(){
          JFrame f = new JFrame();
          f.setTitle("Kaleidoscope");
          f.add(new Trip2());
          f.setSize(500,300);
          f.setLocationRelativeTo(null);
          f.setVisible(true);
          f.setResizable(false);
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
      }
  
  }
  </code></pre>
  
  <p>and...</p>
  
  <pre><code>package paranoid;
  
  import java.awt.Color;
  import java.awt.Graphics;
  
  import javax.swing.JPanel;
  
  
  public class Trip2 extends JPanel {
  
      private static final long serialVersionUID = 1L;
  
      private int xmin = 0,
                  xmax = 499,
                  ymin = 0,
                  ymax = 279;
      private int x = 120;
      private int y = 80;
      private int dx = 1;
      private int dy = 1;
      private int temp = 0;
      private int update_counter = 0;
      private int repaint_counter = 0;
      private int x_pos[] = new int[6];
      private int y_pos[] = new int[6];
      private int screen[][] = new int[500][280];
  
  
      public Trip2() {
  
          initialisation();
          for(int i = 0; i &lt; 5000; i++)
          {
              update();
              System.out.println("Just returned from update()");
  
              repaint();                                          //This repaint is not being activated until all updates 
              System.out.println("Just returned from paint()");   //have been completed, but I want a repaint after EVERY update.
          }
      }
  
      public void initialisation(){
          System.out.println("initialising...");
          x_pos[0] = x;
          y_pos[0] = y;
  
          x_pos[1] = xmax - x;
          y_pos[1] = y;
  
          x_pos[2] = x;
          y_pos[2] = ymax - y;
  
          x_pos[3] = xmax - x;
          y_pos[3] = ymax - y;
  
          x_pos[4] = (int)(xmax/2)-50;
          y_pos[4] = (int)(ymax/2);
  
          x_pos[5] = (int)(xmax/2)+50;
          y_pos[5] = (int)(ymax/2);
  
          for(int j = 0; j&lt;280; j++){
              for(int i = 0; i&lt;500; i++){
                  screen[i][j] = 0;
              }
          }
      } //end of initialisation()
  
      public void update(){
          System.out.println("updating... for the "+update_counter+"th time");
  
          temp = (int)Math.floor(Math.random()*100);
          if(temp &lt; 40){ // 40% chance that the direction is changed
              dx = (int)Math.floor(Math.random()*3);
              dy = (int)Math.floor(Math.random()*3);
              dx = dx - 1;
              dy = dy - 1;
          }
  
  
          x_pos[0] = x_pos[0]+dx;
          y_pos[0] = y_pos[0]+dy;
  
          x_pos[1] = x_pos[1]-dx;
          y_pos[1] = y_pos[1]+dy;
  
          x_pos[2] = x_pos[2]+dx;
          y_pos[2] = y_pos[2]-dy;
  
          x_pos[3] = x_pos[3]-dx;
          y_pos[3] = y_pos[3]-dy;
  
          x_pos[4] = x_pos[4]-dy;
          y_pos[4] = y_pos[4]-dx;
  
          x_pos[5] = x_pos[5]+dy;
          y_pos[5] = y_pos[5]+dx;
  
          for(int k = 0; k &lt; 6; k++){
              if(x_pos[k] &lt; 0){
                  x_pos[k] = 0;
              }
              if(x_pos[k] &gt; 499){
                  x_pos[k] = 499;
              }
          }
  
          for(int k = 0; k &lt; 6; k++){
              if(y_pos[k] &lt; 0){
                  y_pos[k] = 0;
              }
              if(y_pos[k] &gt; 279){
                  y_pos[k] = 279;
              }
          }
  
  
          screen[x_pos[0]][y_pos[0]] = 1;
          screen[x_pos[1]][y_pos[1]] = 1;
          screen[x_pos[2]][y_pos[2]] = 1;
          screen[x_pos[3]][y_pos[3]] = 1;
          screen[x_pos[4]][y_pos[4]] = 1;
          screen[x_pos[5]][y_pos[5]] = 1;
  
          update_counter = update_counter + 1;
  
      } //end of update()
  
      public void paint(Graphics g){
          System.out.println("painting screen for the "+repaint_counter+"th time");
  
              g.setColor(Color.BLACK);
              g.fillRect(xmin, ymin, xmax, ymax);
  
              for(int j = 0; j&lt;280; j++){
                  for(int i = 0; i&lt;500; i++){
                      if(screen[i][j] == 0){
                          g.setColor(Color.BLACK);
                      } else 
                      {   
                          g.setColor(Color.GREEN);    
                      }
                      g.drawLine(i,j,i,j); //plots pixel
                  }
              }
  
  
              try{
                  Thread.sleep(100);
              }
              catch(InterruptedException e){  
              }
  
              repaint_counter = repaint_counter + 1;
  
  
      }//end of paint(Graphics g)
  
  }//end of Trip2 class
  </code></pre>