001         package com.croftsoft.apps.scribble;
002    
003         import java.applet.*;
004         import java.awt.*;
005    
006         import com.croftsoft.core.lang.lifecycle.Lifecycle;
007    
008         /*********************************************************************
009         * Use your mouse to draw on a canvas in multiple colors.
010         *
011         * @see
012         *   Java in a Nutshell, First Edition
013         *
014         * @version
015         *   2002-02-27
016         * @since
017         *   1996
018         * @author
019         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
020         *********************************************************************/
021    
022         public final class  Scribble
023           extends Applet
024           implements Lifecycle
025         //////////////////////////////////////////////////////////////////////
026         //////////////////////////////////////////////////////////////////////
027         {
028    
029            private int last_x = 0;
030            private int last_y = 0;
031            private Color current_color = Color.black;
032            private Button clear_button;
033            private Choice color_choices;
034    
035    
036        public void init() {
037            this.setBackground ( Color.white );
038            clear_button = new Button ( "Clear" );
039         clear_button.setForeground ( Color.magenta );
040         clear_button.setBackground ( Color.black );
041            this.add ( clear_button );
042            color_choices = new Choice ( );
043            color_choices.addItem ( "black" );
044            color_choices.addItem ( "red" );
045            color_choices.addItem ( "yellow" );
046            color_choices.addItem ( "green" );
047            color_choices.setForeground ( Color.black );
048            color_choices.setBackground ( Color.lightGray );
049            this.add ( new Label ( "Color:  " ) );
050            this.add ( color_choices );
051        }
052    
053        public boolean mouseDown ( Event e, int x, int y ) {
054            last_x = x;
055            last_y = y;
056            return true;
057        }
058    
059        public boolean mouseDrag ( Event e, int x, int y ) {
060            Graphics g = this.getGraphics ( );
061            g.setColor ( current_color );
062            g.drawLine ( last_x, last_y, x, y );
063            last_x = x;
064            last_y = y;
065            return true;
066        }
067    
068        public boolean action ( Event event, Object arg ) {
069            if ( event.target == clear_button ) {
070                    Graphics g = this.getGraphics ( );
071                    Rectangle r = this.bounds ( );
072                    g.setColor ( this.getBackground ( ) );
073                    g.fillRect ( r.x, r.y, r.width, r.height );
074                    return true;
075            }
076            else if ( event.target == color_choices ) {
077                    if ( arg.equals ( "black" ) ) current_color = Color.black;
078                    else if ( arg.equals ( "red" ) ) current_color = Color.red;
079                    else if ( arg.equals ( "yellow" ) ) current_color = Color.yellow;
080                    else if ( arg.equals ( "green" ) ) current_color = Color.green;
081                    return true;
082            }
083            else return super.action ( event, arg );
084        }
085    
086    }