I have a thread in my screen recording application that won't cooperate:
package recorder;
import java.awt.AWTException;
import java.awt.Insets;
import java.io.IOException;
import javax.swing.JFrame;
public class RepeatThread extends Thread {
boolean stop;
public volatile Thread recordingThread;
JFrame frame;
int count = 0;
RepeatThread( JFrame myFrame ) {
stop = false;
frame = myFrame;
}
public void run() {
while( stop == false ) {
int loopDelay = 33; // 33 is approx. 1000/30, or 30 fps
long loopStartTime = System.currentTimeMillis();
Insets insets = frame.getInsets(); // Get the shape we're recording
try {
ScreenRecorder.capture( frame.getX() + insets.left,
frame.getY() + insets.top, frame.getWidth()
- ( insets.left + insets.right ),
frame.getHeight() - ( insets.top + insets.bottom ) );
}
catch( AWTException e1 ) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch( IOException e1 ) {
// TODO Auto-generated catch block
e1.printStackTrace();
} // Add another picture
long loopEndTime = System.currentTimeMillis();
int loopTime = (int )( loopEndTime - loopStartTime );
if( loopTime < loopDelay ) {
try {
sleep( loopDelay - loopTime ); // If we have extra time,
// sleep
}
catch( Exception e ) {
} // If something interrupts it, I don't give a crap; just
// ignore it
}
}
}
public void endThread() {
stop = true;
count = 0;
ScreenRecorder.reset();
// Once I get this annoying thread to work, I have to make the pictures
// into a video here!
}
}
It's been bugging me for ages. It periodically takes screenshots to the specified area.
When you start recording, it hides (decativates) the window. On a Mac, when you give an application focus, any hidden windows will activate. In my class WListener (which I have confirmed to work), I have:
public void windowActivated(WindowEvent e) {
if(ScreenRecorder.recordingThread != null) {
ScreenRecorder.recordingThread.endThread();
}
}
So what SHOULD happen is, the screenshot-taking thread stops when he clicks on the application. However, I must be brutally screwing something up, because when the thread is running, it won't even let the window reappear. This is my first thread, so I expected a weird problem like this. Do you know what's wrong?