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


Partners & Affiliates











advertisement

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

Event handling of key events

Handling key events in applets works very similar to the mouse event handling I talked about in the chapter before. There is one special method for every possible event (key down, key up). You have to overwrite the corrosponding method, if you want to handle, for example, a key down - event. In this chapter we'll have a short look at these two methods. Afterwards we will change our "moving ball" applet that way, so that one can change the direction of the ball movement by pressing the left or right cursor key and stop the ball movement by pressing the space bar. Be carefull! An applet is only able to handle key events once you have clicked in the applet window!

Here comes the explanation of the methods:

  1. public boolean keyDown (Event e, int key): This method listens to events that occur if a key is pressed down.
  2. Every key has a value (ASCII). This value is given to the method with the help of the "key" variable. Space bar has a value of 32. If you want your applet to listen to Space bar pressed down, you only have to test, if the value of "key" is 32 (details later). Sometimes you don't know which value a certain key has. Then you can print out this value to the standard output by writing this line in your keyDown(...) - Method:

    System.out.println ("Charakter: " + (char)key + " Integer Value: " + key);

    Some frequently used keys have special variables. For example the cursor keys have Event.LEFT, Event.RIGHT, Event.UP, Event.DOWN. These values are field variables in the class Event. For more variables please read the API!

  3. public boolean keyDown (Event e, int key): This method reacts to key up - events. You can use this method exactly the same way as the keyDown - Method!

Now we want to modify our "moving ball" applet a little bit. The user should be able to change the direction of the ball movement by pressing the left or right cursor key. If he presses the space bar, the ball should stop its movement. To make this possible, we have to add the following lines to our applet:

    // method to handle key - down events
    public boolean keyDown (Event e, int key)
    {
      // user presses left cursor key
      if (key == Event.LEFT)
      {
        // changing x - speed so that ball moves to the left side (x_speed negative)
        x_speed = -1;
      }
      // user presses right cursor key
      else if (key == Event.RIGHT)
      {
        // changing x - speed so that ball moves to the right side (x_speed positive)
        x_speed = 1;
      }
      // user presses space bar (value = 32!)
      else if (key == 32)
      {
        // Stop ball (x_speed = 0)
        x_speed = 0;
      }
      else
      {
        /* Additionally the method prints out the ASCII - value if an other key is pressed. This is not necessary but a possibility for you to test which value a key has.*/
        System.out.println ("Charakter: " + (char)key + " Integer Value: " + key);
      }

      // DON'T FORGET (although it has no meaning here)
      return true;
    }

Now you are able to program every essential thing used in a game. You can move objects, handle events and load sounds and images. In the next chapter we will program our first complete game (which was the first game I programmed in Java). To understand the next chapter you have to know something about classes, objects, calling methods of other classes... . And of course you have to know everything I talked about in the last chapters. By programming this game I will show you some new techniques; (how to "hit" a ball, changing mouse pointer, random ball movement...). After programming this game you should be able to program your own games. Well Ok have fun in the next chapter and now you can watch the applet you programmed in this chapter and download the source code!

Sourcecode download
Take a look at the applet

Next chapter

Our first game

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
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 34
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%.

IBM Brings Developers Into the Cloud
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?

Delivering Web-based Embedded Fonts in CSS 3
Adobe Helps PHP Developers Create Rich Internet Applications
Java Developers Finding a Home at Adobe Flex
Virtualization Delivers a Dynamic Infrastructure
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

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