943,948 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 615
  • Java RSS
Jan 10th, 2009
0

help required in painComponent()

Expand Post »
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);
    }
    
}
Reputation Points: 3
Solved Threads: 0
Light Poster
gauravmishra is offline Offline
31 posts
since Nov 2007
Jan 10th, 2009
0

Re: help required in painComponent()

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,
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Jan 10th, 2009
0

Re: help required in painComponent()

i dont know much abount java threads
i will try to work it out but is there any other way to achieve the same.............
Reputation Points: 3
Solved Threads: 0
Light Poster
gauravmishra is offline Offline
31 posts
since Nov 2007
Jan 10th, 2009
0

Re: help required in painComponent()

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.
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Jan 10th, 2009
0

Re: help required in painComponent()

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 ..............................
Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 3
Solved Threads: 0
Light Poster
gauravmishra is offline Offline
31 posts
since Nov 2007
Jan 10th, 2009
0

Re: help required in painComponent()

Hello again,

Heres the code
java Syntax (Toggle Plain Text)
  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,
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Jan 10th, 2009
0

Re: help required in painComponent()

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 .................
Reputation Points: 3
Solved Threads: 0
Light Poster
gauravmishra is offline Offline
31 posts
since Nov 2007
Jan 11th, 2009
0

Re: help required in painComponent()

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,
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: is it possible that a LinkedStack will be full?
Next Thread in Java Forum Timeline: please help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC