title: Using paintComponent() to draw rectangle in JFrame
body: <p>I'm trying to create a program that draws shapes (a rectangle on the example below) using JPanel's paintComponent(), but I can't get it to work and can't spot what is wrong.</p>
  
  <p>The code is as follows:</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  
  public class RandomRec{
      JFrame frame;
  
      public void go(){
          frame = new JFrame();
          frame.setSize(500,500);
          frame.setVisible(true);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          DrawPanel panel = new DrawPanel();
      }
  
      public static void main (String[] args){
          class DrawPanel extends JPanel{
             public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.orange);
                g.drawRect(20, 20, 100, 60);
             }
          }
  
          RandomRec test = new RandomRec();
          test.go();
      }
  }
  </code></pre>
  
  <p>Any help on this would be much appreciated.</p>
  
  <p>Thank you.</p>
  
  <p>*<strong><em>UPDATE</em>*</strong> Problem solved! Moving the go() method out of the main method, adding a frame.add(panel) and moving the frame.setVisible(true) to the bottom of the go() method (more specifically, move it after the panel is added to the frame) has sorted the issue. Thank you. </p>