Color Scribble Applet


//  this applet allows the user to draw by dragging the mouse
//  the user can choose the drawing color from the Choice box

//  inner classes are used for handling button clicks and mouse
//  events. inner classes should be able to access all fields and
//  methods of the enclosing class, even if they are private.
//  netscape 4.5 won't allow the inner classes to access private
//  fields of the enclosing class, so all fields have been changed
//  to package visibility.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class ColorScribbleInner11 extends Applet implements ItemListener {
    int lastx;
    int lasty;
    Button clearButton;
    Image drawing;
    Graphics drawGraph;
    Choice colorChoice;
    Color currentColor;
    int width;
    int height;

//  create the clear button and the choice box with the list of drawing colors
//  add these two components to the applet. get the size of the applet.

    public void init()
    {
        clearButton = new Button("Clear");
        this.add(clearButton);
        colorChoice = new Choice();
        colorChoice.addItem("Black");
        colorChoice.addItem("Blue");
        colorChoice.addItem("Red");
        colorChoice.addItem("Yellow");
        add(colorChoice);
        width = getSize().width;
        height = getSize().height;
        colorChoice.addItemListener(this);

//  use anonymous inner class to handle button clicks
        clearButton.addActionListener(new ActionListener() {
        //  clear the display when the user clicks on the clear button
            public void actionPerformed(ActionEvent e)
            {
                if (e.getActionCommand().equals("Clear"))
                    clear();
            }
        });

//  use anonymous inner class to handle mouse moves
        this.addMouseMotionListener(new MouseMotionAdapter() {
        //  draw a line from the previous position to the current position when the user
        //  drags the mouse. also save the new position
            public void mouseDragged(MouseEvent e)
            {
                drawGraph.drawLine(lastx,lasty,e.getX(),e.getY());
                repaint();
                lastx = e.getX();
                lasty = e.getY();
            }
        });

//  use anonymous inner class to handle mouse clicks
        this.addMouseListener(new MouseAdapter() {
        //  save the position when the user presses the mouse button
            public void mousePressed(MouseEvent e)
            {
                lastx = e.getX();
                lasty = e.getY();
            }
        });
    }

//  change the color if the user has chosen a new one

    public void itemStateChanged(ItemEvent e)
    {
        String choice = colorChoice.getSelectedItem();
        if (choice.equals("Black"))
        {
            drawGraph.setColor(Color.black);
            currentColor = Color.black;
        }
        if (choice.equals("Blue"))
        {
            drawGraph.setColor(Color.blue);
            currentColor = Color.blue;
        }
        if (choice.equals("Red"))
        {
            drawGraph.setColor(Color.red);
            currentColor = Color.red;
        }
        if (choice.equals("Yellow"))
        {
            drawGraph.setColor(Color.yellow);
            currentColor = Color.yellow;
        }
    }


//  clear the offscreen drawing area and redraw border, then redraw on screen

    public void clear()
    {
        drawGraph.clearRect(0,0,size().width,size().height);
        drawGraph.setColor(Color.black);
        drawGraph.drawRect(0,0,width-1,height-1);
        drawGraph.drawRect(1,1,width-3,height-3);
        drawGraph.setColor(currentColor);
        repaint();
    }

    public void update(Graphics g)
    {
        paint(g);
    }

//  create the offscreen drawing image and draw rectangle around it if necessary
//  draw the offscreen image on the screen

    public void paint(Graphics g)
    {
        if (drawing == null)
        {
            drawing = createImage(width,height);
            drawGraph = drawing.getGraphics();
            drawGraph.setColor(Color.black);
            drawGraph.drawRect(0,0,width-1,height-1);
            drawGraph.drawRect(1,1,width-3,height-3);
            drawGraph.setColor(currentColor);
        }
        g.drawImage(drawing,0,0,null);
    }

}