/*
	BounceGif

	Bounces a user defined set of GIFs up and down inside the applet
	window.
	
	Necessary Parameters (will crash without) :
		NO_GIFS		No of gifs to bounce
				(GIFS will be named bng0.gif ... bng<n-1>.gif)
		GIFW		Width of GIF
		GIFH		Height of GIF
	Optional Parameters :
		MAXVEL		Max strength of bounce.
		MINVEL		Min strength of bounce.

	Note the width and height that the applet _thinks_ the GIFs are doesn't have to be the same as the actual
	GIFs.

	Naff, but hey it is my first Java applet :-)
*/

import java.applet.Applet;
import java.awt.*;

public class BounceGif extends Applet implements Runnable
{

    /* State
    */
    Image[]	gif;
    int		no;
    int		gifw;
    int		gifh;
    int		start_x;
    int		max_y;

    int[]	posy;
    int[]	posyi;
    int		maxvel;
    int		minvel;

    Image	img;
    int		imgw,imgh;

    int		cx,cy;
    int		width,height;
    Color	bg;

    Thread	task;

    /* Local utils
    */

    private String FName(int i)
    {
	String s;

	s="bng"+i+".gif";
	return(s);
    }


    private int RND(int i)
    {
	double res;

	res=Math.random();

	return ((int)(res*i));
    }


    private int GetVel()
    {
	if (minvel==maxvel)
	    {
	    return(maxvel);
	    }
	else
	    {
	    return(RND(maxvel-minvel)+minvel);
	    }
    }


    private void GetScreenSize()
    {
	width=size().width;
	height=size().height;

	cx=width/2;
	cy=height/2;

	if ((img==null)||(width!=imgw)||(height!=imgh))
	    {
	    img=this.createImage(width,height);
	    imgw=width;
	    imgh=height;
	    }
    }


    public void DrawGifs(Graphics g) 
    {
	int f;
	int x;

	g.setColor(bg);
	g.fillRect(0,0,width-1,height-1);

	x=start_x;

	for(f=0;f<no;f++)
	    {
	    g.drawImage(gif[f],x,posy[f],null);

	    x+=gifw;

	    posy[f]+=posyi[f];
	    posyi[f]+=1;

	    if (posy[f]>=max_y)
		{
		posy[f]=max_y;
		posyi[f]=-GetVel();
		}
	    }
    }


    /* Main loop
    */
    public void run()
    {
	Thread.currentThread().setPriority(Thread.NORM_PRIORITY);

	while (true)
	    {
	    GetScreenSize();
	    DrawGifs(img.getGraphics());
	    this.getGraphics().drawImage(img,0,0,this);

	    try
		{
		Thread.sleep(100);
		} catch (InterruptedException e) {};
	    }
    }

    /* Applet event hooks
    */
    public void init() 
    {
	String param;
	int f;
	boolean fl;

	bg=Color.black;

	no=Integer.valueOf(getParameter("NO_GIFS")).intValue();
	gifw=Integer.valueOf(getParameter("GIFW")).intValue();
	gifh=Integer.valueOf(getParameter("GIFH")).intValue();

	param=getParameter("MAXVEL");

	if (param!=null)
	    {
	    maxvel=Integer.valueOf(param).intValue();

	    param=getParameter("MINVEL");

	    if (param!=null)
		{
		minvel=Integer.valueOf(param).intValue();
		}
	    else
		{
		minvel=maxvel;
		}
	    }
	else
	    {
	    maxvel=6;
	    minvel=6;
	    }

	GetScreenSize();

	/* Get GIF stuff
	*/
	max_y=height-gifh-1;
	start_x=cx-(gifw*no)/2;

	gif=new Image[no];

	for(f=0;f<no;f++)
	    {
	    showStatus("Loading " + FName(f));
	    gif[f]=this.getImage(this.getDocumentBase(),FName(f));
	    }

	posy=new int[no];
	posyi=new int[no];

	fl=true;

	for(f=0;f<no;f++)
	    {
//  	    if (fl)
//  		{
//  		posy[f]=max_y;
//  		posyi[f]=-vel;
//  		}
//  	    else
//  		{
//  		posy[f]=0;
//  		posyi[f]=0;
//  		}

	    posy[f]=max_y-(f*f*2);
	    posyi[f]=0;

	    fl=!fl;
	    }
    }


    public void start() 
    {
	if (task==null)
	    {
	    task=new Thread(this);
	    task.start();
	    }
    }

    //public void paint(Graphics g) 
    //{
	// Leave the drawing to the thread.
    //}

    public void stop() 
    {
	destroy();
    }

    public void destroy() 
    {
	if (task!=null)
	    {
	    task.stop();
	    task=null;
	    }
	img=null;
    }
}
