Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 知道GIF何时播放完毕_Java_Animation_Gif - Fatal编程技术网

Java 知道GIF何时播放完毕

Java 知道GIF何时播放完毕,java,animation,gif,Java,Animation,Gif,我正在尝试向JLabel显示GIF文件。没问题,我加载GIF并将其设置为标签的图标。但现在我真的需要知道它什么时候结束,因为它必须作为一个剪接场景。我已经试过了(游戏场景是一个自定义对象,但我知道setIcon可以工作,相信我): 现在,我是否可以在不让整个程序在给定时间内休眠的情况下实现这一点,然后继续执行其余代码,完全不绘制GIF文件?是否可以使用用于制作GIF文件的程序使GIF不重复?大多数制作GIF图像的照片编辑器都提供了重复或不重复的选项 如果我理解你的问题,你似乎只想让gif播放一次

我正在尝试向JLabel显示GIF文件。没问题,我加载GIF并将其设置为标签的图标。但现在我真的需要知道它什么时候结束,因为它必须作为一个剪接场景。我已经试过了(游戏场景是一个自定义对象,但我知道setIcon可以工作,相信我):


现在,我是否可以在不让整个程序在给定时间内休眠的情况下实现这一点,然后继续执行其余代码,完全不绘制GIF文件?

是否可以使用用于制作GIF文件的程序使GIF不重复?大多数制作GIF图像的照片编辑器都提供了重复或不重复的选项

如果我理解你的问题,你似乎只想让gif播放一次


如果是为了一个剪辑场景,为什么不让程序在这段时间内休眠呢

这是一个非常简单的例子,它只适用于非循环gif,如果gif重复,则必须测试请求帧位之间的时间间隔,并尝试确定是否可以检测到循环点

public class MonitoringLabel extends JLabel {

    public MonitoringLabel(Icon image) {
        super(image);
    }

    public void addActionListener(ActionListener listener) {
        listenerList.add(ActionListener.class, listener);
    }

    public void removeActionListener(ActionListener listener) {
        listenerList.remove(ActionListener.class, listener);
    }

    protected void fireActionPerformed() {
        ActionListener[] listeners = listenerList.getListeners(ActionListener.class);
        if (listeners.length > 0) {
            ActionEvent evt = new ActionEvent(this, 0, "stopped");
            for (ActionListener listener : listeners) {
                listener.actionPerformed(evt);
            }
        }
    }

    @Override
    public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) {
        boolean finished = super.imageUpdate(img, infoflags, x, y, w, h);
        if (!finished) {
            fireActionPerformed();
        }
        return finished;
    }

}
上面的类提供了一个
ActionListener
,当图像“完成”加载时触发该类(当它们在动画中没有要播放的新帧时触发-重复GIF永远不会返回
false

如果需要,您可以使用不同的侦听器接口,我只是把它作为一个示例放在一起


我选择覆盖
imageUpdate
的原因是
JLabel
在绘制图像时已经充当图像的
ImageObserver
,这样我们就可以使用
JLabel
的强大功能,而无需手动绘制gif。。。是的,我做到了,以“直截了当”开始提问很痛苦,不是吗?你答对了吗?正如你在我的代码中看到的那样,这正是问题所在,它会在用户给出的提示下休眠一段时间,但它只是出于某种原因暂停了整个程序。这就是我打算做的。但这有点帮助了:驻留,睡眠确实会在调用整个程序时暂停整个程序,暂停时间为设定的毫秒数。我不认为我完全理解这里的问题。如果您需要一个计算gif本身长度的包罗万象的解决方案,我在Java文档中找到了imageObserver和imageReader类,它们可能会对您有所帮助。具体来说:@JarnoGabriël这是因为Swing是单线程的,并且使用
线程。sleep
将阻塞主线程,阻止它更新UI。您需要使用Swing
定时器进行此类操作谢谢!我不知道主线程被阻塞了。:)循环GIF可以通过每次传递
FRAMEBITS
标志时增加一个计数器来处理,并且只有在达到帧计数时才触发操作。@teppic假设您知道帧数,这不是最容易获得的,您可以“等待”给定的时间段“希望”是关于“正确的2”,但同样,它假设了一定数量的knowledge@MadProgrammer我想试试,但我现在不在家。@MadProgrammer谢谢!这正是我想要的,thx人。我该如何,确切地说,使用这个例子?我必须做什么才能停止GIF动画的信号?请给我举个例子,我完全不懂。对不起,我5天前才开始用Java编写代码。
public class MonitoringLabel extends JLabel {

    public MonitoringLabel(Icon image) {
        super(image);
    }

    public void addActionListener(ActionListener listener) {
        listenerList.add(ActionListener.class, listener);
    }

    public void removeActionListener(ActionListener listener) {
        listenerList.remove(ActionListener.class, listener);
    }

    protected void fireActionPerformed() {
        ActionListener[] listeners = listenerList.getListeners(ActionListener.class);
        if (listeners.length > 0) {
            ActionEvent evt = new ActionEvent(this, 0, "stopped");
            for (ActionListener listener : listeners) {
                listener.actionPerformed(evt);
            }
        }
    }

    @Override
    public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) {
        boolean finished = super.imageUpdate(img, infoflags, x, y, w, h);
        if (!finished) {
            fireActionPerformed();
        }
        return finished;
    }

}