Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
65 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Contents
 The Game
 Threads
 Keyboard and Mouse
 Double Buffering
 Coding the Game

 Printable version
 Discuss this article
 in the forums


The Series
 The Basics
 Making a
 Simple Game

 The Power of Arrays

Threads

As I mentioned before in our intial discussion about the game we need to have a loop capability that will allow us repeat the input, processing, and output phases over and over again. In order to do this in our java applet we are going to use a concept called threads. By creating a thread and starting that thread we gain access to a new method called run. Inside this new method we will create a loop that will enable our game to run properly (ie. allowing for a repeat of input, processing, and output).

Let’s now work through a simple example applet in order to get familiar with the basics of threaded applets. This applet should be in a file called SampleThread.java. The first lines to appear in any of our applets are the imports. In this particular example because we aren’t doing anything fancy all we need is the following import.

import java.applet.*;

This allows us to create our basic applet and use the methods necessary to get an applet up and running. The next line in our code is where we actually create our class that represents our java applet. What name do we give to our class? If you said SampleThread then you are absolutely right. The reason we name our class SampleThread is that we have named our code file SampleThread.java and the name of the class must always match up with that filename.

public class SampleThread extends Applet implements Runnable
{

You will notice that after we stated that the class would extend the Applet class we add on the fact that the class will implement Runnable. By implementing Runnable we allow our applet to be threaded and gain access to the method run which we will talk about in a minute. Next we must declare a couple of variables which will allow our thread demonstration to produce something to show what is happening.

  Thread t;
  int i;

The first variable is going to be our thread that will be created in our applet. The second variable is simply an integer counter that is used to demonstrate that we now have the capability to loop which was the fourth phase that I discussed before.

Now it is time to write the code for the methods that our class will contain. In the past we have used two main methods: init and paint. We will be using these again, but this time we will also be including a new method, run, which will become a key portion of our applets in the future.

  public void init()
  {
    t = new Thread(this);
    t.start();

    i = 0;
  }

The init method contains a couple of lines that get our thread up and running. The first line is responsible for creating a new thread for the applet. The reason we type this inside the brackets is that when our applet is actually running we have an instance of our SampleThread class. The init method is then run for this instance and the thread is created for this instance. It is possible to have multiple instances of a class so each instance will have a thread setup for that particular instance. If that still isn’t 100% clear don’t worry about it for the time being and hopefully with a little time it will become clearer. After we create our new thread we need to make sure that we start the thread so that our run method, which we will discuss next, is called. I also initialize my counter, i, to zero so that I know the exact value that it begins with. I know that you will find many programmers who just assume that their variables are going to be initialized to zero if they are integers, but this is a dangerous assumption. I have on occasion forgotten to initialize a variable to zero when programming and lucky for me my computer has been nice enough to fix this by doing it for me. One time after programming for a couple of days at home and having no problems with the code or how the program was running I took the program to school. When running the program at school I had no idea why a particular part of the program that ran perfectly at home had a small glitch in it when I ran it at school. After a lot of debugging I finally stumbled across a variable that I was assuming was intialized to zero. It turns out that while my computer at home was nice enough to initialize variables to zero for me the computer at school thought it would be nice to initialize my integer variables to some number other than zero. The moral of this story is don’t assume anything and always make sure that you initialize your variables before you use them.

The next method that we need to include in our program is the run method. The run method is called when we create a thread in our applet. Inside the run the method we want to loop until the applet is done or the user leaves. For now in order to ensure that we stay in the run method until the user leaves our page we will use a while loop with the keyword true used as the condition. By using the keyword true as the condition we create an infinite loop that will go until either we explicitly break out of it or the user leaves the page containing the applet. It is important not to be scared of infinite loops because they can be very useful to us if we use them properly.

  public void run()
  {
    while(true)
    {
      i++;
      repaint();

      try {
        t.sleep(1000/30);
      } catch (InterruptedException e) { ; }
    }
  }

You will notice that inside the while loop I always increment the variable i and call the repaint method. The repaint method calls my paint method and if I didn’t call repaint in the while loop any changes that were made to the game (ie. puck moving) would not show up on the screen. The reason I have the variable i is so that each time paint is called the variable i contains a new value which will be displayed on the screen in place of the old one to show that we are looping and that we now know how to implement the fourth phase of our four important phases.

The last method that we need to implement is the paint method. The paint method is where we take care of the output that we would like to have displayed on the screen. For this applet the paint method is very simple because the only thing that it does is print a string out to the screen which shows the value of i and makes it look marginally pretty by concatenating the string i = to the front of it.

  public void paint(Graphics g)
  {
    g.drawString("i = "+i,10,20);
  }
}

If you compile and run this applet you will see that the applet displays the ever increasing value of i on the screen. While this may not seem very exciting it is a fundamental concept that will be critical to getting our air hockey game up and running.





Next : Keyboard and Mouse