Home | Java Menu | Java Applets | Java Tips | E-mail Us
View Java Applet


/* Animation.java
  *
  * A Java Applet demonstrating image animation.
  *
  * Authored by Khim Theng
  * khim_theng@completeesolutions.com
  *
  * Last modified on 05/11/2000
  */

import java.applet.Applet;
import java.awt.*;

public class Animation extends Applet
{
    private Image dogs[];
    private int totalImages = 4,  //Total number of images
                      currentImage = 0,  //Current image subscript
                      sleepTime = 500;  //Milliseconds to sleep

    //Load the image when the applet begins executing
    public void init()
    {
        setBackground(Color.white);  //Set the background to white

        dogs = new Image[totalImages];  //Create a new Image array

        for (int i=0; i<dogs.length; i++)
            dogs[i] = getImage(getDocumentBase(), "dog" + i + ".jpg");  //Find the images in the same directory as the HTML file
    }

    public void start()
    {
        currentImage = 0; //Always start with the 1st image
    }

    //Display the images
    public void paint(Graphics g)
    {
        g.drawImage(dogs[currentImage], 1, 1, this);  //Draw the image at x position 1 and y position 1

        currentImage = (currentImage + 1)%totalImages;  //Increment to the next image

        try
        {
            Thread.sleep(sleepTime);  //Sleep for 500 milliseconds
        }
        catch (InterruptedException e)
        {
            showStatus(e.toString());
        }

        repaint();
    }

}  //End of Animation
                
View Java Applet

Home | Java Menu | Java Applets | Java Tips | Legal
© 1999-2003 Complete e Solutions, Inc.