Tutorials : Java by Example : Section 6 :
Section Six Contents
Using Mousemove and Mousedrag
Keyboard Commands and Playing Sound
Detecting Collisions and Intersections
A Bouncing Balls Applet

Input Devices

Using Mousemove and Mousedrag

We have not explored the funcion mouseMove yet, which is a useful and important one. It is called whenever you move the mouse inside the applet and can also detect, at which coordinates the cursor is. We use this function in the following applet, in order to display the actual mouse coordinates and to detect whether the mouse cursor is inside one of three colored rectangles. We use the Rectangle class here, which provides us with a convenient method inside(int x, int y), to tell whether a given point is inside the rectangle or not. Clicking just paints the background in a different color here.

//Sourcecode

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

public class Project27 extends Applet
{
   Image Buffer;
   Graphics gBuffer;
   int x, y;
   boolean click, mouseInside, insideR1, insideR2, insideR3;
   //for reference see package java.awt class Rectangle
   Rectangle r1, r2, r3;

   public void init()
   {
      Buffer=createImage(size().width,size().height);
      gBuffer=Buffer.getGraphics();

      //create 3 rectangle objects
      r1=new Rectangle(20,70,80,100);
      r2=new Rectangle(110,70,80,100);
      r3=new Rectangle(200,70,80,100);
   }

   public void drawStuff()
   {
      //if mouse was clicked, paint background magenta, otherwise black
      if(click)
             gBuffer.setColor(Color.magenta);
      else
             gBuffer.setColor(Color.black);

      gBuffer.fillRect(0,0,size().width,size().height);

      //paint the 3 rectangles
      gBuffer.setColor(Color.blue);
      gBuffer.fillRect(r1.x, r1.y, r1.width, r1.height);

      gBuffer.setColor(Color.green);
      gBuffer.fillRect(r2.x, r2.y, r2.width, r2.height);

      gBuffer.setColor(Color.red);
      gBuffer.fillRect(r3.x, r3.y, r3.width, r3.height);

      //draw the mouse coordinates
      if (mouseInside)
      {
          gBuffer.setFont(new Font("Helvetica",Font.BOLD,12));
          gBuffer.setColor(Color.white);
          gBuffer.drawString ("X:  "+x,30,30);
          gBuffer.drawString ("Y:  "+y,30,50);
      }
      else
      {
          gBuffer.setFont(new Font("Helvetica",Font.PLAIN,14));
          gBuffer.setColor(Color.orange);
          gBuffer.drawString ("Move the mouse inside the applet!",40,30);
      }

      //since we need this font several times, we create it here
      Font bigFont=new Font("Helvetica",Font.BOLD,45);

      if (insideR1)
      {
          gBuffer.setFont(bigFont);
          gBuffer.setColor(Color.blue);
          gBuffer.drawString ("BLUE",120,50);
          gBuffer.setColor(Color.white);
          gBuffer.drawRect(r1.x, r1.y, r1.width-1, r1.height-1);
      }

      if (insideR2)
      {
          gBuffer.setFont(bigFont);
          gBuffer.setColor(Color.yellow);
          gBuffer.drawString ("GREEN",120,50);
          gBuffer.setColor(Color.white);
          gBuffer.drawRect(r2.x, r2.y, r2.width-1, r2.height-1);
      }

      if (insideR3)
      {
          gBuffer.setFont(bigFont);
          gBuffer.setColor(Color.red);
          gBuffer.drawString ("RED",120,50);
          gBuffer.setColor(Color.white);
          gBuffer.drawRect(r3.x, r3.y, r3.width-1, r3.height-1);
      }
   }

   //detects any mouse move within our applet
   public boolean mouseMove(Event evt,int x,int y)
   {
      //submit the mouse coordinates to our instance variables
      this.x=x;
      this.y=y;

      //"inside" is a method of the Rectangle class
      if(r1.inside(x,y))
         insideR1=true;
      else
         insideR1=false;

      if(r2.inside(x,y))
         insideR2=true;
      else
         insideR2=false;

      if(r3.inside(x,y))
         insideR3=true;
      else
         insideR3=false;

      drawStuff();

      repaint();
      return true;
   }

   public boolean mouseEnter(Event evt,int x,int y)
   {
      mouseInside=true;
      return true;
   }

   public boolean mouseExit(Event evt,int x,int y)
   {
      mouseInside=false;
      repaint(500);
      return true;
   }

   public boolean mouseDown(Event evt,int x,int y)
   {
      click=true;
      repaint();
      return true;
   }

   public boolean mouseUp(Event evt,int x,int y)
   {
      click=false;
      repaint();
      return true;
   }

   public void update(Graphics g)
   {
      paint(g);
   }

   public void paint(Graphics g)
   {
      drawStuff();
      g.drawImage (Buffer,0,0, this);
   }
}

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.