Why does clearRect() method not clear an area on the canvas?
<p>I wrote a Java applet that prints a text on the canvas at the same position after getting updated in every iteration of a loop. The text value changes and the output is correct but the <strong>clearRect()</strong> method I call in the loop to clear the area of the text before printing out each time doesn't work and the text gets overwritten. I can see the output up to 2-3 iterations but then it's not readable. The applet actually performs a countdown and displays the time in <strong>HH : MM : SS</strong> format. Below is my code. Please predict the cause of this and/or correct it:  </p>

<pre><code>/* Only the section of the code I'm having problems in is included */

while (t&gt;=0) {
    //int t stores total time in seconds
    int h=(t/3600);
    int m=((t%3600)/60);
    int s=((t%3600)%60);
    str=h+" : "+m+" : "+s;
    /* String str holds data to be displayed, i.e., time in HH : MM : SS format */
    g.clearRect(20,200,150,30);
    g.drawString(str,20,200);
    try{
        Thread.sleep(1000); //elapses 1 second
    }
    catch(Exception e) {}
    t=t-1;
}
</code></pre>