title: JPanel Graphics clearing and repainting?
body: <p>I have a <code>JPanel</code> with a <code>paintComponent()</code> function. I'll call it once, then when the user clicks a different <code>JButton</code>, I'll set some flag and want to call this function again as it will do something slightly different after the flag is set. </p>
  
  <p>So here's what I'm wondering: how do I clear the existing stuff from <code>paintComponent</code>? And to redraw, do I just call <code>paintComponent</code> again? </p>
  
  <p>Currently I'm trying the following: </p>
  
  <pre><code>flag2 = true;
  repaint(); //I expect (want) paintComponent to be called again
  </code></pre>
  
  <p>In paint component, I do stuff like:</p>
  
  <pre><code>if (flag2==true) {
      g.drawRect(...);
  } else {
      g.drawLine(...);
  }
  </code></pre>
  
  <p>But through testing it seems like there is something wrong with what I'm doing. </p>
  
  <p>Thanks for any help.</p>