Question

Im trying to slowly paint a rectangle using two calls of .fillrect method with Thread.sleep call between each method. What is happening is that the sleep method is getting called before the rectangle is initialised, so it appears that the rectange has already been painted. I just want to paint part of the rectange, pause for five seconds and then paint antother part.

Here is my code -

public void paint(Graphics g, int w, int h) {
    g.drawRect(0, 0, w - 1, h - 1);
    g.fillRect(0, 0, 10, h-1);

   try {
    Thread.sleep(5000);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

    g.fillRect(0, 0, 50, h-1);
 }

Thanks

Was it helpful?

Solution

It is always a very BAD idea to cause an event thread to block, no matter what the platform.

What you should be doing is defining variables somewhere that store the current extent of the area you want painted. Update those variables on a separate thread (you can block that thread all you want) and call the repaint() method to schedule a repaint whenever you update the variables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top