/** MyApplet8.java: Interactive mouseDown Events ** Assignment: Modify this file centering the click ** instruction at the top of the applet, and 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, take a look at the MyApplet8.html webpage ** file for displaying this applet. **/ /** USE EXISTING JAVA CLASSES **/ import java.awt.*; import java.applet.Applet; /** EXTEND THE JAVA APPLET CLASS **/ public class MyApplet8 extends Applet { Font font = new Font("TimesRoman", Font.BOLD, 18); 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 { // (e.g. 0-24) is less than the addspot(x,y); // max number of events (e.g. 25), return true; // then call the addspot method. } else { 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) { g.setColor(Color.blue); g.setFont(font); g.drawString("Click in Window", 100, 20); for (int i = 0; i < curevent; i++) // Note that nothing happens here, until a { // mouseDown event occurs. Repaint all spots if (i < maxnumspots) // from 0 to maxnumspots-1, each { // centered on the mouse pointer. g.fillOval(xevent[i] - 10,yevent[i] - 10,20,20); } else { g.setColor(Color.red); // Also, if curevent > maxnumspots, then print g.drawString("Ouch, ouch, too many spots !!",xevent[i],yevent[i]); // this too. } } } }