/** 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 
    int rand1, rand2, rand3, rand4;
						      // current array entry number,
						      // starting with 0 (instead of 1).
    public void init()	                      
       {setBackground(Color.green);}             // 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.blue);
	for (int i = 0; i < entrynum; i++)                   // Note that nothing happens here, 
	  {if (i < maxnumlines)                              // until a mouseDown event occurs.  
	    {rand1 = (int) Math.floor(Math.random()*60);
	    rand2 = (int) Math.floor(Math.random()*60);
	    rand3 = (int) Math.floor(Math.random()*60);
	    rand4 = (int) Math.floor(Math.random()*60);
	    g.drawLine(startpoint[i].x + rand1, startpoint[i].y - rand2,    // Repaint all lines from 0 to
			    endpoint[i].x - rand3, endpoint[i].y + rand4);}  // maxnumlines-1.    
	   else                                               
	    {g.setColor(Color.red);                          // Also, if entrynum > maxnumlines, 
	     g.drawString("Getting Confused !",   // then print this instead of any
			    endpoint[i].x, endpoint[i].y);  // extra line.
	     g.drawString("To many jumpy lines !", endpoint[i].x, endpoint[i].y + 15);}
	   }
	if (connectpoint != anchorpoint)                     // Paint a temporary purple line 
	 {rand1 = (int) Math.floor(Math.random()*60);
	  rand2 = (int) Math.floor(Math.random()*60);
	  rand3 = (int) Math.floor(Math.random()*60);
	  rand4 = (int) Math.floor(Math.random()*60);
	 g.setColor(Color.magenta);                         // in response to a mouseDrag event.
	  g.drawLine(anchorpoint.x + rand1, anchorpoint.y + rand2, 
		      connectpoint.x - rand3, connectpoint.y - rand4);}
	}
    }		 