import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class tinyRandomImage extends Applet {

    int maxImages=500,                    // Added by a user
    maxImage=0,                           // maxImage, # of Images being sent 
    lastInt=-1,
    tmpInt=-1;

    Image[] images = new Image[maxImages];// Images, to be displayed

    Image bg;                             // image to display

    public void init() {
        /****             Images             ****/
        for (int x=0; x<maxImages; x++) {
            images[x] = getImage(getDocumentBase(),
                          getParameter("IMAGE" + Integer.toString(x+1)));
        }

        /****          Find maxImage         ****/
        for (int x=0; x<maxImages; x++) {
            if (images[x] == null) break;
            maxImage++;
        }

        /****          Select Image          ****/
//        tmpInt = getRandomValue();

    }

    public int getRandomValue() {
        while (lastInt == tmpInt) {
            double tmpDbl = Math.random()*maxImage;
            tmpInt = (int)(Math.floor(tmpDbl));
        }
        lastInt = tmpInt;
        return tmpInt;
    }

    public void start() {
//        repaint();
    }

    public void paint(Graphics screen) {
        boolean loaded = false;

        bg = images[getRandomValue()];

        MediaTracker mt = new MediaTracker(this);
        mt.addImage(bg, 0);
        try { mt.waitForAll();
              loaded = !mt.isErrorAny();
            }
        catch (InterruptedException e) {}
        if (!loaded) { return; }

        try { screen.drawImage(bg, 0, 0, this); }
        catch (NullPointerException e) {}
    }

}
