import java.applet.*;
import java.awt.*;
import java.lang.*;

public class testAnimSprite extends Applet implements Runnable
{
	Playfield myfield;
	AnimatedSprite mysprite;
	Image buffer;
	Thread animate;
	
	/**
	 * startup code for testSprite
	 *
	 */
	public void init( )
	{
		Image img1,img2,img2a;
  		Raster tmpRast;
	
		img1=getImage(getDocumentBase(),"background.jpg");
		myfield=new Playfield(img1,1);
		//buffer=myfield.toImage();

		img2=getImage(getDocumentBase(),"animsprite.gif");
		mysprite=new AnimatedSprite(img2,2,1, 200,200,38,38);

		img2a=getImage(getDocumentBase(),"animspritealpha.gif"); // set alpha
		tmpRast=new Raster(img2a);
		mysprite.setAlpha(tmpRast);
		mysprite.addTrack(0,2);
		mysprite.addFrame(0,0,0,10,-10);
		mysprite.addFrame(0,1,1,-5,5);
		
		myfield.addSprite(0,mysprite);
		//myfield.draw();
    		renderPlayfield();
		start();
	}
	
	public void start( )
	{
		animate=new Thread(this);	// fire up the thread!
		animate.start();
	}

	public void stop( )
	{
		animate=null;	// release the thread
	}

	public void run( )
	{
		int i=10;
		while(i-->0)
		{
			try
			{
				animate.sleep(500);
				mysprite.nextFrame();
			}
			catch(InterruptedException e)
			{
			}
			renderPlayfield();
		}
	}
	
	public void renderPlayfield( )
	{
		myfield.draw();
		buffer=myfield.toImage();
		repaint();
	}
	
	public void paint(Graphics g)
	{
	    g.drawImage(buffer,0,0,this);
	}
	
	public void update(Graphics g)
	{
	    g.drawImage(buffer,0,0,this);
	}

//	public boolean mouseDown(Event e, int x, int y)
	//{
//		if(animate != null)
//		{
//			stop();	// halt the animation
//			return true;
//		}
			
		//if(mysprite.activePixelP(x,y))
		//{
		//	mysprite.nextFrame();
//			renderPlayfield();
		//	return true;
		//}
			
		//mysprite.x=x;
		//mysprite.y=y;
//		renderPlayfield();
//		start();
		//return true;
	//}
}
