An Introduction to Games Programming with Java

How to Create a Game Console within a Java Applet in a Web Browser

© Mark Alexander Bain

Sep 9, 2009
An Introduction to Games Programming with Java, Mark Alexander Bain
Studies show that certain computer games can help improve a child's literacy and numeracy. It is, therefore, the Java programmer's duty to start creating games

The UK's Daily Mail has informed the nation of How brain training games 'give children a grade boost. In her article Fiona MacRae says that:

  • games that develop memory also increase literacy and numeracy with IQ levels rising by an average of 10 points
  • the grades of children that play 'brain games' can be shown to improve over just a few weeks
  • these games may also aid adults

Which is great news, not just for children, but also for any programmers planning to create new games, especially when Java makes games development so easy.

Creating a Java Games Applet Applet

All that's required for a simple Java game are:

So, the first job is to do is to create the applet:

import java.applet.*;
import java.awt.*;
public class game extends Applet implements Runnable {

The "Runnable" statement allows threads to be run within the applet. These threads enable the applet to process inputs from the user (such as mouse clicks) at the same time as carrying out operations within the game. This means that the game applet can animate objects (such as figure 1 at the bottom of this article) and move them across a background (as can be seen in figure 2).

Initiating the Java Game Applet

The programmer uses the init method (which runs automatically) to set up the initial state of the applet, and can use this to set the size of the screen and load the images:

Image alien;
Image backdrop;
int alien_height;
int alien_width;
int applet_width = 300;
int applet_height = 225;
int x_speed;
int pos_speed = 2;
public void init () {
setSize(applet_width, applet_height);
alien = this.getImage (this.getDocumentBase (), "alien.png");
alien_width = alien.getWidth (null);
alien_height = alien.getHeight (null);
backdrop = this.getImage (this.getDocumentBase (), "background.jpg");
x_speed = pos_speed;
}

With everything loaded then then running of the game can be considered.

Starting the Java Applet

Once the applet has run the init method then it will run the start method. In this example it is used to start the thread that is required for the animation to be carried out:

int x_speed;
int pos_speed = 2;
public void start () {
Thread animation = new Thread (this);
animation.start ();
x_speed = pos_speed;
}

At this point the applet is up an running, and so the programmer can start to consider how the game should look.

The Java Applet Game Display

The paint method is used to display items on the screen. This is run whenever the applet needs to be drawn:

int x_pos = 0;
int y_pos = 30;
public void paint (Graphics g) {
g.drawImage (backdrop, 0, 0, this);
g.drawImage (alien, x_pos, y_pos, this);
}

However, nothing much will happen yet. The code for the animation is required.

Animating Objects in a Java Applet Game

Associated with the runnable object's thread is the run method. This starts executing when the thread is started, and in this case it consists of an infinite loop that will move an image around the game's screen:

int neg_speed = -1;
int y_speed;
boolean x_axis = true;
public void run () {
while (true) {
if (x_axis) {
if (x_pos + alien_width > applet_width) {x_speed = neg_speed;}
else if (x_pos < alien_width) {x_speed = pos_speed;}
x_pos += x_speed;
} else {
if (y_pos > applet_height - alien_height) {y_speed = neg_speed;}
else if (y_pos < alien_height) {y_speed = pos_speed;}
y_pos += y_speed;
}
repaint ();
try {Thread.sleep (20);} catch (InterruptedException ex) {}
}
}

The final statement of the method builds in a time delay. This, together with a screen repaint, creates the animation (which can be seen in figure 3). However, at the moment there is no user interaction. For that mouse clicks can be taken into account.

Working With Mouse Clicks in a Java Based Game

Threads allow user commands to be captured whilst the animation is running. The mouseDown method fires whenever a mouse button is clicked and, in this case, it's used to change the direction in which the image is moving (changing it from horizontal movement to vertical movement, and vice versa):

public boolean mouseDown (Event e, int x, int y) {
if (x_axis) {
x_speed = 0;
y_speed = pos_speed;
x_axis = false;
} else {
x_speed = pos_speed;
y_speed = 0;
x_axis = true;
}
return true;
}

However, if the application is run at this point then a major problem can be observed. There is a terrible screen flicker.

Removing Screen Flicker in a Java Game

The screen flicker is caused by Java updating the whole of the game screen, rather than just the components that have changed position. The solution is to use the update method and only update the essential areas of the screen:

private Image dbImage;
private Graphics dbg;
public void update (Graphics g) {
dbImage = createImage (this.getSize ().width, this.getSize ().height);
dbg = dbImage.getGraphics ();
paint (dbg);
g.drawImage (dbImage, 0, 0, this);
}
}

Now any elements of the game will move smoothly around the screen.

Obviously this particular game is unlikely to raise the IQ of any child, but it does show how easily and how quickly a simple, but professional looking game, can be created when a programmer uses Java and the Java applet.

BNC101


The copyright of the article An Introduction to Games Programming with Java in Javascript/Java Programming is owned by Mark Alexander Bain. Permission to republish An Introduction to Games Programming with Java in print or online must be granted by the author in writing.


An Introduction to Games Programming with Java, Mark Alexander Bain
Figure 1: An Alien for a Java Game, Clker.com - clip art, royalty free & public domain
Figure 2: Gedney Drove End, Lincolnshire, UK, Mark Alexander Bain
Figure 3: A Simple Java Game, Mark Alexander Bain
 


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo