import java.awt.*; /** * A canvas that knows how to paint a monogram. * Contract: * will paint when asked * will update monogram when asked */ public class AlphaCanvas extends Canvas { /** * What to paint. */ protected SafeMonogram mono = new SafeMonogram( 'l', 's' ); /** * Set up by telling a smarts to look out for me and telling Java * about the keyListener that will handle my events. */ public AlphaCanvas() { super(); AlphaSmarts smarts = new AlphaSmarts( this ); this.addKeyListener( new SimpleKeyListener( smarts ) ); } /** * How to tell me if you want my monogram to change. * Note: also tells Java I need to be repainted. */ public void setText( SafeMonogram m ) { this.mono = m; this.repaint(); } /** * How Java tells me it's time to paint myself. */ public void paint( Graphics g ) { g.drawChars( this.mono.getChars(),0,2,10,10); } /** * Stuff to tell Java what size I like to be. */ public Dimension getPreferredSize() { return new Dimension( 20, 20 ); } }