★ wanayoo — archive 1999 http://javaboutique.webdeveloper.com/tutorials/Java_Game_Programming/MausereignisseEng.htmlNouvelle recherche | Portail wanayoo
Computer Digital Expo


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
Business Search
Find a Consultant
Track Defects Online
Promote Your Website
Internet Jobs
Best Deals on PDAs!
Shop For Computers
Web Site Hosting
Hacker's Secrets
Submit Your Site
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 : The Java Game Development Tutorial :

Contents
Introduction
Basic structure of an applet
Move a ball
Double buffering
Ball bouncing and change the direction
Using sound in applets
Using pictures in applets
Mouse events
Keyboard events
The first complete game
Artificial intelligence for a pong like game
Generation of random 2D landscapes

Handle mouse events

I think there is no game that doesn't "interact" with the player. In our case, as a programmer, you have two possibilities to allow the player to interact with the computer: the player has to use the mouse or the keyboard. In the next two chapters I want to tell you how to handle mouse and keyboard events in an applet. Again this is much easier to do in an applet than in a real application programmed for example using Java Swing. Also you won't be able to use the things you will learn here about event handling in an application, because event handling is very different in applications. But every good book about Java tells you how to handle events in applications so I think it will be no problem for you to learn it by yourself if you need to.

Now I want to change our "ball movement" applet so that the ball changes the direction if the user clicks on the applet.

To do this you have to implement just one method in our Main class called mouseDown. This method should look like this:

    // Method to handle mouse down events
    public boolean mouseDown (Event e, int x, int y)
    {
      // Change direction
      x_speed = - (x_speed);

      // DON'T FORGET (although not necessary here)!!
      return true;
    }

Of course you can handle other events than the mouseDown event. Java makes it possible to overwrite the following methods the same way as I did above and handle other mouse events that way. Here comes a list of the methods:

  1. Mouse click events:
    1. public boolean mouseDown (Event e, int x, int y): handles events that occur if mouse button is pressed down
    2. public boolean mouseUp (Event e, int x, int y): handles events that occur if mouse button is released again.
    3. Use the variable e.clickCount to get the number of clicks performed. That's how you can handle for example double clicks! You'll see a example for this in chapter 5 ("Our first game").
  2. Mouse movement:
    1. public boolean mouseMove (Event e, int x, int y): handles events that occur if mouse is moved over the applet
    2. public boolean mouseDrag (Event e, int x, int y): handles events that occur if mouse is moved over the applet with pressed mouse button
  3. Mouse position:
    1. public boolean mouseEnter(Event e, int x, int y): handles events that occur if mouse enters the applet
    2. public boolean mouseExit (Event e, int x, int y): handles events that occur if mouse leaves the applet

With the help of the variables int x and int y you can get the coordinates where the event happened, which is very important for many games. Also you have to take care of the return statement "return true;" that often has no meaning for the functionality of your game but is expected by the method.

Sourcecode download
Take a look at the applet

Next chapter

Handling keyboard events


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:
A Barebones Guide to Usability Testing
At first glance, usability testing may seem like unnecessary, or additional work. In todays budget slashed world, it is not uncommon for projects not to have the luxury of this valuable phase. This is a dangerous path to tread. Let's look at how we can incorporate usability testing into our design process.
... [more articles]

New Tutorial:
Java Certification Path API
Certificate based security forms the basis for SSL (Secure socket layer) transactions? Every year billions of transactions involving trillions of dollars are done safely using the SSL protocol. This week we'll look at the role of the Java certification path API and see how it provides a mechanism to implement certification path functionality.
... [more Tutorials]

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

Apache Updates Popular HTTP Server

Is Adobe Targeting Microsoft's InfoPath?

Oracle Takes on Collaboration Market



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.



Scrolling Text
This is a suite of three applets that displays scrolling text using an array of images. The detailed description of array of each applet is provided.
Download of the Week
DS PageFlipText
This applet displays a page-flip effect between texts. Over the animation you can also insert an image and a scroll-text.

JB User Poll
Which version of Java do you use the most?
The JavaBoutique Top 15:
1. ChompText
2. PulseText
3. DJPopMenu
4. NavBar
5. RushHour
6. PingPong
7. 3DMaze
8. AScroll2
9. Viewer
10. AnimLetters_anim
11. Encyclo
12. Distorter
13. BMI
14. MusicKeyboard
15. AdvPano
Want more? Check out our Top 100!

Refer-It
Affiliate Program and Referral Directory.
New on internet.com
Cisco Exploit Is Active
Security researchers have increased the threat level on a vulnerability affecting Cisco routers and switches after an exploit was posted to a popular mailing list.

The 3G Tokyo Time Machine
What will a 3G wireless future be like? Step into DoCoMo's time machine and find out.

SBA's Whistling Dixie
Small businesses throughout the South are extended the opportunity to meet one-on-one with government agencies and large corporations to obtain near-term contract opportunities for their products and services.


Jupitermedia is publisher of the internet.com and EarthWeb.com networks.
Copyright 2003 Jupitermedia Corporation All Rights Reserved.

Legal Notices,  Licensing, Reprints, & Permissions,  Privacy Policy.
http://www.earthweb.com/
http://www.internet.com/