Draw Lines Applet


//  this applet allows the user to draw up to 10 lines by dragging the mouse
//  drawing is done on an offscreen Image to reduce flicker

//  inner classes are used for handling 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.awt.Graphics;
import java.awt.Image;
import java.awt.Color;
import java.awt.Point;
import java.applet.AudioClip;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionAdapter;

public class LinesInner11 extends java.applet.Applet
{
    final int MAXLINES = 10;
    Point[] starts;
    Point[] ends;
    Point anchor;
    Point currentpoint;
    int currlines;
    AudioClip tooMany;
    Image drawing;
    Graphics drawGraph;
    int width;
    int height;

//  allocate the array of Points, set the background color, load the error sound
//  save the size of the applet, and register to listen for mouse events

    public void init() {
        width = getSize().width;
        height = getSize().height;
        starts = new Point[MAXLINES];
        ends = new Point[MAXLINES];
        setBackground(Color.white);
        currlines = 0;
        tooMany = getAudioClip(getCodeBase(),"bong.au");

//  use inner class to handle mouse events
        this.addMouseListener(new MyMouseAdapter());

//  use inner class to handle mouse motion events
        this.addMouseMotionListener(new MyMouseMotionAdapter());
    }  //  end of init()

//  inner class to handle mouse events

    class MyMouseAdapter extends MouseAdapter {
    //  start a new line when the user depresses the mouse
        public void mousePressed(MouseEvent e)
        {
            anchor = new Point(e.getX(),e.getY());
            repaint();
        }
    //  add the new line when the user releases the mouse button
        public void mouseReleased(MouseEvent e)
        {
            if (currlines < MAXLINES)
                addline(e.getX(),e.getY());
            else {
                tooMany.play();
                currentpoint = null;
                repaint();
            }
        }
    }

//  inner class to handle mouse motion events

    class MyMouseMotionAdapter extends MouseMotionAdapter {
    //  draw the potential line as the user drags the mouse around
        public void mouseDragged(MouseEvent e)
        {
            currentpoint = new Point(e.getX(),e.getY());
            repaint();
        }
    }

//  add new line to array of permanent lines

    void addline(int x, int y) {
        starts[currlines] = anchor;
        ends[currlines] = currentpoint;
        currlines++;
        currentpoint = null;
        repaint();
    }

//  override update to remove call to clear applet

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

//  create the offscreen drawing image, draw rectangle around it,
//  and draw the lines (with potential line in blue)
//  draw the offscreen image on the screen

    public void paint(Graphics g)
    {
        if (drawing == null)
        {
            drawing = createImage(width,height);
            drawGraph = drawing.getGraphics();
        }

//  clear the Image, then draw the border

        drawGraph.setColor(Color.black);
        drawGraph.clearRect(0,0,width,height);
        drawGraph.drawRect(0,0,width-1,height-1);
        drawGraph.drawRect(1,1,width-3,height-3);

//  draw the permanent lines, then draw the potential line in blue

        for (int i = 0 ; i < currlines ; i++)
            drawGraph.drawLine(starts[i].x,starts[i].y,ends[i].x,ends[i].y);
        drawGraph.setColor(Color.blue);
        if (currentpoint != null)
            drawGraph.drawLine(anchor.x,anchor.y,currentpoint.x,currentpoint.y);

//  transfer the completed Image onto the Graphics object for the applet

        g.drawImage(drawing,0,0,null);
    }


}