advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement


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.

 Avaya Developer Showcase
XML error: undefined entity at line 24
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Apache at 10: You Can't Buy Us
Microsoft's CodePlex Foundation Moving Forward
Apple Claims 100,000 Apps, Google Analyzes Them
Nokia Latest to Play Opera Mobile 10 Browser
PayPal Opens Up Payment Platform to Devs
Ubuntu Linux 9.10 'Karmic Koala' Starts Its Climb
IBM Links Rational Developer Tools, Tivoli Apps
Libraries Give Vista Apps a Windows 7 Look
Ubuntu: The 'Default Alternative' to Windows?
Tilera's New Line to Include 100-Core CPU

Consuming XML Web Services in iPhone Applications
Build a More Agile Business with IBM
POJO-Based Solutions for LDAP Access: One Good, One Better
IBM Offers Enhanced Measurement and Management for Energy Usage
IBM Helps Transformation to an Information-Based Enterprise
Top Five Touch UI-Related Design Guidelines
Architectural Layers and Tips on how to Achieve Architecture Consistency
Can You Spy BI?
Once Again Business, Not IT Top Concern for CIOs
SOA, Cloud and the Rise of the IT services-based Economy

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs