// ClickingsAWTApplet.java (c) 2000-2006 Kari Laitinen // http://www.naturalprogramming.com/ // 2000-11-02 First program version created. // 2006-10-18 Last modification // This program demonstrates how a Java program can react to // mouse events. The following is the mechanism to react to // mouse events: // -- this program implements the MouseListener interface which // specifies a set of methods that are called automatically // by the program execution system // -- in the init() method the ClickingsAWTApplet object informs the // program execution system that it will listen to mouse events // -- this program provides either dummy or non-dummy implementations // for all the methods specified in the MouseListener interface import java.awt.* ; import java.awt.event.* ; public class ClickingsAWTApplet extends java.applet.Applet implements MouseListener { final int MAXIMUM_NUMBER_OF_CLICKINGS = 100 ; int current_number_of_clickings = 0 ; int x_coordinates_of_clickings[] = new int[ MAXIMUM_NUMBER_OF_CLICKINGS ] ; int y_coordinates_of_clickings[] = new int[ MAXIMUM_NUMBER_OF_CLICKINGS ] ; public void init() { setBackground( Color.white ) ; addMouseListener( this ) ; } // The following dummy methods are needed // to fully implement the MouseListener interface. public void mouseClicked( MouseEvent event ) {} public void mouseEntered( MouseEvent event ) {} public void mouseExited( MouseEvent event ) {} public void mouseReleased( MouseEvent event ) {} // The following method will be called automatically by the // program execution system when a mouse button is preesed // down. The MouseEvent object that is received as a parameter // contains information related to the mouse event. public void mousePressed( MouseEvent event ) { if ( current_number_of_clickings < MAXIMUM_NUMBER_OF_CLICKINGS ) { x_coordinates_of_clickings[ current_number_of_clickings ] = event.getX() ; y_coordinates_of_clickings[ current_number_of_clickings ] = event.getY() ; current_number_of_clickings ++ ; repaint() ; } } public void paint( Graphics graphics ) { for ( int clicking_index = 0 ; clicking_index < current_number_of_clickings ; clicking_index ++ ) { graphics.fillOval( x_coordinates_of_clickings[ clicking_index ] - 3, y_coordinates_of_clickings[ clicking_index ] - 3, 6, 6 ) ; graphics.drawString( "(" + x_coordinates_of_clickings[ clicking_index ] + "," + y_coordinates_of_clickings[ clicking_index ] + ")", x_coordinates_of_clickings[ clicking_index ] + 7, y_coordinates_of_clickings[ clicking_index ] ) ; } } }