Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Code Samples

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
Print Button

Members Only Requires login

Early Access Members Only

Downloads

Bug Database Members Only
Submit a Bug
View Database

Newsletters
Back Issues
Subscribe

Learning Centers
Articles
Bookshelf
Code Samples
New to Java
Question of the Week
Quizzes
Tech Tips
Tutorials

Forums

Technology Centers
java.applet
Code Samples Index

These code examples and other materials are subject to Sun Microsystems, Inc. Legal Terms

Animating an Array of Images in an Applet

This is the simplest applet to animate an array of images. In practice, you should use double-buffering (which this example does not use) to eliminate ``flickering.''

 import java.applet.*;
 import java.awt.*;
     
 public class AnimApplet extends Applet 
                        implements Runnable {
   Image[] images = new Image[2];
   int frame = 0;
   volatile Thread thread;
     
   public void init() {
     images[0] = getImage(getDocumentBase(), 
       "http://hostname/image0.gif");
     
     images[1] = getImage(getDocumentBase(), 
       "http://hostname/image1.gif");
   }
   public void start() {
     (thread = new Thread(this)).start();
   }
   public void stop() {
     thread = null;
   }
   public void paint(Graphics g) {
     g.drawImage(images[frame], 0, 0, this);
   }
   public void run() {
     int delay = 1000;    // 1 second
     try {
       while (thread == Thread.currentThread()) {
         frame = (frame+1)%images.length;
         repaint();
         Thread.sleep(delay);
        }
     } catch (Exception e) {
       }
   }
 }
 

Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon


Print Button
[ This page was updated: 10-Apr-2001 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary | Feedback | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-2001 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.