user188944
user188944

Reputation:

Java: How to wait until a screenshot is captured before taking a new one

I have written java code that captures a screenshot. User can set a time duration in seconds, e.g. 20 seconds, then after 20 seconds screenshot of current working application will be taken. Now I want to improve code by allowing user to set Number of screenshots to be captured in sec like if user sets ImageCnt=5 then 5 screenshots should be taken after every 20 sec.the code is like ....

int cnt = ImageCnt;

while(cnt!=0)
{
   timerclass t=new timerclass(Time);//captures screenshot after specified Time
   cnt--;
}   

My Problem is in this code. I want that second screenshot should not be captured before the first is completed and third should not be captured before 2nd is completed and so on........ In my code it simultaneously captures all screenshot.

Upvotes: 0

Views: 388

Answers (3)

camickr
camickr

Reputation: 324128

I don't understand what the benefit of taking 5 screenshots everytime the Timer fires will do for you. Each screenshot will take milliseconds to perform, so what is going to change on the screen? You can just create copy of the first image which will be more efficient then taking a new screen image.

I would also probably use a Swing Timer to schedule the screenshot so the image is created in the EDT which means the GUI won't be attempting to repaint itself at the same time you are creating the image.

I use the ScreenImage class to create my images.

Upvotes: 0

jheddings
jheddings

Reputation: 27573

You'll need a method in your timerclass that indicates the capture was completed.

Something like:

 while (cnt > 0) {
   timerclass t = new timerclass(Time);
   t.waitForCapture();
   cnt--;
}

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500785

It sounds to me like instead of creating 5 timers, you ought to create a single timer which knows how many screenshots to take. It can then take one after another when the timer fires.

It's hard to know exactly what that will entail without seeing the rest of your code, but I'd advise adding the logic to your screenshot capture code rather than the code which sets up the timers.

Upvotes: 4

Related Questions