Customizing an applet via HTML parameters

<html>
<applet code="LogoApplet.class" width=400 height=400>
<param name="totalimages" value="30">
<param name="imagename" value="deitel">
<param name="animationdelay" value="200">
</applet>
</html>

LogoApplet.java

HTML parameter "animationdelay" is an int indicating milliseconds to sleep between images (default 50).

HTML parameter "imagename" is the base name of the images that will be displayed (i.e., "deitel" is the base name for images "deitel0.gif," "deitel1.gif," etc.). The applet assumes that images are in an "images" subdirectory of the directory in which the applet resides.

HTML parameter "totalimages" is an integer representing the total number of images in the animation. The applet assumes images are numbered from 0 to totalimages - 1 (default 30).

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

public class LogoApplet extends JApplet{   
   public void init()
   {
      String parameter;

      parameter = getParameter( "animationdelay" );
      int animationDelay = ( parameter == null ? 50 :
                             Integer.parseInt( parameter ) );

      String imageName = getParameter( "imagename" );

      parameter = getParameter( "totalimages" );
      int totalImages = ( parameter == null ? 0 :
                          Integer.parseInt( parameter ) );

      // Create an instance of LogoAnimator
      Animator animator;

      if ( imageName == null || totalImages == 0 )
         animator = new Animator();
      else
         animator = new Animator( totalImages,
                       animationDelay, imageName );

      setSize( animator.getPreferredSize().width,
               animator.getPreferredSize().height );
      getContentPane().add( animator, BorderLayout.CENTER );

      animator.startAnimation();
   }
}

Animation.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Animator extends JPanel
                          implements ActionListener {
   protected ImageIcon images[];
   protected int totalImages = 30,
                 currentImage = 0,
                 animationDelay = 50; // 50 millisecond delay
   protected String imageName = "deitel";
   protected Timer animationTimer;

   public Animator()
   {
      initializeAnim();
   }

   // new constructor to support customization 
   public Animator( int num, int delay, String name )
   {
      totalImages = num;
      animationDelay = delay;
      imageName = name;

      initializeAnim();
   }

   private void initializeAnim()
   {
      images = new ImageIcon[ totalImages ];

      for ( int i = 0; i < images.length; ++i ) 
         images[ i ] = new ImageIcon( "images/" +
                              imageName + i + ".gif" );

      // moved here so getPreferredSize can check the size of
      // the first loaded image.
      setSize( getPreferredSize() ); 

      startAnimation();
   }

   public void paintComponent( Graphics g )
   {
      super.paintComponent( g );

      if ( images[ currentImage ].getImageLoadStatus() ==
           MediaTracker.COMPLETE ) {
         images[ currentImage ].paintIcon( this, g, 0, 0 );
         currentImage = ( currentImage + 1 ) % totalImages;
      }
   }

   public void actionPerformed( ActionEvent e )
   {
      repaint();
   }

   public void startAnimation()
   {
      if ( animationTimer == null ) {
         currentImage = 0;  
         animationTimer = new Timer( animationDelay, this );
         animationTimer.start();
      }
      else  // continue from last image displayed
         if ( ! animationTimer.isRunning() )
            animationTimer.restart();
   }

   public void stopAnimation()
   {
      animationTimer.stop();
   }

   public Dimension getMinimumSize()
   { 
      return getPreferredSize(); 
   }

   public Dimension getPreferredSize()
   {
      return new Dimension( images[ 0 ].getIconWidth(),
                            images[ 0 ].getIconHeight() );
   }

   public static void main( String args[] )
   {
      Animator anim = new Animator();

      JFrame app = new JFrame( "Animator test" );
      app.getContentPane().add( anim, BorderLayout.CENTER );
	  app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	  /*
      app.addWindowListener(
         new WindowAdapter() {
            public void windowClosing( WindowEvent e )
            {
               System.exit( 0 );
            }
         }
      );
	  */

      app.setSize( anim.getPreferredSize().width + 10,
                   anim.getPreferredSize().height + 30 );
      app.show();
   }
}


Maintained by John Loomis, last updated 19 July 2000