在具有时间间隔的面板上之后显示图像 [英] display image after after on the panel with a time gap

查看:102
本文介绍了在具有时间间隔的面板上之后显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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帧。

Is my code. My img1 is an array of frames. in this instance my video has 60 frames.

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

I wanna add each frame to my panel with a time gap. i am unable to do it because every time i add it to the panel in my ActionEvent its acting weird. help me out please.

推荐答案

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

Use a Swing Timer (not a TimerTask). When the Timer fires the code will execute in the EDT so you can safely reset the icon of your JLabel. So I would first create ImageIcons from your BufferedImages and store the Icons in your array.

如何使用计时器获取更多信息。您可能还想查看Concurreny部分,了解为什么在EDT中执行代码很重要。

Read the secton from the Swing tutorial on How to Use Timers for more information. You may also want to check out the section on Concurreny to understand why executing code in the EDT is important.

这篇关于在具有时间间隔的面板上之后显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆