/** Copyright (c) 1997 Sams.net Publishing. All Rights Reserved.
 ** Teach Yourself Java 1.1, Laura Lemay and Charles L. Perkins.
 ** Adapted from Listing 11.2:  The Lines applet.
 **
 ** Modified to SimpleApplet7 by John Pais, January 1998.    **/
 
Toggle your browser's Back and Forward buttons to compare the running applet to this code.
Click here to run a fresh copy of the applet:  SimpleApplet7.html

/** USE EXISTING JAVA CLASSES **/

import java.awt.*;
import java.applet.Applet;

/** EXTEND THE JAVA APPLET CLASS **/

public class SimpleApplet7 extends Applet
   {final int maxnumlines = 20;                               // Define max number of lines,
     final int maxsize = maxnumlines + 5;                 // max array size,
     Point startpoint[] = new Point[maxsize];           // the array of start points,
     Point endpoint[] = new Point[maxsize];            // the array of end points,
     Point anchorpoint, connectpoint;                      // the current anchor point and
     int entrynum = 0;                                             // connection point, and the
                                                                           // current array entry number,
                                                                           // starting with 0 (instead of 1).
     public void init()
        {setBackground(Color.white);}             // Change the gray background to white.
 
     /** HANDLE THE MOUSEDOWN EVENT **/
 
     public boolean mouseDown(Event e, int x, int y)
        {if (entrynum < maxsize)
          {anchorpoint = new Point(x,y);                    // Initialize both points to
            connectpoint = anchorpoint;                       // the current mouse position.
            return true;}                                               // Note that this will give us a
         else                                                               // way to prevent painting a single
          {return false;}                                              // point (see the mouseUp method).
          }
 
Toggle your browser's Back and Forward buttons to compare the running applet to this code.
Click here to run a fresh copy of the applet:  SimpleApplet7.html
 
     /** HANDLE THE MOUSEDRAG EVENT **/
 
     public boolean mouseDrag(Event e, int x, int y)
        {if (entrynum < maxsize)                                // A mouseDrag event occurs only
          {connectpoint = new Point(x,y);                   // when the mouse is moved. So,
            repaint();                                                   // we must have connectpoint !=
            return true;}                                              // anchorpoint, which ensures that
         else                                                              // a temporary (purple) line will
          {return false;}                                             // be painted (see the paint method).
          }
 
     /** HANDLE THE MOUSEUP EVENT **/
 
     public boolean mouseUp(Event e, int x, int y)
        {if (entrynum < maxsize && connectpoint != anchorpoint)   // Don't add a single point.
          {addline();                                                                        // Add new points to the arrays.
            repaint();                                                                        // Repaint everything, including
            return true;}                                                                   // the new line (all blue).
         else
          {return false;}
          }
 
Toggle your browser's Back and Forward buttons to compare the running applet to this code.
Click here to run a fresh copy of the applet:  SimpleApplet7.html
 
     /** THE ADDLINE METHOD **/
 
     void addline()
        {startpoint[entrynum] = anchorpoint;            // Record the startpoint and endpoint
          endpoint[entrynum] = connectpoint;           // of the new line.
          entrynum++;                                             // Add 1 to the current entry number.
          anchorpoint = null;                                    // Nullifying these points prevents the
          connectpoint = null;                                   // line from being painted twice (see
          }                                                              // the paint method).
 
    /** CREATE THE CURRENT FRAME **/

    public void paint(Graphics g)
       {g.setColor(Color.blue);
         for (int i = 0; i < entrynum; i++)                             // Note that nothing happens here,
           {if (i < maxnumlines)                                           // until a mouseDown event occurs.
             {g.drawLine(startpoint[i].x, startpoint[i].y,         // Repaint all lines from 0 to
                                  endpoint[i].x, endpoint[i].y);}       // maxnumlines-1.
            else
             {g.setColor(Color.red);                                     // Also, if entrynum > maxnumlines,
               g.drawString("Sorry, line quota exceeded !!",   // then print this instead of any
               endpoint[i].x, endpoint[i].y);}                           // extra line.
             }
         if (connectpoint != anchorpoint)                             // Paint a temporary purple line in
          {g.setColor(Color.magenta);                                 // response to a mouseDrag event.
            g.drawLine(anchorpoint.x, anchorpoint.y,
                                connectpoint.x, connectpoint.y);}
         }
    }
 
Toggle your browser's Back and Forward buttons to compare the running applet to this code.
Click here to run a fresh copy of the applet:  SimpleApplet7.html