-
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)
-
Re: print screen help
-
Re: print screen help
why don't you use:
or
Code:
boolean a = true;
while (a)
//...
just my opinion... maybe you have a reason why you use it...
-
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)
-
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 .:)
-
Re: print screen help
The time is in milliseconds. In future you can refer to the java docs if you're not sure. In fact I posted the link to it already in my previous post.
In case you don't know, 1000 milliseconds = 1 seconds, so 100 would be 0.1 seconds.