循环重绘
我正在使用 Java Swing 编写游戏。我想在每次循环执行时进行绘制,中间有一个小的延迟,以在屏幕上创建级联效果。我相信系统中的效率例程将对 repaint()
的调用折叠为单个调用。无论如何,在完全延迟之后,所有变化都会立即发生。有没有某种方法可以强制系统立即重新绘制,然后在循环的每次迭代上延迟?
我的代码:
for(int i=0;i<10;i++){
JButton[i].setBackground(Color.WHITE);
JButton[i].repaint();
Thread.sleep(2000);
JButton[i].setBackground(Color.GRAY);
JButton[i].repaint();
}
I am writing a game using Java Swing. I want to paint each time a loop executes with a small delay in between to create a cascade effect on screen. I believe that the efficiency routines in the system are collapsing the calls to repaint()
into a single call. At any rate, the changes all occur at once after the total delay. Is there some way to force the system to repaint immediately and then delay on each iteration of the loop?
My Code:
for(int i=0;i<10;i++){
JButton[i].setBackground(Color.WHITE);
JButton[i].repaint();
Thread.sleep(2000);
JButton[i].setBackground(Color.GRAY);
JButton[i].repaint();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
JComponent.paintImmediately
强制立即重绘。编辑:再次阅读您的问题后,我发现您可能正在事件分派线程上执行逻辑。这意味着重绘请求只有在您的方法返回后才会执行。如果您将代码放入另一个线程中,那么这可能会解决问题,并且比使用
paintImmediately
好得多。这有点混乱,但希望它能让您知道从哪里开始。
You can use
JComponent.paintImmediately
to force an immediate repaint.EDIT: After reading your question again it occurs to me that you might be executing your logic on the event dispatch thread. This would mean that the repaint requests will not be executed until after your method returns. If you put your code into another thread then that will probably fix the problem and it will be a lot nicer than using
paintImmediately
.It's a bit mucky but hopefully it gives you an idea of where to begin.
不确定我是否走在正确的轨道上,但这是否是一个线程问题?因为 sleep 将使当前线程进入睡眠状态。因此,如果重绘是在当前线程上,那么您将有效地对重绘进行排队,保持,然后对另一个重绘进行排队。然后该方法结束,图形线程的事件处理循环触发,执行两次重绘(折叠),使所有事情同时发生。
基于这种想法,我认为您可能有两个选择:
将代码放在第二个线程上,这样它就不会停止主图形线程,而且因为这意味着图形线程可以在睡眠发生的同时进行重新绘制.
在你睡觉之前(我认为不太推荐),触发事件循环来强制重绘发生。我得查一下这个。我一时想不起怎么做。 :-)
我可能会将#1 视为最佳选择。
Not sure if I'm on the right track here, but is this a thread issue? Because the sleep will be putting the current thread to sleep. So if the repaint is on the current thread, you will be effectively queuing a repaint, holding, then queuing another repaint. Then the method ends and the event processing loop of the graphics thread fires, executing two repaints (collapsed) making everything happen at once.
Based on that thought I think you might have two options:
Put the code on a second thread so that it doesn't halt the main graphics thread and also because it means that the graphics thread cando a repaint along side the sleep happening.
Before you sleep (not really recommended I would think), trigger the event loop to force the repaint to happen. I'd have to look this up. Cannot remember how to do it off the top of my head. :-)
I'd probably be looking at #1 as the best option.
我在 Swing 中做了一些动画,并且使用了计时器。有 java.util.Timer 和 javax.swing.Timer。看看两者,看看哪一个最能满足您的需求。
I have done some animation in Swing, and I use a Timer. There's java.util.Timer and javax.swing.Timer. Take a look at both and see which one best meets your needs.