TimerTestFrame

package ch.usi.inf.sape.gui.test.app;
 
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.Timer;
 
 
/**
 * Test GUI capture and replay tools for their ability to synchronize replay with timers.
 * 
 * See our AST'10 paper "Automating Performance Testing of Interactive Java Applications".
 * 
 * Copyright (c) 2010 - Sape Research Group, University of Lugano
 */
public final class TimerTestFrame extends JFrame {
 
	private Timer timer;
 
 
	public TimerTestFrame() {
		super("TimerTestFrame");
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				timer.stop();
			}
		});
		add(new JComponent() {
			private boolean armed;
			private int hitCount;
			private int clickCount;
			{
				addMouseListener(new MouseAdapter() {
					public void mouseClicked(final MouseEvent ev) {
						if (armed) {
							hitCount++;
						}
						clickCount++;
						System.out.println(clickCount+" clicks; "+hitCount+" hits");
						repaint();
					}
				});
				// toggle every 300ms
				timer = new Timer(300, new ActionListener() {
					public void actionPerformed(final ActionEvent ev) {
						armed = !armed;
						repaint();
					}
				});
				timer.start();
			}
			public void paint(final Graphics g) {
				final int width = getWidth();
				final int height = getHeight();
				if (armed) {
					g.setColor(Color.RED);
				} else {
					g.setColor(Color.WHITE);
				}
				g.fillRect(0, 0, width, height);
				g.setColor(Color.BLACK);
				g.drawString(clickCount+" clicks; "+hitCount+" hits", 5, 12);
			}
		});
		setSize(300, 300);
		setVisible(true);
	}
 
	public static void main(final String[] args) {
		new TimerTestFrame();
	}
 
}