I am trying to take continuous pictures of small window of my screen and create a continuos motion picture sort of thing and display it on the java panel .
For this i am taking snapshots of the screen window by using "Robot" class .
but the panel is only showing the last picture it took. I dont know what to do.............
pls help.............

this is the code i used.............

import java.awt.image.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.awt.geom.*;

import javax.imageio.ImageIO;

public class ScreenVideo
{
    public static void main(String args[]) throws Exception
    {
	ScreenFrame frame=new ScreenFrame();
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setVisible(true);
    }
    
}

class ScreenFrame extends JFrame
{
    public ScreenFrame() throws Exception
    {
	setTitle("testing");
	setSize(500,500);
	ScreenPanel panel=new ScreenPanel();
	add(panel);
    }
}

class ScreenPanel extends JPanel
{
   public BufferedImage img;
    public ScreenPanel() throws Exception
    {
	
	
	    takeShots();
	
	
    }
    
    public void takeShots() throws Exception
    {
	long time=1000L;
	Rectangle rec=new Rectangle(100,100,300,300);
	
	    Robot robot=new Robot();
	
	int i=0;
	while(i!=100)
	{
	    System.out.println("running in the loop with counter#"+i);
	    img = robot.createScreenCapture(rec);
	   
	    Thread.sleep(time);
	   
	    repaint();
	   
	    i++;
	}
	
    }
    
    public void paintComponent(Graphics g)
    {
	super.paintComponent(g);
	Graphics2D g2=(Graphics2D)g;
	
	g2.drawImage(img,0,0,this);
    }
    
}

Hello,

According to me, Once you start program you have to wait for 100 secs then a JFrame will be displayed with last image only.

Its because, you are using same thread to get image from Robot class with 1 second time interval. ( takeShots() method ).

Do takeShots() method working in new thread and code according to it.

Regards,

i dont know much abount java threads
i will try to work it out but is there any other way to achieve the same.............

i dont know much abount java threads
i will try to work it out but is there any other way to achieve the same.............

I dont think so, But if you can remove Thread.Sleep() then its quite possible.

I am confused what does the repaint() method actually does now i tried to create a array of 100 images stored in an array but when i try to display them it shows nothing ..............................

import java.awt.image.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.awt.geom.*;

import javax.imageio.*;

public class ScreenVideo
{
    public static void main(String args[]) throws Exception
    {
	ScreenFrame frame=new ScreenFrame();
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setVisible(true);
    }
    
}

class ScreenFrame extends JFrame
{
    public ScreenFrame() throws Exception
    {
	setTitle("testing");
	setSize(500,500);
	ScreenPanel panel=new ScreenPanel();
	add(panel);
    }
}

class ScreenPanel extends JPanel
{
    public BufferedImage img[]=new BufferedImage[100];
    int flag=0;
    public Robot robot;
    public Rectangle rec;
    public BufferedImage img1;
    public ScreenPanel() throws Exception
    {
	
	 rec=new Rectangle(100,100,300,300);
	  robot=new Robot();
	  
	  for(int i=0;i<100;i++)
	      img[i]=new BufferedImage(100,100,BufferedImage.TYPE_INT_RGB);
	  
	  takeShots();
	
	
    }
    
    public void takeShots() throws Exception
    {
	//long time=500L;
	
	
	   
	
	int i=0;
	while(i!=100)
	{
	    System.out.println("running in the loop with counter#"+i);
	    img[i] = robot.createScreenCapture(rec);
	   
	    
	   
	    repaint();
	   //Thread.sleep(time);
	   // robot.delay(100);
	    i++;
	    
	}
	flag=1;
	repaint();
	
    }
    
    public void paintComponent(Graphics g)
    {
	super.paintComponent(g);
	Graphics2D g2=(Graphics2D)g;
	if(flag==1)
	{
	    for(int i=0;i<100;i++)
	    {
		g2.drawImage(img[i],0,0,this);
		robot.delay(500);
	    }
	}
    }
    
}

Hello again,

Heres the code

import java.awt.AWTException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

public class ScreenVideo extends JFrame {
	BufferedImage img;
	Robot robot;
	Rectangle rect;
	public ScreenVideo(){
		//init components
		try {
			robot = new Robot();
			rect = new Rectangle(100,100,300,300);
		} catch (AWTException e) {
			e.printStackTrace();
		}
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(500,500);
		this.setVisible(true);
		takeShots();
	}
	private void takeShots() {
		for (int i = 0; i < 100; i++) {
			img = robot.createScreenCapture(rect);
			repaint();
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
	}
	public void paint(Graphics g){
		super.paintComponents(g);
		Graphics2D g2 = (Graphics2D)g;
		g2.drawImage(img,0,0,this);
	}
	public static void main(String[] args) {
		new ScreenVideo();
	}
}

Regards,

thanks a lot for your help ...
but still i cant figure out what was the mistake i was doing in my earlier code it is very much similar to this one except that i did not put the sleep() in try catch block....
what difference it made .................

thanks a lot for your help ...
but still i cant figure out what was the mistake i was doing in my earlier code it is very much similar to this one except that i did not put the sleep() in try catch block....
what difference it made .................

Hello,

1st difference is, you invoked takeShots() method first by creating object of ScreenPanel class to add it in frame. So before the frame got visible all the takeshots() processing got completed (takes almost 100 secs).

2nd one, repaint() method calls the paint(Graphics g) method. So i have done the drawing work in paint(Graphics g) method.

Regards,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.