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

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

public class ColorScribble11 extends Applet
          implements ItemListener, ActionListener, MouseListener, 
	                                               MouseMotionListener
{
    private int lastx;
    private int lasty;
    private Button clearButton;
    private Image drawing;
    private Graphics drawGraph;
    private Choice colorChoice;
    private Color currentColor;
    private int width;
    private 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. register event listeners for Button, Choice, and Mouse

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

//  save the position when the user presses the mouse button

    public void mousePressed(MouseEvent e)
    {
        lastx = e.getX();
        lasty = e.getY();
    }

    public void mouseClicked(MouseEvent e) { }
    public void mouseEntered(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }
    public void mouseReleased(MouseEvent e) { }

//  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;
        }
    }

//  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();
    }

    public void mouseMoved(MouseEvent e) { }

//  clear the display when the user clicks on the clear button

    public void actionPerformed(ActionEvent e)
    {
        if (e.getActionCommand().equals("Clear"))
            clear();
    }

//  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);
    }

}