java - 如何在 ImageIcon 中模拟视频?

标签 java multithreading swing video imageicon

我有 JToggleButton 和此方法捕获事件:

tgl_playMouseClicked(java.awt.event.MouseEvent evt) {                                             
            new Thread() {
                public void run() {
                    int i = 0;
                    String outputName = null;
                    while ((i <= 99)) {
                        ImageIcon imgThisImg = new ImageIcon("images/" + outputName + i + ".png");
                        lbl_image.setIcon(imgThisImg);
                        i++;
                    }
                    tgl_play.setSelected(!tgl_play.isSelected());

                }
            }.start();
}

我尝试通过读取和显示 imageIcon 中的单个图像来模拟视频。 当我第一次单击 JToggleButton 时,一切正常。视频正在运行。但当我再次按下时,什么也没有发生。当打印显示在输出中时捕获该事件,但 ImageIcon 上没有刷新。

我在那里使用线程以便能够在帧之间设置一些延迟。

怎么了?请帮助我

最佳答案

我认为最好的方法之一是使用 javax.swing.Timer 来调整“视频”的速度。这将确保您正确使用 Swing EDT 进行所有操作。

(如果毫秒不够,那么我会看一下:java.util.concurrent.Executors.newScheduledThreadPool(int)java.util.concurrent.ScheduledThreadPoolExecutor。 ScheduleAtFixedRate(Runnable, long, long, TimeUnit) 并添加立即调用 SwingUtilities.invokeLater()) 中所有代码的 Runnable)

这里我制作了一个小型演示示例,其中包含显示不断增大和缩小的圆圈的图像列表(这些图像是使用一些 JPanel 动态创建的,但这仅用于演示)。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UnsupportedLookAndFeelException;

public class TestAnimation {
    private static final int NB_OF_IMAGES = 50;
    private static final int NB_OF_IMAGES_PER_SECOND = 25;
    private static final int WIDTH = 300;
    private static final int HEIGHT = 300;

    protected void initUI() {
        final JFrame frame = new JFrame(TestAnimation.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        // the label on which I will set images
        final JLabel label = new JLabel();
        // By adding to the frame, it is set as the central component of the 
        // BorderLayout of the JFrame. Eventually, the label will have the size of the content pane
        frame.add(label);
        frame.setSize(WIDTH, HEIGHT);
        // Creating a list of images (just for demo purposes)
        final List<Image> images = new ArrayList<Image>(NB_OF_IMAGES);
        for (int i = 0; i < NB_OF_IMAGES; i++) {
            CirclePanel circle = new CirclePanel(WIDTH / 2, WIDTH / 2, 2 * WIDTH * (NB_OF_IMAGES / 2 - Math.abs(i - NB_OF_IMAGES / 2))
                    / NB_OF_IMAGES);
            circle.setSize(WIDTH, HEIGHT);
            BufferedImage image = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration()
                    .createCompatibleImage(WIDTH, HEIGHT, BufferedImage.TRANSLUCENT);
            circle.print(image.getGraphics());
            images.add(image);
        }
        // Here is the timer logic
        Timer t = new Timer(1000 / NB_OF_IMAGES_PER_SECOND, new ActionListener() {
            private int i = 0;

            @Override
            public void actionPerformed(ActionEvent e) {
                if (i == images.size()) {
                    i = 0;
                }
                label.setIcon(new ImageIcon(images.get(i++)));
            }
        });
        frame.setVisible(true);
        t.start();
    }

    // Simple class that draws a red circle centered on x,y and given radius
    public static class CirclePanel extends JPanel {

        private int x;
        private int y;
        private int radius;

        public CirclePanel(int x, int y, int radius) {
            super();
            this.x = x;
            this.y = y;
            this.radius = radius;
            setOpaque(false);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.RED);
            g.drawArc(x - radius / 2, y - radius / 2, radius, radius, 0, 360);
        }
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestAnimation().initUI();
            }
        });
    }
}

关于java - 如何在 ImageIcon 中模拟视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13424069/

相关文章:

java - 从 JedisPool 获取资源时线程处于等待状态

java - 第一次打开 JDialog 时运行 KeyEventDispacther,然后将其删除

java - 如何为java程序创建快捷方式图标

java - 在 Hyperledger Fabric V1.0 的本地开发环境中使用 REST API 支持

java - 为什么我们必须使用 InetSocketAddress 来输入我们的端口号才能使用 ServerSocketChannel

Java:通过流通信的两个线程是公司,三个是人群

java - 我有一个 JMenuItem 实例(比如说 TEMP)。我想知道添加了 TEMP 的 JMenu 的名称是什么。如何?

java - 我们可以在 Java 中重载 main() 方法吗?

java - 检查 IPv4 地址是否在私有(private)范围内

c++ - std::async 和 std::future 是否在幕后使用互斥体和条件变量?