/** Copyright (c) 1997 Sams.net Publishing. All Rights Reserved.
 ** Teach Yourself Java 1.1, Laura Lemay and Charles L. Perkins.
 ** Adapted from Listing 11.1:  The Spots applet.
 **
 ** Modified to SimpleApplet6 by John Pais, December 1997.   **/
 
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:  SimpleApplet6.html

/** USE EXISTING JAVA CLASSES **/

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

/** EXTEND THE JAVA APPLET CLASS **/

public class SimpleApplet6 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.
                                                                               // 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;}
         }
 
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:  SimpleApplet6.html
 
     /** REPAINT THE FRAME USING THE ADD SPOT 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)
       {g.setColor(Color.blue);
         for (int i = 0; i < curevent; i++)                              // Note that nothing happens here, until a
           {if (i < maxnumspots)                                         // mouseDown event occurs. Repaint all
             {g.fillOval(xevent[i] - 10,yevent[i] - 10,20,20);}  // spots from 0 to maxnumspots-1, each
            else                                                                  // centered on the mouse pointer.
             {g.setColor(Color.red);                                     // Also, if curevent > maxnumspots, then
               g.drawString("Ouch, ouch, too many spots !!",xevent[i],yevent[i]);}  // print this too.
            }
         }
     }