FileDocCategorySizeDatePackage
AnimatedCharacterDisplayCanvas.javaAPI DocExample1761Sun Apr 18 22:43:48 BST 2004javathreads.examples.ch04.example2

AnimatedCharacterDisplayCanvas

public class AnimatedCharacterDisplayCanvas extends CharacterDisplayCanvas implements Runnable, CharacterListener

Fields Summary
private boolean
done
private int
curX
private Thread
timer
private Object
doneLock
Constructors Summary
public AnimatedCharacterDisplayCanvas()


      
    
public AnimatedCharacterDisplayCanvas(CharacterSource cs)

        super(cs);
    
Methods Summary
public synchronized voidnewCharacter(CharacterEvent ce)

        curX = 0;
        tmpChar[0] = (char) ce.character;
        repaint();
    
protected synchronized voidpaintComponent(java.awt.Graphics gc)

        Dimension d = getSize();
        gc.clearRect(0, 0, d.width, d.height);
        if (tmpChar[0] == 0)
            return;
        int charWidth = fm.charWidth(tmpChar[0]);
        gc.drawChars(tmpChar, 0, 1,
                     curX++, fontHeight);
    
public voidrun()

	synchronized(doneLock) {
            while (true) {
                try {
                    if (done) {
                        doneLock.wait();
                    } else {
                        repaint();
                        doneLock.wait(100);
                    }
                } catch (InterruptedException ie) {
                    return;
                }
            }
	}
    
public voidsetDone(boolean b)

	synchronized(doneLock) {
            done = b;

            if (timer == null) {
                timer = new Thread(this);
                timer.start();
            }
            if (!done)
                doneLock.notify();
	}