Home E-Books Special Effects and Game Development in Java AppletAnimator II 

Special Effects and Game Development in Java(TM) - AppletAnimator II, now configurable 

by Anibal Wainstein

4.1.3 AppletAnimator II, now configurable

There are not many changes that must be done in the AppletAnimator applet from section 4.0.4 to make it configurable. We start by adding a variable, "maxitems", which will indicate the number of images the user has specified in the applet parameters. The value for this variable must be specified by the user as a parameter, the name will also be called "maxitems":

//"maxitems" will be used to indicate the number of
//images that exist.
int maxitems=0;
//"sleeptime" indicates the delay between each image //in milliseconds.
int sleeptime=0; Image images[];
int currentimage=0; public void init() {
///Get the value of the parameter "maxitems" //with the number base 10. maxitems=getIntegerParameter("maxitems",10); /Get the value of the parameter "sleeptime". sleeptime=getIntegerParameter("sleeptime",10); //The array "images" will have "maxitems" number //of elements images=new Image[maxitems]; MediaTracker tracker=new MediaTracker(this);
for (int i=0; i<maxitems; i++) {
//The following line will read the values of the //parameters "image0", "image1", "image2" and so on //up to the value that maxitems indicates. It will also //use the strings to load the files specified in those //parameters. images[i]=getImage(getDocumentBase() ,getParameter("image"+i)); //The ID number for each image must be increased //for each image you add to the tracker. tracker.addImage(images[i],i); } //Finally we make sure that the tracker loads the images.
try {tracker.waitForAll();}
catch(InterruptedException e) {
System.out.println("Something happened while reading images..."); } }  

The applet will start first by reading the value of "maxitems". This variable must contain the exact number of image parameters that will be read by the applet. The variable "sleeptime" indicates the delay between each image frame, which may be very useful to implement. The file name for each image is specified in the parameters "image0", "image1" and so on. What happens is that the for loop reads a parameter and directly uses it's value to load the image file and adds it to the tracker's list. The configuration for getting the same effect as the example in section 4.0.4 would then look like this:

<APPLET CODE="appletanimator2.class" WIDTH=100 HEIGHT=100>

<PARAM name="maxitems" value="4">

<PARAM name="sleeptime" value="200">

<PARAM name="image0" value="image0.gif">

<PARAM name="image1" value="image1.gif">

<PARAM name="image2" value="image2.gif">

<PARAM name="image3" value="image3.gif">

</APPLET>

The applet is not yet ready. We need to make two small changes in the run() method:

public void run()
{
    while (true)
    {
        update(getGraphics()); //"currentimage" is increased so that the next //image is displayed in the next screen update. currentimage++; //However it must not exceed the value //of maxitems minus 1.
if (currentimage>maxitems-1) currentimage=0; //The variable sleeptime sets //the delay in milliseconds.
        try {Thread.sleep(sleeptime);}
        catch(InterruptedException e) {}
    } }

Please note that "maxitems-1" is the upper limit for the "currentimage" variable and the delay is now set by the "sleeptime" variable. At last it is time to present our first configurable applet! To demonstrate how the same applet can be reconfigurated and look completely different, we use it as a simple slide show with six texture images. We do this by increasing the delay to 2000 milliseconds (2 seconds) and setting "maxitems" to 6. Click here!

 


Next Page >>