Results 1 to 7 of 7
Thread: print screen help
- 03-23-2012, 04:55 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
print screen help
hi there,
i am new to java and now i am doing a project that nid to print screen (example : 1seconds 1 pic). Below is my code :
import java.awt.image.BufferedImage;
import java.awt.Rectangle;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Robot;
import java.io.File;
import javax.imageio.ImageIO;
class ScreenRecorder {
public static void main(String args[]) {
try {
Boolean a = false;
Toolkit tool = Toolkit.getDefaultToolkit();
Dimension d = tool.getScreenSize();
Rectangle rect = new Rectangle(1440,900);
Robot robot = new Robot();
Thread.sleep(2000);
while (a != true)
{
File f = new File("screenshot.jpg");
BufferedImage img = robot.createScreenCapture(rect);
ImageIO.write(img,"jpeg",f);
tool.beep();
}
} catch(Exception e){
e.printStackTrace();
}
}
}
i can set it to keep print screen but only with a single image and it will keep overwrite it.
can anyone teach me how to set it so thaat it can print screen in 1s or maybe 0.1s or maybe 0.001s ..
thanks in advance for replying
-
Re: print screen help
So you want one pic every second?
You could use a Timer, TimerTask and Timer.scheduleAtFixedRate(TimerTask, 100, 1000) to repeat the task every second, starting after 0.1 seconds.
Then you could use Timer.cancel() when you want to stop.
Free links for you:
Timer (Java 2 Platform SE v1.4.2)
TimerTask (Java 2 Platform SE v1.4.2)Last edited by ozzyman; 03-23-2012 at 05:02 AM.
- 03-23-2012, 07:59 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
Re: print screen help
thanks ozzyman ..
- 03-23-2012, 03:34 PM #4
Re: print screen help
why don't you use:Java Code:while (a != true)
orJava Code:while (true)
just my opinion... maybe you have a reason why you use it...Java Code:boolean a = true; while (a) //...
-
Re: print screen help
If you use a while loop you have two problems:
1) Your thread doesn't respond until the while loop has finished and
2) Capturing the images every second would clog up your main thread anyway
So your program will become less responsive or unresponsive.
However, when you create a Timer a separate background thread is created just for the Timer's tasks.
Every TimerTask scheduled to that Timer shares that thread though.
Read it here:
Timer (Java Platform SE 6)
- 03-27-2012, 08:18 AM #6
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
Re: print screen help
thanks for all your reply
i have modified my program as below
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;
import java.awt.Dimension;
import java.awt.Robot;
import java.io.File;
import javax.imageio.ImageIO;
public class Main {
Toolkit toolkit;
Timer timer;
public Main() {
toolkit = Toolkit.getDefaultToolkit();
timer = new Timer();
timer.scheduleAtFixedRate(new RemindTask(), 0,
1*100);
}
class RemindTask extends TimerTask {
public void run() {
try {
Boolean a = true;
Toolkit tool = Toolkit.getDefaultToolkit();
Dimension d = tool.getScreenSize();
Rectangle rect = new Rectangle(1440,900);
Robot robot = new Robot();
Thread.sleep(2000);
while (a )
{
File f = new File("screenshot.jpg");
BufferedImage img = robot.createScreenCapture(rect);
ImageIO.write(img,"jpeg",f);
}
} catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String args[]) {
new Main();
}
}
this is my new code and the things i wan to ask is
timer.scheduleAtFixedRate(new RemindTask(), 0,
1*100);
the 1*100 is how long ???
if i wan 0.01s or even 0.001s is there any possible to do that ?
and sometimes print screen the half of the image will be black. what it will like that ???
thanks in advance for the reply .:)Last edited by Ericyue; 03-27-2012 at 08:33 AM.
-
Similar Threads
-
Print a receipt to screen
By africanhacker in forum New To JavaReplies: 8Last Post: 04-28-2011, 06:51 AM -
choosing random array to print to screen
By debugdoug in forum New To JavaReplies: 4Last Post: 04-05-2011, 03:45 PM -
can you print a JPanel without actually rendering it on screen first?
By AcousticBruce in forum New To JavaReplies: 11Last Post: 03-09-2011, 08:18 AM -
print on the client screen
By a_maged in forum NetworkingReplies: 0Last Post: 12-17-2007, 04:10 PM -
How do i tell it to print out the result to the screen???
By paul in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:04 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks