title: How does paintComponent work?
body: <p>This might be a very noob question. I'm just starting to learn Java</p>
  
  <p>I don't understand the operation of paintComponent method. I know if I want to draw something, I must override the paintComponent method.</p>
  
  <pre><code>public void paintComponent(Graphics g)
  {
     ...
  }
  </code></pre>
  
  <p>But when is it called? I never see anything like "object.paintComponent(g)" but still it is drawn when the program is running. </p>
  
  <p>And what is the Graphics parameter? Where is it from? Parameter must be supplied when the method is called. But as I said before, it seems like this method is never be explicitly called. So who provides this parameter? And why do we have to cast it to Graphics2D?</p>
  
  <pre><code>public void paintComponent(Graphics g)
  {
      ...
      Graphics2D g2= (Graphics2D) g;
      ...
  }
  </code></pre>