开发者

Filling a rectangle - paint method (MIDP)

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 s开发者_开发百科econds 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜