Reviews : Java Books :
Beginning Java 2- JDK 1.3 Version : Images and Animation

Buy this book
Title: Beginning Java 2- JDK 1.3 Version
ISBN: 1861003668
US Price: $ 49.99
Canadian Price:
C$ 74.95
UK Price: £ 35.99
Publication Date: March 2000
Pages: 1230
© Wrox Press Limited, US and UK.

Beginning Java 2- JDK 1.3 Version
Images and Animation

An Animated Applet

Just so that you know where we are heading, our first program illustrating animation will be an applet that drops the Wrox logo from a great height to see what happens. Since Wrox Press is an immensely resilient company, the logo will bounce.

To implement this we just need to draw the logo at its new position at fixed intervals of time – the new position being determined by how far the logo has fallen during the time interval. We can store the coordinates of the current location of the image in data members of the applet class, imageX and imageY and the paint()ImagePanel will draw the image at that position. The animation code in the run() method will need access to the height of the image so it can work out when the image hits the ground, so we should store the image dimensions, imageWidth imageHeight, as data members too.

We will call the applet class LogoBounce, so the outline contents of the source file will be:

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.net.*;

public class LogoBounce extends JApplet
		implements Runnable
{
// This method is called when the applet is loaded
public void init()
  {
// Code to initialize the applet...
  }
// This method is called when the browser starts the applet
public void start()
  {
  bouncer = new Thread(this); // Create animation thread
  bouncing = true;
  bouncer.start(); // and start it
}

// This method is called when the browser wants to stop the applet
// - when is it not visible for example
public void stop()
{
  bouncing = false;  // Stop the animation loop
  bouncer = null;  // Discard the thread
}

// This method is called when the animation thread is started
public void run()
{
  // Code for the animation thread...
}

class ImagePanel extends JPanel
{
  public ImagePanel(Image image)
  {
     this.image = image;
  }

  public void paint(Graphics g)
  {
     // Initialize the animation...

     while(bouncing)
     {    
       // Code for the animation loop
     }
  }

  Image image; // The image
}

Thread bouncer;  // The animation thread
boolean bouncing = false; // Controls animation thread
ImagePanel imagePanel;  // Panel for the image
int imageWidth, imageHeight; // Image dimensions
int imageX, imageY; // Current image position

}

All the basic things we need are here. In the init() method we will set up a component on which we can draw an image and add this to the content pane for the applet, just as we did in the previous example. The start() method creates the animation thread, bouncer, sets the variable, bouncing, that will control the animation loop to true, and starts the thread. The run() method will contain the animation loop that will continue to run as long as bouncing is true. In the stop() method we just set bouncing to false to stop the animation loop in the run() method, then discard the animation thread object by setting bouncer to null. The start() method will create a new thread if the animation needs to be restarted.

We will fetch the image from the file in the init() method using the getImage() method as we did in the previous example. There's a complication here though. The browser will call start() as soon as the init() method returns to start the applet, and this will start the thread to do the animation. Since this will involve calculations involving the height of the image, we don't want this to go ahead until we are sure the height is available. One way of determining when an image has been loaded is to make use of something called a media tracker.

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.