FileDocCategorySizeDatePackage
SwingTypeTester.javaAPI DocExample3607Thu Apr 01 23:53:06 BST 2004javathreads.examples.ch02.example7

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
Constructors Summary
public SwingTypeTester()

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

        handler.addCharacterListener(cl);
    
private voidinitComponents()

        handler = new CharacterEventHandler();
        displayCanvas = new AnimatedCharacterDisplayCanvas();
        feedbackCanvas = new CharacterDisplayCanvas(this);
        quitButton = new JButton();
        startButton = new JButton();
        stopButton = new JButton();
        add(displayCanvas, BorderLayout.NORTH);
        add(feedbackCanvas, BorderLayout.CENTER);
        JPanel p = new JPanel();
        startButton.setLabel("Start");
        stopButton.setLabel("Stop");
        stopButton.setEnabled(false);
        quitButton.setLabel("Quit");
        p.add(startButton);
        p.add(stopButton);
        p.add(quitButton);
        add(p, 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) {
                producer = new RandomCharacterGenerator();
                displayCanvas.setCharacterSource(producer);
                producer.start();
                displayCanvas.setDone(false);
                Thread t = new Thread(displayCanvas);
                t.start();
                startButton.setEnabled(false);
                stopButton.setEnabled(true);
                feedbackCanvas.setEnabled(true);
                feedbackCanvas.requestFocus();
            }
        });
        stopButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                startButton.setEnabled(true);
                stopButton.setEnabled(false);
                producer.setDone();
                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);