title: Combining repaint() with Thread.Sleep()
body: <p>What im trying to do is pretty simple, I want to show the steps of an algorithm on the screen, hence why im trying to combine repaint() with sleep(), but I am doing it wrong, Id love it if someone knows enough about it to firstly explain whats wrong with this code, and secondly, what do i do to make it work...</p>
  
  <p>thanks!</p>
  
  <p>in summery, what this code was meant to do is paint 10 red vertices, then balcken em one by one in intervals of 200 milliseconds.</p>
  
  <p>here's the code:</p>
  
  <pre><code>public class Tester {
  
         public static void main(String[] args) {
              SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                      ShowGUIGraph();
                  }
              });
          }
  
          private static void ShowGUIGraph() {
              JFrame f = new JFrame("something");
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              JPanel p=new JPanel();
              p.setLayout(new BorderLayout());
              p.add(BorderLayout.CENTER,new SomePanel());
              f.add(p);
              f.setPreferredSize(new Dimension(800,600));
              f.pack();
              f.setVisible(true);
          }
  }
  
  public class SomePanel extends JPanel {
      private static final long serialVersionUID = 1L;
      LinkedList&lt;Vertex&gt; vertices=new LinkedList&lt;Vertex&gt;();
      public SomePanel () {
          for (int i=0;i&lt;10;i++) {
              Vertex v=new Vertex(i);
              v.setLocation(20+30*i, 20+30*i);
              vertices.add(v);
          }
          traverseVerticesRecoursive(0);
          traverseVerticesNonRecoursive();
      }
      public void traverseVerticesRecoursive(int i) {
          if (i&gt;=vertices.size()) return;
          vertices.get(i).setColor(Color.black);
  
          try {
              Thread.sleep(200);
          } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          repaint();
          traverseVerticesRecoursive(i+1);
      }
      public void traverseVerticesNonRecoursive() {
          for (int i=0;i&lt;10;i++) {
              vertices.get(i).setColor(Color.red);
              try {
                  Thread.sleep(200);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              repaint();
          }
      }
      protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          for (int i=0;i&lt;vertices.size();i++) {
              vertices.get(i).paintVertex(g);
          }
  
      }
  }
     public class Vertex {
          private int x,y,tag,r=20;
          private Color color=Color.red;
          Vertex (int i) {
              tag=i;
          }
          public void setLocation(int x0,int y0) {
              x=x0;
              y=y0;
          }
          public int getX() {
              return x;
          }
          public int getY() {
              return y;
          }
  
          public void setColor(Color c) {
              color=c;
          }
          public boolean colorIs(Color c) {
              return (color.equals(c));
          }
  
          public void paintVertex(Graphics g) {
              g.setColor(color);
              g.fillOval(x,y,r,r);
              g.setColor(Color.BLACK);
              g.drawOval(x,y,r,r);
              g.drawString(""+tag, x+r/2, y+r/2+4);
          }
          public int getR() {
              return r;
          }
      }
  </code></pre>