Home E-Books Special Effects and Game Development in Java An animation applet

Special Effects and Game Development in Java(TM) - An animation applet (AppletAnimator) 

by Anibal Wainstein

4.0.4 An animation applet (AppletAnimator) 

With the applet AppletAnimator we will try to imitate the effect that the animated GIF89 images give. Let us load several images in the init() method:

       //The Image array "images" must be global. 
       Image images[];
      //The variable "currentimage" will hold the
      //image currently being displayed.
int currentimage=0;
public void init() {
//Init "images" to have four elements. images=new Image[4]; MediaTracker tracker=new MediaTracker(this); //We use a for-loop to load the images.
for (int i=0; i<4; i++) {
//"image0.gif","image1.gif",...,"image3.gif" will be //loaded. images[i]=getImage(getDocumentBase(),"image"+i+".gif"); //The ID for each image must be increased
//for each image that is added to the
//tracker.
tracker.addImage(images[i],i); } //Finally we must make sure the tracker load the images.
try {tracker.waitForAll();}
catch(InterruptedException e) {
System.out.println("Something happened while reading images..."); } }  

We begin by initializing the image array with 4 Image objects. The getImage() method will read the images and the for loop with make sure that the images end up in the right place in the array and in the tracker. That is, "images[0]" will reference "image0.gif" at the same time that it is added to the location 0, in the tracker's list. The variable "images[1] will reference "image1.gif" and be added to the location 1, in the tracker's list, and so on.

 
"images"-index Filename MediaTracker list
0
"image0.gif"
0
1
"image1.gif"
1
2
"image2.gif"
2
3
"image3.gif"
3

The applet will be a thread based applet with start() and stop() methods which you can read more about in section 3.0.1. The Run() method looks almost the same as in the textscroller example in chapter 3:

public void run()
{
    while (true)
    {
        update(getGraphics());
//"currentimage" is increased so that the next //image is displayed in the next screen update. currentimage++;
//However, "currentimage" must not exceed the //index value of 3.
if (currentimage>3) currentimage=0;
        try {Thread.sleep(200);}
        catch(InterruptedException e) {}
    } }

The only change we have made in the textscroller's run() method is the handling of the "currentimage" variable. This variable will be used as index so that the applet will change image every 200 millisecond (5 times per second). The paint() method will look like this:

public synchronized void paint(Graphics g)
{
//The integer "currentimage" is used as
    //index for the "images" array.
    g.drawImage(images[currentimage],0,0,this);
}

The images we will animate look like this:

The result of the animation will be that the arrow will appear to being rotated. This is not very interesting to see and we will in a later chapter review how we can do this with only one image and using image processing. Click here to see the applet.


Next Page >>