开发者

Breakdown of Threads used by a JApplet

I've been spending a great deal of time trying to und开发者_如何学Pythonerstand this. I created a JApplet that used Thread.sleep() in a loop to animate the applet. But when I tried to run the Applet the screen stayed blank. When I draw the animations in a separate thread and call repaint, the applet works perfectly. I've seen many explanations of why this happens but none have been very comprehensive.

What thread calls the paint method? And why can't that thread partially draw on the canvas, pause, then continue drawing? I've noticed that some Thread.sleep() calls work as expected, particularly when they are not in a loop.


UI needs to be repainted and this happens on UI thread.

Therefore you should not seize control of the UI thread - it seems that you are doing just this (animation loop with .sleep()).

You should return control of the UI thread (exit the function that you are in) so that it can be repainted.

Take a look at Swing Painting and SwingWorker to understand how to run a background thread to do some work and then update UI on UI thread.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜