/////////////////////////////////////////////////////////
// Copyright 1997 A.A. Butterfield (DCompose)
/////////////////////////////////////////////////////////
package com.dcompose.slideshow;

import java.util.*;
import java.awt.*;
import java.applet.*;

/*************************************************
* loads an image and waits for it to complete loading
*************************************************/
class LoadThread extends Thread
{
   /** the loaded image **/
	private Image mImage;
	/** the applet whos methods are used to load image **/
	private Applet mApplet;
	/** the file to load the image from **/
	private String mFile;
	/** flag to stop multiple attempts to load **/
	private boolean mLoaded;

   /*************************************************
   * construct the thread
   * @param aApplet the applet whos methods are used to load image
   * @param aFile the file to load the image from
   *************************************************/
	public LoadThread(Applet aApplet, String aFile)
	{
		mApplet=aApplet;
		mFile=aFile;
		start();
	}

   /*************************************************
   * threads run method
   * @see java.lang.Thread
   *************************************************/
	synchronized public void run()
	{
	   if (!mLoaded)
	   {
   		try
   		{
         	mImage=mApplet.getImage(mApplet.getDocumentBase(),mFile);
   	      MediaTracker mt=new MediaTracker(mApplet);
   	      mt.addImage(mImage,1);
   	      mt.waitForAll();
   		}
   		catch (InterruptedException e)
   		{
   			System.err.println("LoadThread.run "+e);
   		}
   		mLoaded=true;
   		stop();
   	}
	}

   /*************************************************
   * @return the loaded image
   *************************************************/
	public Image getImage()
	{
		return mImage;
	}
}


