/* imageLoader.java
 * Copyright (C) 1996 by William Giel
 *
 * E-mail: rvdi@usa.nai.net
 * WWW: http://www.nai.net/~rvdi/home.htm
 *
 ***************************************************************************
 * Abstract
 * --------
 * Creates a separate, image loading thread.
 ***************************************************************************
 * Permission to use, copy, modify, and distribute this software and its
 * documentation without fee for NON-COMMERCIAL purposes is hereby granted.
 *
 * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
 * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 ***************************************************************************/
import java.awt.*;
import java.applet.*;
import java.lang.*;
import java.net.*;



public class imageLoader extends Thread
{

    static final String BAD_URL       =  "\r\nError: Malformed Image URL.";
    static final String IMAGE_ERROR   =  "\r\nError: Image(s) not loaded.";
    static final String IMAGE_LOADED  =  "Done.";
    static final String IMAGE_LOADING =  "\r\nLoading images...";

	Image image[];
	String szImage[];
	int nImages;
	boolean loaded;
	Object instance;
	TextArea s;

	imageLoader(Object inst, String szName[],int numImages, TextArea scroller)
	{

		image=new Image[numImages];
		szImage=new String[numImages];

		for(int i=0;i<numImages;i++){
			szImage[i]=szName[i];
			image[i]=null;
		}
		nImages=numImages;
		instance = inst;
		s=scroller;
	}

	public void run()
	{
        if(instance instanceof Applet){
		    URL imageURL;
    		if(null != s)s.appendText(IMAGE_LOADING);
	    	loaded=false;
	        MediaTracker  tracker = new MediaTracker((Applet)instance);
	        for(int i=0; i<nImages; i++){
			    if(null != szImage[i]){
		                try{
			                imageURL=new URL(((Applet)instance).getCodeBase(),szImage[i]);
    	    	        } catch (MalformedURLException e)
	    	                {
    	    	   		        imageURL=null;
    	    	   		        if(null != s)s.appendText(BAD_URL);
    	    	   		        System.out.println(BAD_URL);
    	    	   		        continue;
		                    }
			    }
			    else imageURL=null;

			    if(null != imageURL){
       			        image[i]=((Applet)instance).getImage(imageURL);
	           	        if(null != image[i])
 		                tracker.addImage(image[i],i);
	    	    }
	    	    else image[i]=null;

			    if(null != image[i])
				try{
					    tracker.waitForID(i);
	        	   }catch (InterruptedException e)
	        	        {
	        	   		        image[i]=null;
	        	   		        if(null != s)s.appendText(IMAGE_ERROR);
	        	   		        System.out.println(IMAGE_ERROR);
        	       	   	}

		    }
		}
		else{
		    if(null != s)s.appendText(IMAGE_LOADING);
	    	loaded=false;
	        MediaTracker  tracker = new MediaTracker((Component)instance);
	        for(int i=0; i<nImages; i++){
			    if(null != szImage[i]){
		            image[i]=((Component)instance).getToolkit().getImage(szImage[i]);
	           	        if(null != image[i])
 		                tracker.addImage(image[i],i);
	    	    }
	    	    else image[i]=null;

			    if(null != image[i])
				try{
					    tracker.waitForID(i);
	        	   }catch (InterruptedException e)
	        	        {
	        	   		        image[i]=null;
	        	   		        if(null != s)s.appendText(IMAGE_ERROR);
	        	   		        System.out.println(IMAGE_ERROR);
        	       	   	}

		    }
		}
		loaded=true;
		if(null != s)s.appendText(IMAGE_LOADED);
	}

	Image[] retrieveImages()
	{
		if(loaded)
			return image;
		else
			return null;
	}
}

