Slide Show in Java



import java.applet.*;
import java.awt.*;
import java.net.*;

public class SlideShow extends Applet implements Runnable
{

    Image images[];
	AudioClip sounds[];
    int imageNum = 0;
	int soundNum = 0;
	int nimgs;
	int nsnds;
    int imgwidth;
    int x, newx;
    int dist = 5;
    int timeout = 500;
	Thread thread1;

	public void init()
	{

		showStatus("Loading images");

		MediaTracker mediaTracker = new MediaTracker (this);
        
		newx = x = size().width;

		nimgs = 12;
		images = new Image[nimgs];
		for (int i = 0; i < nimgs; i++) { images[i] = getImage (getCodeBase(), "graphics/P" + (i+1) + ".jpg"); }

		showStatus("Loading sound");

		nsnds = 3;
		sounds = new AudioClip[nsnds];
		for ( int j = 0; j < nsnds; j++ ) { sounds[j] = getAudioClip ( getCodeBase(), "sounds/" + (j+1) + ".au" ); }

		try{mediaTracker.waitForAll();} catch (InterruptedException e){}

		showStatus("Done loading");

	}

	public void start ()
	{
		if ( thread1 == null )
		{
			thread1 = new Thread(this);
			thread1.start();
		}
	}
	
	public void stop ()
	{
		if (thread1 != null)
		{
			thread1.stop();
			thread1 = null;
		}
	}

    public void run ()
    {
		while (true)
        {
            try
            {
                Thread.currentThread().sleep ( timeout );
            }
            catch (Exception e)
            {
            }
			scroll ( dist );
        }
    }

    synchronized void scroll (int dist)
	{
		newx += dist;
		repaint ();
		++imageNum;
    }

    public void update (Graphics g)
    {

		g.setColor (Color.lightGray);

		if (newx != x)
		{
			int dist = newx - x;

			if (dist > 0)
			{
				g.copyArea (1, 1, (size().width-2) - dist, size().height-2, dist, 0);
				for (x = newx ; x > size().width ; x -= Math.max(size().width - 2, imgwidth));
				paint (g, 1, dist + 1);
			}
			
			else
			{
				g.copyArea (1 - dist, 1, (size().width-2) + dist, size().height-2, 1, 1);
				for (x = newx ; x < 0 ; x += Math.max(size().width - 2, imgwidth));
				paint (g, (size().width-1) + dist, size().width-1);
			}

		}

		else
		{
			paint (g);
		}

	}

    public boolean imageUpdate (Image img, int flags, int x, int y, int w, int h)
	{
		if ((flags & WIDTH) != 0)
		{
			imgwidth += img.getWidth(this);
		}
		return super.imageUpdate (img, flags, x, y, w, h);
    }

    public void paint (Graphics g, int fromx, int tox)
	{
		int x = this.x;
		newx = x;

        if ( ( ( ( soundNum - 1 ) * 4 ) + 5) == imageNum ) { sounds[soundNum++].play(); } 

		g.setColor(Color.lightGray);
		g.fillRect(fromx, 1, tox - fromx, size().height-2);
		g.clipRect(fromx, 1, tox - fromx, size().height-2);
		g.setColor(Color.black);

		for (int i = 0 ; i < nimgs ; i++)
		{
			if (images[i] == null)
			{
				continue;
			}

		    int w = images[i].getWidth(this);
		    int h = images[i].getHeight(this);
		    if ((w > 0) && (h > 0))
			{
				if ((x + w > fromx) && (x < tox))
				{
				    g.drawImage (images[i], x, size().height - (h+1), this);
				}
				if ((x + w) > size().width)
				{
				    x -= Math.max(size().width - 2, imgwidth);
				    if ((x + w > fromx) && (x < tox))
					{
						g.drawImage (images[i], x, size().height - (h + 1), this);
				    }
				}
				x += w;
		    }
		}
    }

    public void paint (Graphics g)
	{
		g.draw3DRect (0, 0, size().width, size().height, true);
		paint(g, 1, size().width-1);
    }

}