/** Use JWS to modify MyApplet9.java (previously SimpleApplet7.java) 
 ** by changing it to definitely have some different (weird?) behavior 
 ** to entertain (befuddle?) the user.  Also, edit MyApplet9.html in 
 ** the project directory, both to run the MyApplet9.class file you 
 ** create, and so that everything looks nice and works properly.  **/

/** USE EXISTING JAVA CLASSES **/

import java.awt.*;
import java.applet.Applet;

/** EXTEND THE JAVA APPLET CLASS **/

public class MyApplet9 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.black);}             // 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).
	 }
	 
     /** 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;}      
	 }
	 
	 
     /** 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.red);
	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, out of ink!!",   // then print this instead of any
			    endpoint[i].x, endpoint[i].y);}  // extra line.
	   }
	if (connectpoint != anchorpoint)                     // Paint a temporary purple line 
	 {g.setColor(Color.green);                         // in response to a mouseDrag event.
	  g.drawLine(anchorpoint.y, anchorpoint.x, 
		      connectpoint.y, connectpoint.x);}
	}
    }		 