★ wanayoo — archive 1999 http://javaboutique.webdeveloper.com/tutorials/Step/Chapter2/MouseClickExample.htmlNouvelle recherche | Portail wanayoo
80211 Planet

100's of firms that help your Web site be found


Find a Web Host With:
CGI Access
DB Support
NT Servers
UNIX Servers
Telnet Access

advanced search
Internet News
Internet Investing
IT
Windows Technology
Linux/Open Source
Developer
Interactive Marketing
xSP Resources
Small Business
Wireless Internet
Downloads
Internet Resources
Internet Lists
International
EarthWeb
Career Resources

Search internet.com
Advertise
Corporate Info
Newsletters
E-mail Offers
Be a Commerce Partner
Shop For Computers
Web Hosting
Breathing Problems?
Software Store
Promote Your Website
Business Search
Submit Your Site
Best Deals on PDAs!
Hacker's Secrets
Build an Online Store
FlashKit
Gif.com
HierMenusCentral
JavaBoutique
JavaScript.com
JavaScriptSource
WebDev Jobs
JustSMIL
ScriptSearch
StreamingMediaWorld
Web Hosts
WebDevelopersJournal WDVL
WebDeveloper.com
WebReference.com
XMLFiles.com
CGI
HTML
Java
JavaScript
Web Design and Promotion
Web Site Development
E-mail Address:


Can you receive
HTML e-mail?

Yes   No

Zip Code:


Tutorials : Step by Step :
Chapter 2 :

Contents
Event Listeners
The ActionListener
The MouseListener
The MouseMotionListener

Chapter 2 - Event Listeners

2. The MouseListener




 

/* You might want to create areas where the user can click instead
of using buttons. Somtimes your whole applet will have to be clickable and that is
where the MouseListener comes in.
We are going to create an applet that will show where the user has clicked,
that will also tell if he clicked on a hotspot and if the mouse is in the applet
or not.
*/

import java.awt.*;
import java.applet.*;
// import an extra class for the MouseListener
import java.awt.event.*;

// Tells the applet you will be using the MouseListener methods.

public class MouseClickExample extends Applet implements MouseListener
{
 // The X-coordinate and Y-coordinate of the last click.
  int xpos;
 int ypos;

 // The coordinates of the rectangle we will draw.
 // It is easier to specify this here so that we can later
 // use it to see if the mouse is in that area.
 int rect1xco,rect1yco,rect1width,rect1height;

 // The variable that will tell whether or not the mouse
 // is in the applet area.
  boolean mouseEntered;

 // variable that will be true when the user clicked in the rectangle 
 // then we will draw.
  boolean rect1Clicked;

 public void init() 
 {
  // Assign values to the rectanagle coordinates.
  rect1xco = 20;
  rect1yco = 20;
  rect1width = 100;
  rect1height = 50;

  // Add the MouseListener to your applet
  addMouseListener(this);
 }

  public void paint(Graphics g) 
 {
  // Rectangle's color
  g.setColor(Color.green);

  g.fillRect(rect1xco,rect1yco,rect1width,rect1height);

  g.setColor(Color.red);

  // When the user clicks this will show the coordinates of the click
  // at the place of the click.
  g.drawString("("+xpos+","+ypos+")",xpos,ypos);

  // If the click was in the rectangle show this message
  if (rect1Clicked) g.drawString("You clicked in the Rectangle",20,120);
  // else this one
  else g.drawString("You clicked outside of the rectangle",20,120);

  if (mouseEntered) g.drawString("Mouse is in the applet area",20,160);
  else g.drawString("Mouse is outside the Applet area",20,160);
  }

/* These methods always have to be present when you implement MouseListener

 public void mouseClicked (MouseEvent me) {}
 public void mouseEntered (MouseEvent me) {}
 public void mousePressed (MouseEvent me) {}
 public void mouseReleased (MouseEvent me) {} 
 public void mouseExited (MouseEvent me) {} 
*/

 // This method will be called when the mouse has been clicked.
 public void mouseClicked (MouseEvent me) {

  // Save the coordinates of the click like this.
  xpos = me.getX();
  ypos = me.getY();

  // Check if the click was inside the rectangle area.
  if (xpos > rect1xco && xpos < rect1xco+rect1width && ypos >rect1yco && 
    ypos < rect1yco+rect1height)  rect1Clicked = true;
  // if it was not then rect1Clicked is false;
  else 
   rect1Clicked = false;
  //show the results of the click
  repaint();

 }

 // This is called when the mouse has been pressed
 public void mousePressed (MouseEvent me) {}

 // When it has been released
 // not that a click also calls these Mouse-Pressed and Released.
 // since they are empty nothing happens here.
 public void mouseReleased (MouseEvent me) {} 

 // This is executed when the mouse enters the applet. it will only
 // be executed again when the mouse has left and then re-entered.
  public void mouseEntered (MouseEvent me) {
  // Will draw the "inside applet message"
  mouseEntered = true;
  repaint();
 }

 // When the Mouse leaves the applet.
  public void mouseExited (MouseEvent me) {
  // will draw the "outside applet message"
  mouseEntered = false;
  repaint();
 } 

/* So now you can use the MouseListener instead of Buttons. These methods will be ones that you will
often use. These methods are good for mouseClicks, but when you need mouseOvers like in Javascript
then you'll need the MouseMotionListener.
Go to MouseMotionExample.java
*/

 


Applet Index
(sorted alphabetically)

A B C D E F G H I J K
L M N O P Q R S T U
V W X Y Z #s
The Java Source
(applets w/source code)

A B C D E F G H I J K
L M N O P Q R S T U
V W X Y Z

How to Add Java Applets to Your Site

New on the Java Boutique:

New Article:
J2EE Deployment Specification
Why is it that J2EE applications written in the same language, Java, may not coexist on different servers? While the answer isn't simple Sun has set out to alleviate these problems with the "J2EE Deployment Specification".
... [more articles]

New Tutorial:
Designing Packages for Stability
Are your designs stable? This month Samudra will focus on the change impact relationship involved in designing classes showing us how to take them into account when designing the package relationship.
... [more Tutorials]

Year in Review:
The Best of 2002
The year 2002 saw 201 new applets posted to JavaBoutique. Check out the 2 most popular apps from each month of 2002!
... [popular applets]

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.

internet.com logo




Developer News

JBoss Group: Defections Aren't a Big Deal

IE Object Tag Buffer Overflow Patched

SCO Shuts Down German Site



Database Journal Launched!
Jupiter Media has announced the launch of Database Journal. DBJ offers SQL courses and other database related resources for beginner to expert developers.



JWizardPane
Build modern wizard style interface quickly. API easy to use. Bulti-in show dialog and frame support.
Download of the Week
BSCOutline v5.10
BSCOutline Java Outline Applet is a customizable tree view control for use in Web pages, similar to that used by Windows File Manager or Explorer. BSCOutline has been written to be the simple to use and small to store.

JB User Poll
How does .NET fit into your programming?
The JavaBoutique Top 15:
1. ChompText
2. RushHour
3. PulseText
4. PingPong
5. DJPopMenu
6. NavBar
7. 3DMaze
8. Encyclo
9. AnimLetters_anim
10. AScroll2
11. Nibbly
12. SonaliCalendar
13. BMMenu
14. BMI
15. SlideMenu
Want more? Check out our Top 100!

Refer-It
Affiliate Program and Referral Directory.
New on internet.com
Verizon Ordered to Reveal Names of Music Pirates
The U.S. Court of Appeals Wednesday afternoon rejected Verizon's for request for a stay in earlier court ruling ordering the telecom giant to turn over the names of two subscribers suspected of illegally downloading copyrighted music.

E-mail Archiving Emerges as Critical Function
In today's corporate world, e-mail holds a company's critical information. Now a growing number of businesses are realizing that they need to save these e-mails. And they just can't pack them away like receipts you toss in a box at home.

The Deadly Duo: Spam and Viruses, May 2003
Spam attacks increase another 6+% for the month, as the global ratio of unwanted messages in business e-mail breaks the 50% mark.


The Event For Leading Edge Flash Development

Copyright 2003 Jupitermedia Corporation All Rights Reserved.
Legal Notices,  Licensing, Reprints, & Permissions,  Privacy Policy.
http://www.internet.com/