简体   繁体   English

使用Java与计时器交替显示图像

[英]Alternating images with a timer using java

Since I'm not a CS major, I'm having some difficulties translating my programming wishes into an actual program. 由于我不是CS专业的学生,​​因此在将编程愿望转换为实际程序时遇到了一些困难。 What it basically boils down to is the following: how can I alternate an image on a label, showing each image for an amount of tim specific for each image. 基本上可以归结为以下几点:如何在标签上交替显示图像,以针对每个图像的特定时间显示每个图像。

So: say I've images A and B; 所以:说我有图像A和B; I'd like the user to see A for 1000ms and B for 200ms. 我希望用户看到A持续1000ms,B持续200ms。 This keeps on looping until a user presses a certain key. 这一直循环播放,直到用户按下某个键为止。

Now, I'm able to load an image onto a panel, quite easily even, and I've managed to catch user input using KeyListener and stuff, which all works quite nicely and alot easier then I had expected. 现在,我什至可以很容易地将图像加载到面板上,并且我设法使用KeyListener和东西来捕获用户输入,所有这些都很好地工作,并且比我预期的要容易得多。 I also know how to use looping constructs like while, for and do..while, but this timer business is shady. 我也知道如何使用while,for和do..while之类的循环结构,但是此计时器业务很差劲。

I see all kinds of stuff using threads and what not, I really don't need that. 我看到各种使用线程的东西,什么也不用,我真的不需要。 This is not about efficient programming or good code, it's simply about demonstrating something. 这不是关于有效的编程或好的代码,而是关于演示的东西。 Any help would be greatly appreciated! 任何帮助将不胜感激!

Here's something that might be a good example: http://www.java2s.com/Code/Java/Development-Class/UsejavautilTimertoscheduleatasktoexecuteonce5secondshavepassed.htm 这可能是一个很好的例子: http : //www.java2s.com/Code/Java/Development-Class/UsejavautilTimertoscheduleatasktoexecuteonce5secondshavepassed.htm

I can try to explain the code if it appears confusing 如果看起来令人困惑,我可以尝试解释代码

There is nothing necessarily inefficient about using threads when threads are the right tool for the job. 当线程是完成任务的正确工具时,使用线程并不一定会效率低下。

In this case, it would not be unreasonable to create a new class that implements Runnable , which holds a reference to the label you wish to change the image on. 在这种情况下,创建实现Runnable的新类并不是没有道理的,该类包含对要更改图像的标签的引用。

This means that the image could be changed without causing waits on the main application that would cause it to hang until it was done. 这意味着可以更改映像,而不会导致等待主应用程序的等待,直到挂起它为止。

You would want to avoid 'Busy Loops' [basically, a while loop with no Thread.sleep() within it], and look to see if there is any needed thread exit criteria 您可能想避免“忙循环”(基本上是一个while循环,其中没有Thread.sleep()),并查看是否有任何所需的线程退出条件

Use a SwingWorker<Void, Void> . 使用SwingWorker<Void, Void> The doInBackground method of the SwingWorker should look like this : SwingWorker的doInBackground方法应如下所示:

@Override
protected Void doInBackground() {
    try {
        while (true) {
            displayImage(imageA);
            Thread.sleep(1000L);
            if (isCancelled()) {
                return null;
            }
            displayImage(imageB);
            Thread.sleep(200L);
            if (isCancelled()) {
                return null;
            }
        }
    }
    catch (InterruptedException e) {
        // ignore
    }
    return null;
}

private void displayImage(final Icon image) {
    SwingUtilituies.invokeLater(new Runnable() {
        @Override
        public void run() {
            // display the image in the panel
        }
    });
}

The keylistener should simply cancel the SwingWorker. 关键侦听器应仅取消SwingWorker。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM