/** Use JWS to modify MyApplet8.java (previously SimpleApplet6.java) 
 ** by changing it to paint a different colored shape not centered on 
 ** the tip of the pointer, in response to mouseDown events. You might 
 ** also decide to add some other unusual (weird?) behavior to entertain 
 ** (befuddle?) the user.  Also, edit MyApplet8.html in the  project 
 ** directory, both to run the MyApplet8.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 MyApplet8 extends Applet 
   {final int maxnumspots = 20;                       // Define max number of spots,
    final int maxnumevents = maxnumspots + 5;         // max number of events, 
    int xevent[] = new int[maxnumevents];             // array of xcoords. of events,
    int yevent[] = new int[maxnumevents];             // array of ycoords. of events, 
    int curevent = 0;                                 // and the current event number. 
    Font font1 = new Font("TimesRoman",Font.BOLD,30);
    int r,g,b;
    int rand1, rand2;
    						      // Note that Java starts numbering
    public void init()	                              // with 0, instead of 1.
       {setBackground(Color.white);}             // Change the gray background to white.
       
    /** HANDLE MOUSEDOWN EVENT **/    
    
     public boolean mouseDown(Event e, int x, int y)
	{if (curevent < maxnumevents)                 // If the current event number
	  {addspot(x,y);                              // (e.g. 0-24) is less than the 
	   return true;}                              // max number of events (e.g. 25),
	 else                                         // then call the addspot method.
	  {return false;}      
	 }
	
     /** REPAINT THE FRAME USING THE ADDSPOT METHOD **/   
     
     void addspot(int x, int y)
	 {xevent[curevent] = x;                        // Record the xcoord. and ycoord.
	  yevent[curevent] = y;                        // of the current event.
	  curevent++;                                  // Add 1 to the current event number.
	  repaint();                                   // Call the paint method.
	 }
	      
    /** CREATE THE CURRENT FRAME **/

    public void paint(Graphics g)
     {Dimension d = size();
      g.setColor(Color.blue);
       if (curevent != 25)	
	{for (int i = 0; i < curevent; i++)    // Note that nothing happens here, until a
	  if (i < maxnumspots)               // mouseDown event occurs. Repaint all spots 
	   {rand1 = (int) Math.floor(Math.random()*10 + 20);
	    rand2 = (int) Math.floor(Math.random()*20 + 10);
	    g.fillOval(xevent[i] - 10,yevent[i] - 10,rand1,rand2);} // from 0 to maxnumspots-1, each
	  else                                                // centered on the mouse pointer.
	   {if (i < maxnumspots + 4)
	       {g.setColor(Color.red);           // Also, if curevent > maxnumspots, then print
		g.drawString("Ouch, ouch, too many spots !!",xevent[i],yevent[i]);  // this too.
	       }
	   }
	}  
	     						      
	else if (curevent == 25)
	       {//g.setBackground(Color.black);
	       g.setColor(Color.black);      
	       g.fillRect(0,0,d.width,d.height);    
	       g.setColor(Color.red);
	       g.setFont(font1);
	       g.drawString("I warned you.", 60, 75);
	       g.drawString("You aren't clicking", 60, 105);
	       g.drawString("any more.", 60, 135);
	       }
	     }
       }