title: Draw circle and rectangle in java
body: <p>I am new to java and I want to draw circle and rectangle by using java code. I did write code for that purpose and tried at my own. But on Panel is appearing and shapes are not appearing. </p>
  
  <p>Code of "MyPanel" is given below </p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  public class MyPanel extends JPanel{
      public void painComponent(Graphics g){  
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D)g;
          g2.drawRect(20,20,20,20);
          g2.setColor(Color.blue);
          g2.fillOval(50,20,20,20);
          g2.drawString("Hello World", 120, 50);
      }//end painComponent 
  }//end test class
  </code></pre>
  
  <p>Cdoe of driver class "Test" is given below.</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  public class Test{
      JFrame f; 
      MyPanel p; 
      public Test(){
          f = new JFrame();
          Container c = f.getContentPane();
          c.setLayout(new BorderLayout()); 
          p = new MyPanel(); 
          c.add(p, BorderLayout.CENTER);
          f.setSize(400,400);
          f.setVisible(true);
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }//end of constructor
      public static void main(String args[]){
          Test t = new Test();
      }
  }
  </code></pre>
  
  <p>And as per my knowledge when frame becomes visible through the <code>paintChildren()</code> method then panel should become visible 
  Also to become visible panel will call <code>paintComponent()</code> method which will  do your custom drawing, but it seems like panel is not calling <code>paintComponent()</code>.</p>