/* File: GIFCanvasApplet.java
 * Copyright (C) 1998 by Bill Giel/KCMDG. All rights reserved.
 *
 * This little applet demonstrates use of the GIFCanvas package
 * to display animated GIF's in a Java applet (or application.)
 */

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

import com.kcmultimedia.gifcanvas.GIFCanvas;

public class GIFCanvasApplet extends Applet
    implements Runnable
{
    public static final String MSG = "Select an image, press Show";
    Choice picker;
    Button show;
    GIFCanvas canvas;
    boolean hasImage;
    TextField lbl;

    Thread waitForStop;

    URL imageURL;

    
    public void init()
    {
        setBackground(new Color(228,228,228));
        Panel p = new Panel();
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        setLayout(new BorderLayout());
        p.setLayout(gridbag);
        picker = new Choice();
        show = new Button("Show");
        canvas = new GIFCanvas();
       
        lbl = new TextField(MSG);
        lbl.setEditable(false);

        picker.addItem("images/banner1.gif");
        picker.addItem("images/banner2.gif");
        picker.addItem("images/gears.gif");
        picker.addItem("images/compass.gif");
        picker.addItem("images/world.gif");
        picker.addItem("images/eye.gif");
        picker.addItem("images/book.gif");

        c.weightx=1;
        c.weighty=0;
        c.gridwidth=1;
        c.fill=GridBagConstraints.HORIZONTAL;
        c.anchor=GridBagConstraints.EAST;
        c.insets = new Insets(5,5,5,2);
        gridbag.setConstraints(picker,c);
        p.add(picker);

        c.weightx=0;
        c.weighty=0;
        c.gridwidth=GridBagConstraints.REMAINDER;
        c.fill=GridBagConstraints.NONE;
        c.anchor=GridBagConstraints.WEST;        
        c.insets = new Insets(5,2,5,5);
        gridbag.setConstraints(show,c);
        p.add(show);

        c.weightx=1;
        c.weighty=1;
        c.gridwidth=GridBagConstraints.REMAINDER;
        c.fill=GridBagConstraints.BOTH;
        c.anchor=GridBagConstraints.CENTER;
        c.insets = new Insets(5,5,5,5);
        gridbag.setConstraints(canvas,c);
        p.add(canvas);

        add("Center",p);
        add("South",lbl);
    }

    //When the applet stops, stop the animation thread.
    
    public void stop()
    {
        if(hasImage){
            canvas.stop();
        }
        super.stop();
    }

    //If we restart from a backlink, and we had an image, we can simply
    //restart the animation. Netscape Navigator does this correctly. IE
    //apparently does not.
    
    public void start()
    {
        if(hasImage){
            canvas.start();
        }
    }

    public boolean action(Event e, Object arg)
    {
        
		if(e.target.equals(show)){

            if(hasImage){
                canvas.stop();
            }

            //After telling GIFCanvas to stop, we have to wait for
            //it to fully stop, thus the following...
            
            if(null == waitForStop){
                waitForStop=new Thread(this);
                waitForStop.start();
            }

           

			return true;
		}

		else return super.action(e,arg);
	}

    //Our running GIFCanvas does not stop instantaneously, so we
	//will poll the GIFCanvas.isStopped() method to wait until it
	//does.
	public void run()
	{
	    try{
            
	        while(!canvas.isStopped()){
	            waitForStop.yield();
	        }

	        //Once stopped we can do all of this stuff...
	        
	        canvas.hide();

            lbl.setText("Loading selected image...");	        
            imageURL = new URL(getCodeBase(),picker.getSelectedItem());
            canvas.setImage(imageURL);
            hasImage=true;
   	        canvas.show();
            canvas.start();
            lbl.setText(MSG);            
        }catch(Exception ee){
            lbl.setText("An error occured!");
            if(imageURL != null)
                System.out.println("Error loading image: " + imageURL.toString());
            hasImage=false;
        }
	    waitForStop = null;
	}
	    
	    

}

        
        
        

        
