Java Code
import java.awt.*;
import javax.swing.*;
public class SlidesApplet
extends JApplet
{
Slides myslideshow;
public void init( )
{
myslideshow = new Slides( );
myslideshow.init( this );
setLayout(new FlowLayout( ));
add( myslideshow ) ;
}
}
import java.awt.*;
import javax.swing.*;
public class Slides extends JPanel 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
Thread runner;
int currentIndex = 0; // current image index displayed
public void init( JApplet app )
{
photos = new Image[4]; // load the images into memory
photos[0] = app.getImage( app.getCodeBase( ), "Australia.jpg" );
photos[1] = app.getImage( app.getCodeBase( ), "Budapest.jpg" );
photos[2] = app.getImage( app.getCodeBase( ), "Galapagos.jpg" );
photos[3] = app.getImage( app.getCodeBase( ), "Nepal.jpg" );
icon = new ImageIcon( );
imgLabel = new JLabel( );
imgLabel.setHorizontalAlignment( JLabel.CENTER );
setupIcon( 0 ); // initialize to display first image (index 0)
add( imgLabel ); // add label to applet
runner = new Thread(this); // start a thread to time it
runner.start( );
}
/** changes the image in the label */
public void setupIcon( int index )
{
currentIndex = index;
icon.setImage( photos[index] );
imgLabel.setIcon(icon );
validate( );
}
public void run( ) /** time the slideshow */
{
try {
while(true) {
if( currentIndex == photos.length-1 ) // if at the last one,
setupIcon( 0 ); // start over with index 0
else // otherwise just increment the index
setupIcon( currentIndex+1 );
repaint( ); // Show the change.
Thread.sleep(SPEED);
}
} catch (InterruptedException ie) {
}
}
}
©2006 by E.S.Boese. All Rights Reserved