[ Home | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 ]

Exercise 4: Mouse Input

The source code:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Mouse1 extends Applet
   implements MouseListener, MouseMotionListener {

   int width, height;
   int mx, my;  // the mouse coordinates
   boolean isButtonPressed = false;

   public void init() {
      width = getSize().width;
      height = getSize().height;
      setBackground( Color.black );

      mx = width/2;
      my = height/2;

      addMouseListener( this );
      addMouseMotionListener( this );
   }

   public void mouseEntered( MouseEvent e ) {
      // called when the pointer enters the applet's rectangular area
   }
   public void mouseExited( MouseEvent e ) {
      // called when the pointer leaves the applet's rectangular area
   }
   public void mouseClicked( MouseEvent e ) {
      // called after a press and release of a mouse button
      // with no motion in between
      // (If the user presses, drags, and then releases, there will be
      // no click event generated.)
   }
   public void mousePressed( MouseEvent e ) {  // called after a button is pressed down
      isButtonPressed = true;
      setBackground( Color.gray );
      repaint();
      // "Consume" the event so it won't be processed in the
      // default manner by the source which generated it.
      e.consume();
   }
   public void mouseReleased( MouseEvent e ) {  // called after a button is released
      isButtonPressed = false;
      setBackground( Color.black );
      repaint();
      e.consume();
   }
   public void mouseMoved( MouseEvent e ) {  // called during motion when no buttons are down
      mx = e.getX();
      my = e.getY();
      showStatus( "Mouse at (" + mx + "," + my + ")" );
      repaint();
      e.consume();
   }
   public void mouseDragged( MouseEvent e ) {  // called during motion with buttons down
      mx = e.getX();
      my = e.getY();
      showStatus( "Mouse at (" + mx + "," + my + ")" );
      repaint();
      e.consume();
   }

   public void paint( Graphics g ) {
      if ( isButtonPressed ) {
         g.setColor( Color.black );
      }
      else {
         g.setColor( Color.gray );
      }
      g.fillRect( mx-20, my-20, 40, 40 );
   }
}

Try clicking and dragging on the resulting applet. Notice how the status bar in your web browser displays the current mouse position -- that's due to the calls to showStatus(). (You might see some occasional flickering in this applet. This problem will be addressed in an upcoming lesson.)

( You need to enable Java to see this applet. )

The MouseEvent data that gets passed into each of the mouse*() functions contains information on the position of the mouse, the state of the mouse buttons and modifier keys (i.e. the Shift, Alt, Ctrl, and Meta keys), the time at which the event occurred, etc. To find out how to access this information, go here.

Another example:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Mouse2 extends Applet
   implements MouseListener, MouseMotionListener {

   int width, height;
   int x, y;    // the coordinates of the upper-left corner of the box
   int mx, my;  // the most recently recorded mouse coordinates
   boolean isMouseDraggingBox = false;

   public void init() {
      width = getSize().width;
      height = getSize().height;
      setBackground( Color.black );

      x = width/2 - 20;
      y = height/2 - 20;

      addMouseListener( this );
      addMouseMotionListener( this );
   }

   public void mouseEntered( MouseEvent e ) { }
   public void mouseExited( MouseEvent e ) { }
   public void mouseClicked( MouseEvent e ) { }
   public void mousePressed( MouseEvent e ) {
      mx = e.getX();
      my = e.getY();
      if ( x < mx && mx < x+40 && y < my && my < y+40 ) {
         isMouseDraggingBox = true;
      }
      e.consume();
   }
   public void mouseReleased( MouseEvent e ) {
      isMouseDraggingBox = false;
      e.consume();
   }
   public void mouseMoved( MouseEvent e ) { }
   public void mouseDragged( MouseEvent e ) {
      if ( isMouseDraggingBox ) {
         // get the latest mouse position
         int new_mx = e.getX();
         int new_my = e.getY();

         // displace the box by the distance the mouse moved since the last event
         // Note that "x += ...;" is just shorthand for "x = x + ...;"
         x += new_mx - mx;
         y += new_my - my;

         // update our data
         mx = new_mx;
         my = new_my;

         repaint();
         e.consume();
      }
   }

   public void paint( Graphics g ) {
      g.setColor( Color.gray );
      g.fillRect( x, y, 40, 40 );
   }
}

Try clicking and dragging on the gray square:

( You need to enable Java to see this applet. )

A third example:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Mouse3 extends Applet
   implements MouseListener, MouseMotionListener {

   int width, height;

   // We need a place to store a list of mouse positions.
   // Rather than use an array, we'll use a Vector, because
   // it allows elements to be easily appended and deleted.
   // (Technically, it would probably be more appropriate to
   // use a LinkedList, but they're only supported by Java 1.2)
   Vector listOfPositions;

   public void init() {
      width = getSize().width;
      height = getSize().height;
      setBackground( Color.black );

      listOfPositions = new Vector();

      addMouseListener( this );
      addMouseMotionListener( this );
   }

   public void mouseEntered( MouseEvent e ) { }
   public void mouseExited( MouseEvent e ) { }
   public void mouseClicked( MouseEvent e ) { }
   public void mousePressed( MouseEvent e ) { }
   public void mouseReleased( MouseEvent e ) { }
   public void mouseMoved( MouseEvent e ) {

      if ( listOfPositions.size() >= 50 ) {
         // delete the first element in the list
         listOfPositions.removeElementAt( 0 );
      }

      // add the new position to the end of the list
      listOfPositions.addElement( new Point( e.getX(), e.getY() ) );

      repaint();
      e.consume();
   }
   public void mouseDragged( MouseEvent e ) { }

   public void paint( Graphics g ) {
      g.setColor( Color.white );
      for ( int j = 1; j < listOfPositions.size(); ++j ) {
         Point A = (Point)(listOfPositions.elementAt(j-1));
         Point B = (Point)(listOfPositions.elementAt(j));
         g.drawLine( A.x, A.y, B.x, B.y );
      }
   }
}

Move freely over the applet. Notice that moving faster makes the line stretch out longer.

( You need to enable Java to see this applet. )