Java Code


import java.awt.*;
import javax.swing.*;		
public class SlideShow extends JApplet implements Runnable
{
   int SPEED = 1000;		// how fast to rotate through images
   Image[ ] photos;		// array of all your images
   ImageIcon icon;		// images need to be in an ImageIcon
   JLabel imgLabel;		// and stored into a JLabel
   int currentIndex = 0;		// which image index is currently displayed
   Thread  runner;
   public void init( )
   {
	photos = new Image[4];		// load the images into memory
	photos[0] = getImage( getCodeBase( ), "Australia.jpg" );
	photos[1] = getImage( getCodeBase( ), "Budapest.jpg" );
	photos[2] = getImage( getCodeBase( ), "Galapagos.jpg" );
	photos[3] = getImage( getCodeBase( ), "Nepal.jpg" );
	icon = new ImageIcon( );
	imgLabel = new JLabel( );
	imgLabel.setHorizontalAlignment( JLabel.CENTER );
	setupIcon( 0 ); 	// initialize setup to display first image (index 0)
   	add( imgLabel ); 	// add label to applet
   }
   public void start( )
   {
	runner = new Thread(this); 	// start a thread to time it
	runner.start( );
   }
   public void setupIcon( int index )       // changes the image in the label
   {
	currentIndex = index;
   	icon.setImage( photos[index] );
	imgLabel.setIcon(icon );
	validate( );
   }
  public void run( ) 
  {
    try {
            while ( true ) {
	if ( currentIndex == photos.length-1 )	// if at the last one,
		setupIcon( 0 );			// start over with index 0
	else
		setupIcon( currentIndex+1 );	// else increment index
        	repaint( );  	// Show the change.
        	Thread.sleep(SPEED);
            }
    } catch (InterruptedException ie) { }
  }
}

©2006 by E.S.Boese. All Rights Reserved