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 {
volatile 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?
EDIT: Okay, I made stop volatile, and here is the place where I make the thread:
package recorder;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class ScreenRecorder {
static RepeatThread recordingThread;
static int count;
public static void record(JFrame frame) {
if(recordingThread == null) { //Make a new thread if we don't have one
recordingThread = new RepeatThread(frame);
recordingThread.start();
}
}
public static void capture(int x, int y, int width, int height) throws AWTException, IOException {
// capture the whole screen
//BufferedImage screencapture = new Robot().createScreenCapture(
// new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
BufferedImage screencapture = new Robot().createScreenCapture(
new Rectangle( x, y, width, height));
// Save as JPEG
File directory = new File("/Users/stuart/Movies/temp");
if(directory.exists() == false) {
directory.mkdirs();
}
count ++;
File file = new File("/Users/stuart/Movies/temp/screencapture" + count + ".jpg");
ImageIO.write(screencapture, "jpg", file);
// Save as PNG
// File file = new File("screencapture.png");
// ImageIO.write(screencapture, "png", file);
}
public static void stop() {
}
public static void reset() {
count = 0;
}
}