FileDocCategorySizeDatePackage
AnimatedCharacterDisplayCanvas.javaAPI DocExample1960Sat Apr 17 22:01:50 BST 2004javathreads.examples.ch08.example1

AnimatedCharacterDisplayCanvas

public class AnimatedCharacterDisplayCanvas extends CharacterDisplayCanvas implements Runnable, CharacterListener

Fields Summary
private volatile boolean
done
private int
curX
private Lock
lock
private Condition
cv
private Thread
timer
Constructors Summary
public AnimatedCharacterDisplayCanvas(CharacterSource cs)


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

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

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

        try {
            lock.lock();
            while (true) {
                try {
                    if (done) {
                        cv.await();
                  } else {
                      repaint();
                      cv.await(100, TimeUnit.MILLISECONDS);
                  }
               } catch (InterruptedException ie) {
                   return;
               }
           }
        } finally {
            lock.unlock();
        }
    
public voidsetDone(boolean b)

        try {
            lock.lock();
            done = b;
            if (timer == null) {
                timer = new Thread(this);
                timer.start();
           }
           if (!done) cv.signal();
        } finally {
            lock.unlock();
        }