title: What's the difference between Graphics.clearRect and Graphics.drawRect?
body: <p>I found a rendering bug in some code and have found a workaround, but I would like to know why I am getting different behaviour. In the old code, the background would (sometimes) be rendered as white, despite while debugging <code>getBackground()</code> would return the correct colour.</p>
  
  <p>Old code:</p>
  
  <pre><code>@Override
  public void paint(Graphics g) {
    // Stuff
  
    g.setColor(getBackground());
    g.clearRect(0, 0, width, height); // Obviously wrong.
  
    // More stuff
  }
  </code></pre>
  
  <p>New code:</p>
  
  <pre><code>@Override
  public void paint(Graphics g) {
    // Stuff
  
    g.setColor(getBackground());
    g.drawRect(0, 0, width, height); // Correct usage with 'setColor' call.
  
    // More stuff
  }
  </code></pre>
  
  <p>As I put in the code, it is obvious that <code>setColor(getBackground())</code> has no effect on the <code>clearRect(...)</code> call. Yet I would assume that calling <code>clearRect(...)</code> and calling <code>setColor(getBackground())</code> followed by <code>drawRect(...)</code> would be semantically the same.</p>
  
  <p>I have also considered the opaqueness property, but the parent lightweight components and ancestor heavyweight component all use the same background colour, and it is quite obvious that this component is the one with the incorrect behaviour (it is one of 8 of the same type of component owned by its parent - yet only the ones that get to this section of code have a problem).</p>
  
  <p>I am using JDK 1.6.0_07 (for business reasons of course) if that helps.</p>