package cz.zah.is.creator;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
public class TestReakce extends JFrame implements ActionListener{
/**
* Konstruktor t\u0159ídy TestReakce
* @param arg0
*/
public TestReakce(GraphicsConfiguration arg0) {
super(arg0);
init();
}
/**
* Konstruktor t\u0159ídy TestReakce
* @param arg0
* @param arg1
*/
public TestReakce(String arg0, GraphicsConfiguration arg1) {
super(arg0, arg1);
init();
}
/**
* Konstruktor t\u0159ídy TestReakce
* @param arg0
* @throws HeadlessException
*/
public TestReakce(String arg0) throws HeadlessException {
super(arg0);
init();
}
/**
* Pole serialVersionUID long
*/
private static final long serialVersionUID = 1L;
/**
* Pole DEFAULT_INACTIVE_COLOR Color
*/
public static final Color DEFAULT_INACTIVE_COLOR = Color.blue;
/**
* Pole DEFAULT_ACTIVE_COLOR Color
*/
public static final Color DEFAULT_ACTIVE_COLOR = Color.GREEN;
private class StartAction extends AbstractAction {
/**
* Konstruktor t\u0159ídy StartAction
*/
public StartAction() {
super("Start");
}
/**
* Pole serialVersionUID long
*/
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent arg0) {
afterPressStart();
}
}
/**
* Informace o stavu v CVS archivu $Author: $ $Date: $ $Revision:$ $State: $
*
* Log zm\u011bn $Log: $
*/
private class ClickListener implements MouseListener {
@Override
public void mouseClicked(MouseEvent arg0) {
afterClick();
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
private StartAction startAction;
protected StartAction getStartAction() {
if (startAction == null)
startAction = new StartAction();
return startAction;
}
private JPanel testField;
private JButton startButton;
private JPanel workPanel;
private Timer intTimer;
protected Timer getIntTimer() {
if (intTimer == null){
intTimer = new Timer(1000, this);
intTimer.setRepeats(false);
}
return intTimer;
}
protected JPanel getWorkPanel() {
if (workPanel == null){
workPanel = new JPanel();
workPanel.setLayout(new BorderLayout());
workPanel.add(getStartButton(), BorderLayout.SOUTH);
workPanel.add(getTestField(), BorderLayout.CENTER);
}
return workPanel;
}
protected JButton getStartButton() {
if (startButton == null)
startButton = new JButton(getStartAction());
return startButton;
}
protected JPanel getTestField() {
if (testField == null) {
testField = new JPanel();
testField.setPreferredSize(new Dimension(100,100));
testField.setMinimumSize(testField.getPreferredSize());
testField.setBorder(new LineBorder(Color.BLACK));
testField.addMouseListener(new ClickListener());
testField.setBackground(DEFAULT_INACTIVE_COLOR);
}
return testField;
}
public void afterPressStart() {
getStartAction().setEnabled(false);
Timer t =getIntTimer();
t.setDelay((int) ((Math.random() * 10000) % 4000));
t.start();
}
public void afterClick() {
getTestField().setBackground(DEFAULT_INACTIVE_COLOR);
getStartAction().setEnabled(true);
}
public TestReakce() {
super();
init();
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
TestReakce app = new TestReakce();
app.pack();
app.setVisible(true);
} catch (Throwable exception) {
System.err.println("Exception occurred in main()");
exception.printStackTrace(System.out);
}
}
private void init(){
getContentPane().add(getWorkPanel(),BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
getTestField().setBackground(DEFAULT_ACTIVE_COLOR);
}
}