help required in painComponent()

Thread Solved

Join Date: Nov 2007
Posts: 31
Reputation: gauravmishra has a little shameless behaviour in the past 
Solved Threads: 0
gauravmishra gauravmishra is offline Offline
Light Poster

help required in painComponent()

 
0
  #1
Jan 10th, 2009
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);
    }
    
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 122
Reputation: puneetkay is on a distinguished road 
Solved Threads: 24
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: help required in painComponent()

 
0
  #2
Jan 10th, 2009
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,
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - ORM Framework, Pwing - Custom GUI Toolkit
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 31
Reputation: gauravmishra has a little shameless behaviour in the past 
Solved Threads: 0
gauravmishra gauravmishra is offline Offline
Light Poster

Re: help required in painComponent()

 
0
  #3
Jan 10th, 2009
i dont know much abount java threads
i will try to work it out but is there any other way to achieve the same.............
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 122
Reputation: puneetkay is on a distinguished road 
Solved Threads: 24
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: help required in painComponent()

 
0
  #4
Jan 10th, 2009
Originally Posted by gauravmishra View Post
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.
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - ORM Framework, Pwing - Custom GUI Toolkit
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 31
Reputation: gauravmishra has a little shameless behaviour in the past 
Solved Threads: 0
gauravmishra gauravmishra is offline Offline
Light Poster

Re: help required in painComponent()

 
0
  #5
Jan 10th, 2009
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 ..............................
  1.  
  2. import java.awt.image.*;
  3. import java.awt.*;
  4. import java.io.*;
  5. import javax.swing.*;
  6. import java.awt.geom.*;
  7.  
  8. import javax.imageio.*;
  9.  
  10. public class ScreenVideo
  11. {
  12. public static void main(String args[]) throws Exception
  13. {
  14. ScreenFrame frame=new ScreenFrame();
  15. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16. frame.setVisible(true);
  17. }
  18.  
  19. }
  20.  
  21. class ScreenFrame extends JFrame
  22. {
  23. public ScreenFrame() throws Exception
  24. {
  25. setTitle("testing");
  26. setSize(500,500);
  27. ScreenPanel panel=new ScreenPanel();
  28. add(panel);
  29. }
  30. }
  31.  
  32. class ScreenPanel extends JPanel
  33. {
  34. public BufferedImage img[]=new BufferedImage[100];
  35. int flag=0;
  36. public Robot robot;
  37. public Rectangle rec;
  38. public BufferedImage img1;
  39. public ScreenPanel() throws Exception
  40. {
  41.  
  42. rec=new Rectangle(100,100,300,300);
  43. robot=new Robot();
  44.  
  45. for(int i=0;i<100;i++)
  46. img[i]=new BufferedImage(100,100,BufferedImage.TYPE_INT_RGB);
  47.  
  48. takeShots();
  49.  
  50.  
  51. }
  52.  
  53. public void takeShots() throws Exception
  54. {
  55. //long time=500L;
  56.  
  57.  
  58.  
  59.  
  60. int i=0;
  61. while(i!=100)
  62. {
  63. System.out.println("running in the loop with counter#"+i);
  64. img[i] = robot.createScreenCapture(rec);
  65.  
  66.  
  67.  
  68. repaint();
  69. //Thread.sleep(time);
  70. // robot.delay(100);
  71. i++;
  72.  
  73. }
  74. flag=1;
  75. repaint();
  76.  
  77. }
  78.  
  79. public void paintComponent(Graphics g)
  80. {
  81. super.paintComponent(g);
  82. Graphics2D g2=(Graphics2D)g;
  83. if(flag==1)
  84. {
  85. for(int i=0;i<100;i++)
  86. {
  87. g2.drawImage(img[i],0,0,this);
  88. robot.delay(500);
  89. }
  90. }
  91. }
  92.  
  93. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 122
Reputation: puneetkay is on a distinguished road 
Solved Threads: 24
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: help required in painComponent()

 
0
  #6
Jan 10th, 2009
Hello again,

Heres the code
  1. import java.awt.AWTException;
  2. import java.awt.Graphics;
  3. import java.awt.Graphics2D;
  4. import java.awt.Rectangle;
  5. import java.awt.Robot;
  6. import java.awt.image.BufferedImage;
  7.  
  8. import javax.swing.JFrame;
  9.  
  10. public class ScreenVideo extends JFrame {
  11. BufferedImage img;
  12. Robot robot;
  13. Rectangle rect;
  14. public ScreenVideo(){
  15. //init components
  16. try {
  17. robot = new Robot();
  18. rect = new Rectangle(100,100,300,300);
  19. } catch (AWTException e) {
  20. e.printStackTrace();
  21. }
  22. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  23. this.setSize(500,500);
  24. this.setVisible(true);
  25. takeShots();
  26. }
  27. private void takeShots() {
  28. for (int i = 0; i < 100; i++) {
  29. img = robot.createScreenCapture(rect);
  30. repaint();
  31. try {
  32. Thread.sleep(1000);
  33. } catch (InterruptedException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37.  
  38. }
  39. public void paint(Graphics g){
  40. super.paintComponents(g);
  41. Graphics2D g2 = (Graphics2D)g;
  42. g2.drawImage(img,0,0,this);
  43. }
  44. public static void main(String[] args) {
  45. new ScreenVideo();
  46. }
  47. }

Regards,
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - ORM Framework, Pwing - Custom GUI Toolkit
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 31
Reputation: gauravmishra has a little shameless behaviour in the past 
Solved Threads: 0
gauravmishra gauravmishra is offline Offline
Light Poster

Re: help required in painComponent()

 
0
  #7
Jan 10th, 2009
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 .................
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 122
Reputation: puneetkay is on a distinguished road 
Solved Threads: 24
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: help required in painComponent()

 
0
  #8
Jan 11th, 2009
Originally Posted by gauravmishra View Post
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,
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - ORM Framework, Pwing - Custom GUI Toolkit
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum


Views: 455 | Replies: 7
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC