| | |
help required in painComponent()
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 31
Reputation:
Solved Threads: 0
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.............
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,
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
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - ORM Framework, Pwing - Custom GUI Toolkit
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - ORM Framework, Pwing - Custom GUI Toolkit
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - ORM Framework, Pwing - Custom GUI Toolkit
•
•
Join Date: Nov 2007
Posts: 31
Reputation:
Solved Threads: 0
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)
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
Regards,
Heres the code
java Syntax (Toggle Plain Text)
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,
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - ORM Framework, Pwing - Custom GUI Toolkit
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - ORM Framework, Pwing - Custom GUI Toolkit
•
•
•
•
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 .................
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
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - ORM Framework, Pwing - Custom GUI Toolkit
![]() |
Other Threads in the Java Forum
- Previous Thread: is it possible that a LinkedStack will be full?
- Next Thread: please help
Views: 455 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for Java
access actionlistener add android applet application arguments array arraylist arrays binary build c++ chat class classes client code combobox compile component convert data database design detection draw eclipse error event exception file filei/o game givemetehcodez graphics gui helpwithhomework html ide image images inheritance input interface j2me java javafx jframe jpanel julia linked linked-list list loop method methods mobile netbeans newbie number object oracle os parameter pattern phone pixel print printing problem program programming project read recursion remote remove return robot scanner screen search server service set sms socket sort source sql string swing system test text thread time tree user





