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 |
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.
}
}
}