FileDocCategorySizeDatePackage
SwingTypeTester.javaAPI DocExample3925Sat Apr 17 15:57:08 BST 2004javathreads.examples.ch05.example1

SwingTypeTester

public class SwingTypeTester extends JFrame implements CharacterSource

Fields Summary
protected RandomCharacterGenerator
producer
private AnimatedCharacterDisplayCanvas
displayCanvas
private CharacterDisplayCanvas
feedbackCanvas
private JButton
quitButton
private JButton
startButton
private JButton
stopButton
private CharacterEventHandler
handler
private ScoreLabel
score
Constructors Summary
public SwingTypeTester()

        initComponents();
    
Methods Summary
public voidaddCharacterListener(CharacterListener cl)

        handler.addCharacterListener(cl);
    
private voidinitComponents()

        handler = new CharacterEventHandler();
        producer = new RandomCharacterGenerator();
        producer.setDone(true);
        producer.start();
        displayCanvas = new AnimatedCharacterDisplayCanvas(producer);
        feedbackCanvas = new CharacterDisplayCanvas(this);
        quitButton = new JButton();
        startButton = new JButton();
        stopButton = new JButton();
        score = new ScoreLabel(producer, this);

	Container pane = getContentPane();
        pane.add(displayCanvas, BorderLayout.NORTH);
        pane.add(feedbackCanvas, BorderLayout.CENTER);

        JPanel p1 = new JPanel();
	p1.setLayout(new BorderLayout());
	score.setText("     ");
	score.setFont(new Font("MONOSPACED", Font.BOLD, 30));
	p1.add(score, BorderLayout.CENTER);

	JPanel p2 = new JPanel();
        startButton.setLabel("Start");
        stopButton.setLabel("Stop");
        stopButton.setEnabled(false);
        quitButton.setLabel("Quit");
        p2.add(startButton);
        p2.add(stopButton);
        p2.add(quitButton);
        p1.add(p2, BorderLayout.EAST);
	pane.add(p1, BorderLayout.SOUTH);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                quit();
            }
        });

        feedbackCanvas.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent ke) {
                char c = ke.getKeyChar();
                if (c != KeyEvent.CHAR_UNDEFINED)
                    newCharacter((int) c);
            }
        });
        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                displayCanvas.setDone(false);
                producer.setDone(false);
                startButton.setEnabled(false);
                stopButton.setEnabled(true);
                feedbackCanvas.setEnabled(true);
                feedbackCanvas.requestFocus();
                score.resetScore();
            }
        });
        stopButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                startButton.setEnabled(true);
                stopButton.setEnabled(false);
                producer.setDone(true);
                displayCanvas.setDone(true);
                feedbackCanvas.setEnabled(false);
            }
        });
        quitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                quit();
            }
        });
        
        pack();
    
public static voidmain(java.lang.String[] args)

        new SwingTypeTester().show();
    
public voidnewCharacter(int c)

        handler.fireNewCharacter(this, c);
    
public voidnextCharacter()

        throw new IllegalStateException("We don't produce on demand");
    
private voidquit()

        System.exit(0);
    
public voidremoveCharacterListener(CharacterListener cl)

        handler.removeCharacterListener(cl);