FileDocCategorySizeDatePackage
CharacterDisplayCanvas.javaAPI DocExample1345Mon Apr 05 12:57:24 BST 2004javathreads.examples.ch05

CharacterDisplayCanvas

public class CharacterDisplayCanvas extends JComponent implements CharacterListener

Fields Summary
protected FontMetrics
fm
protected char[]
tmpChar
protected int
fontHeight
Constructors Summary
public CharacterDisplayCanvas()


      
        setFont(new Font("Monospaced", Font.BOLD, 18));
        fm = Toolkit.getDefaultToolkit().getFontMetrics(getFont());
        fontHeight = fm.getHeight();
    
public CharacterDisplayCanvas(CharacterSource cs)

        this();
        setCharacterSource(cs);
    
Methods Summary
public voidnewCharacter(CharacterEvent ce)

        tmpChar[0] = (char) ce.character;
        repaint();
    
protected 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((int) tmpChar[0]);
        gc.drawChars(tmpChar, 0, 1,
                     (d.width - charWidth) / 2, fontHeight);
    
public java.awt.DimensionpreferredSize()

        return new Dimension(fm.getMaxAscent() + 10,
                             fm.getMaxAdvance() + 10);
    
public voidsetCharacterSource(CharacterSource cs)

        cs.addCharacterListener(this);