Skip to content

Instantly share code, notes, and snippets.

@tjsingleton
Created June 2, 2011 23:01
Show Gist options
  • Select an option

  • Save tjsingleton/1005528 to your computer and use it in GitHub Desktop.

Select an option

Save tjsingleton/1005528 to your computer and use it in GitHub Desktop.
import java.awt.*;
import javax.swing.*;
public class AnimatedScreenCapture extends JFrame {
JLabel label;
Robot robot;
Rectangle screenRect;
int scaleX;
int scaleY;
public AnimatedScreenCapture() {
super("Animated Screen Capture");
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize().width,
Toolkit.getDefaultToolkit().getScreenSize().height);
scaleX = Toolkit.getDefaultToolkit().getScreenSize().width;
scaleY = Toolkit.getDefaultToolkit().getScreenSize().height;
label = new JLabel();
label.setPreferredSize(new Dimension(scaleX, scaleY));
getContentPane().add( label );
try {
robot = new java.awt.Robot();
} catch (AWTException e) {
e.printStackTrace();
System.exit(-1);
}
}
public void startCapture() {
long start = System.currentTimeMillis();
int i = 0;
int fps = 0;
while (true) {
long current = System.currentTimeMillis();
int seconds = (int) (current-start)/1000;
++i;
// try{
// Thread.sleep(200);
// } catch (Exception e){
// System.out.println("Exception sleeping.");
// }
if (i % 25 == 0 && seconds > 0) {
fps = i / seconds;
this.setTitle("FPS: " + fps);
}
Image screen_capture = robot.createScreenCapture(screenRect); // .getScaledInstance(scaleX, scaleY, Image.SCALE_FAST);
label.setIcon(new ImageIcon(screen_capture));
label.update(label.getGraphics());
}
}
public static void main(String[] args) throws AWTException {
Thread t = new Thread() {
public void run() {
AnimatedScreenCapture asc = new AnimatedScreenCapture();
asc.pack();
asc.setVisible(true);
asc.startCapture();
}
};
SwingUtilities.invokeLater(t);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment