title: Display an image using EventDispatchThread vs without
body: <p>So I'm trying to display an image(<code>ball</code>) which I'll eventually control with user input. For know, the image just gets displayed over intervals using thread's sleep method. </p>
  
  <p>I've made 2 classes, one that extends <code>JPanel</code> and the other extends <code>JFrame</code>.
  The <code>JPanel</code> subclass looks like this:</p>
  
  <pre><code> public class BallPanel extends JPanel {
      private Image ball;
      private int x,y;
      public BallPanel(){
          try {
              ball=ImageIO.read(new File("C:\\Users\\Owner\\Desktop\\ball.png"));
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          x=10;
          y=10;
          Thread thread = new Thread() {
              @Override
              public void run(){
                      loop();
          }
        };
          thread.start();
      }
      public void paintComponent(Graphics g){
          super.paintComponent(g);
          g.drawImage(ball,x,y,null);
      }
      public void loop(){
          while(true){
              repaint();
              try {
                  Thread.sleep(10);
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>In the loop method I call the <code>sleep</code> method to allow <code>repaint</code> to be called over intervals. Then, <code>loop()</code> is called in the constructor.</p>
  
  <p>The <code>JFrame</code> subclass looks like this:</p>
  
  <pre><code>    public class BallFrame extends JFrame {
  
      public BallFrame(){
          setVisible(true);
          setSize(800,800);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          setContentPane(new BallPanel());
      }
      public static void main(String args[]){
          //SwingUtilities.invokeLater(new Runnable() {
               //   @Override
                //  public void run() {
                      new BallFrame();
                 // }
      //});
  
  }
  }
  </code></pre>
  
  <p>Now the interesting, or perhaps confusing thing, is that when I run the code as it is shown here, with the anonymous inner class commented out, the ball doesn't always appear. Sometimes I need to re-size the frame (i.e call <code>repaint</code>) before the ball is shown. However, when I call it through the even dispatch thread using the anonymous inner class the ball appears every time I run the code. What is the reason for this?</p>