// Spots2.java
// Demos mousePressed events
// Clears screen each time repaint is called
// APPLET CODE="Spots2" HEIGHT=250 WIDTH=250
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Spots2 extends JApplet {
	final int MAXSPOTS = 10;
	int xSpot;
	int ySpot;
	int currSpot = 0;

	public void init( ) {
		xSpot = -10;
		ySpot = -10;
		setBackground( Color.white );

		// add anonymous inner class to applet to listen for mousePressed event
		addMouseListener( 
			new MouseAdapter( ) {
				public void mousePressed( MouseEvent e ) {
					if ( currSpot < MAXSPOTS ) {
						showStatus( "Mouse down at " + e.getX( ) + ", " + e.getY( ) );
						xSpot = e.getX( );
						ySpot = e.getY( );
						currSpot++;
						repaint( );
					}
					else {
						showStatus( "Too many spots." );
					}
				}
			}
		);
	}

	public void paint( Graphics g ) {
		// erase screen with white rectangle
		g.setColor( Color.white );
		g.fillRect( 0, 0, 250, 250 );

		// draw spot in blue
		g.setColor( Color.blue );
		g.fillOval( xSpot - 10, ySpot - 10, 20, 20 );
	}
}