循环重绘

发布于 2024-09-30 16:57:25 字数 401 浏览 6 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只涨不跌 2024-10-07 16:57:25

您可以使用JComponent.paintImmediately强制立即重绘。

编辑:再次阅读您的问题后,我发现您可能正在事件分派线程上执行逻辑。这意味着重绘请求只有在您的方法返回后才会执行。如果您将代码放入另一个线程中,那么这可能会解决问题,并且比使用 paintImmediately 好得多。

void uiFunction() {
    new Thread() {
        public void run() {
            for(int i = 0; i < 10; i++) {
                final JButton b = buttons[i];
                SwingUtilities.invokeLater(new Runnable() {
                    b.setBackground(Color.WHITE);
                    b.repaint();
                }
                Thread.sleep(2000);
                SwingUtilities.invokeLater(new Runnable() {
                    b.setBackground(Color.GRAY);
                    b.repaint();
                }
            }
        }
    }.run();
}

这有点混乱,但希望它能让您知道从哪里开始。

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.

void uiFunction() {
    new Thread() {
        public void run() {
            for(int i = 0; i < 10; i++) {
                final JButton b = buttons[i];
                SwingUtilities.invokeLater(new Runnable() {
                    b.setBackground(Color.WHITE);
                    b.repaint();
                }
                Thread.sleep(2000);
                SwingUtilities.invokeLater(new Runnable() {
                    b.setBackground(Color.GRAY);
                    b.repaint();
                }
            }
        }
    }.run();
}

It's a bit mucky but hopefully it gives you an idea of where to begin.

不一样的天空 2024-10-07 16:57:25

不确定我是否走在正确的轨道上,但这是否是一个线程问题?因为 sleep 将使当前线程进入睡眠状态。因此,如果重绘是在当前线程上,那么您将有效地对重绘进行排队,保持,然后对另一个重绘进行排队。然后该方法结束,图形线程的事件处理循环触发,执行两次重绘(折叠),使所有事情同时发生。

基于这种想法,我认为您可能有两个选择:

  1. 将代码放在第二个线程上,这样它就不会停止主图形线程,而且因为这意味着图形线程可以在睡眠发生的同时进行重新绘制.

  2. 在你睡觉之前(我认为不太推荐),触发事件循环来强制重绘发生。我得查一下这个。我一时想不起怎么做。 :-)

我可能会将#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:

  1. 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.

  2. 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.

〆凄凉。 2024-10-07 16:57:25

我在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文