FileDocCategorySizeDatePackage
AnimatedCharacterDisplayCanvas.javaAPI DocExample1924Mon Apr 05 13:37:08 BST 2004javathreads.examples.ch05.example2

AnimatedCharacterDisplayCanvas

public class AnimatedCharacterDisplayCanvas extends CharacterDisplayCanvas implements Runnable, CharacterListener

Fields Summary
private AtomicBoolean
done
private AtomicInteger
curX
private AtomicInteger
tempChar
private Thread
timer
Constructors Summary
public AnimatedCharacterDisplayCanvas()


      
        startAnimationThread();
    
public AnimatedCharacterDisplayCanvas(CharacterSource cs)

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

        curX.set(0);
        tempChar.set(ce.character);
        repaint();
    
protected voidpaintComponent(java.awt.Graphics gc)

        char[] localTmpChar = new char[1];
        localTmpChar[0] = (char) tempChar.get();
        int localCurX = curX.get();

        Dimension d = getSize();
        int charWidth = fm.charWidth(localTmpChar[0]);
        gc.clearRect(0, 0, d.width, d.height);
        if (localTmpChar[0] == 0)
            return;

        gc.drawChars(localTmpChar, 0, 1,
                     localCurX, fontHeight);
        curX.getAndIncrement();
    
public voidrun()

        while (true) {
            try {
                Thread.sleep(100);
                if (!done.get()) {
                    repaint();
                }
            } catch (InterruptedException ie) {
                return;
            }
        }
    
public voidsetDone(boolean b)

        done.set(b);
    
private voidstartAnimationThread()

        if (timer == null) {
            timer = new Thread(this);
            timer.start();
        }