【问题标题】:display image after after on the panel with a time gap在面板上显示图像后有时间间隔
【发布时间】:2009-11-08 07:33:56
【问题描述】:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.*;
import java.io.*;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Video implements ActionListener
{
    static int width=480;
    static int height=368;
    static JFrame frame = new JFrame();
    static JButton button = new JButton("Submit");
    static BufferedImage img = new BufferedImage((int) (width), (int) (height), BufferedImage.TYPE_INT_RGB);
    static BufferedImage img1[] = new BufferedImage[60];
    static {
      for (int i = 0; i < img1.length; i++) {
        img1[i] = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      }
    }
    public static void main(String[] args)
    {
        Video V1 = new Video();
        String fileName = args[0];
        try {
            File file = new File(fileName);
            InputStream is = new FileInputStream(file);

            long len = file.length();
            byte[] bytes = new byte[(int)len];
            int offset = 0;
            int numRead = 0;
            int ind =0;
            int[][] pixarray=new int[height+100][width+100];
            int[][] red=new int[width*2][height*2];
            int[][] green=new int[width*2][height*2];
            int[][] blue=new int[width*2][height*2];
            while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
                offset += numRead;
            }
            for(int frames=0;frames<60;frames++)
            {
                ind=height*width*frames*3;
                   for(int y = 0; y < height; y++)
                {
                       for(int x = 0; x < width; x++)
                       {
                           byte a = 0;
                           byte r = bytes[ind];
                           byte g = bytes[ind+height*width];
                           byte b = bytes[ind+height*width*2];
                           int pix = 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
                           pixarray[y][x]=pix;
                           red[y][x] = (pix >>> 16) & 0xff;
                           green[y][x] = (pix >>> 8) & 0xff;
                           blue[y][x] = pix & 0xff;
                           img1[frames].setRGB(x,y,pix);
                           ind++;
                       }
                }
            }

        }
        catch (IOException e) {
            e.printStackTrace();
         }


        JLabel label = new JLabel(new ImageIcon(img1[50]));

        frame.setLayout(new FlowLayout());
        //frame.setSize(200,100);
        frame.setVisible(true);
       // Button button = new Button("Submit");
       // frame.add(button);
        frame.getContentPane().add(label, BorderLayout.CENTER);
        frame.getContentPane().add(button, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);

        button.addActionListener(V1);

    }
    public void actionPerformed(ActionEvent e) {
        System.out.println("1");
        for(int i=0;i<img1.length;i++)
        {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            System.out.println(img1.length);
         }


    }
}

是我的代码。我的 img1 是一组帧。在这种情况下,我的视频有 60 帧。

我想将每个帧添加到我的面板中,并带有时间间隔。我无法做到这一点,因为每次我将它添加到我的ActionEvent 的面板中时,它的行为都很奇怪。请帮帮我。

【问题讨论】:

    标签: java swing


    【解决方案1】:

    使用 Swing Timer(不是 TimerTask)。当 Timer 触发时,代码将在 EDT 中执行,因此您可以安全地重置 JLabel 的图标。所以我会首先从您的 BufferedImages 创建 ImageIcons 并将图标存储在您的数组中。

    阅读 How to Use Timers 上的 Swing 教程中的部分以获取更多信息。您可能还想查看 Concurreny 部分,了解为什么在 EDT 中执行代码很重要。

    【讨论】:

      【解决方案2】:

      你可以创建一个 TimerTask

      类 VideoTask 扩展 TimerTask { 私人框架框架; 私有 int 框架 ID; 公共无效运行(){ frame.drawImage(....); 帧ID++; } }

      在按钮的动作监听器中 - 安排任务:

      VideoTask videoTask = new VideoTask(frame); videoTask.schedule(..);

      【讨论】:

        猜你喜欢
        • 2018-01-18
        • 2011-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-08
        • 2021-09-08
        • 2013-06-03
        • 1970-01-01
        相关资源
        最近更新 更多