tags:

views:

74

answers:

2
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);
         }


    }
}

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

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.

A: 

You can create a TimerTask

class VideoTask extends TimerTask {
   private Frame frame;
   private int frameId;
   public void run() {
     frame.drawImage(....);
     frameId++;
   }
}

And in the action listener for the button - schedule the task:

VideoTask videoTask = new VideoTask(frame);
videoTask.schedule(..);
Bozho
+1  A: 

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.

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.

camickr