문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top