// Take a snapshot with white background after a delay specified in a dialog

import fiji.selection.Select_Bounding_Box;

import ij.IJ;
import ij.ImagePlus;

import ij.gui.GenericDialog;
import ij.gui.Roi;

import ij.plugin.ScreenGrabber;

import ij.process.ImageProcessor;

import java.awt.Color;
import java.awt.Dialog;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.KeyboardFocusManager;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;

public class Snapshot extends Thread {
	protected long delay;
	protected Frame whiteBackground;
	protected Window window;

	public Snapshot(long delay) {
		this.delay = delay;
	}

	public void run() {
		Thread.sleep(delay);
		window = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
		if (window == null) {
			IJ.showMessage("No focused ImageJ window!");
			return;
		}
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		whiteBackground = new Frame();
		whiteBackground.setUndecorated(true);
		panel = new Panel();
		panel.setSize(screenSize);
		panel.setMinimumSize(screenSize);
		panel.setBackground(Color.WHITE);
		whiteBackground.add(panel);
		whiteBackground.pack();
		whiteBackground.setSize(screenSize);
		whiteBackground.setVisible(true);
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				Thread.sleep(100);
				toFrontAndSnap();
			}
		});
	}

	public void toFrontAndSnap() {
		window.toFront();
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				Thread.sleep(100);
				snap();
			}
		});
	}

	public void snap() {
		ImageProcessor imageProcessor = new ScreenGrabber().captureScreen().getProcessor();
		Point upperLeft = whiteBackground.getLocationOnScreen();
		Dimension size = whiteBackground.getSize();
		whiteBackground.dispose();
		Rectangle rect = new Rectangle(upperLeft.x, upperLeft.y,
			imageProcessor.getWidth() - upperLeft.x, imageProcessor.getHeight() - upperLeft.y);
		rect = Select_Bounding_Box.getBoundingBox(imageProcessor, rect, 0xffffff);
		imageProcessor.setRoi(new Roi(rect));
		String title = "Delayed snapshot";
		if (window instanceof Frame)
			title = "Snapshot of " + ((Frame)window).getTitle();
		else if (window instanceof Dialog)
			title = "Snapshot of " + ((Dialog)window).getTitle();
		new ImagePlus(title, imageProcessor.crop()).show();
	}
}

gd = new GenericDialog("Delay");
gd.addSlider("Delay (secs.): ", 0, 20, 5);
gd.showDialog();

if (!gd.wasCanceled())
	new Snapshot((long)(1000 * gd.getNextNumber())).start();