/*Erin PiateskiPd. 7 Graphics5-7-98Java applet which uses sprites to move pictures of 2 Gargoyles against a background.When a collision with a wall is detected, they bounce off.  When a collision againsteach other is detected, there is an explosion and the background changes.  Theirstarting positions and movement against the new background are randomized. */import java.applet.Applet;import java.awt.*;import java.awt.event.*;import java.util.*;public class drawsprite extends Applet implements Runnable{//The main program. 	Image goliath,broadway,buffer,back,temp,exp;	Graphics grdraw;	usesprite bob,fred;  //sprites for the images of Goliath and Broadway	collisions coll;	Thread runner=null;	Random randnum;		//Beginning x and y position and movement values for the sprites	int starter, startery, stmovx, stmovy,starter2, startery2, stmovx2, stmovy2;		int yescoll;		public void init()	{		//initializes necessary variables and creates classes.				yescoll=0;		goliath=getImage(getDocumentBase(),"goliath.gif");		//load needed images		broadway=getImage(getDocumentBase(),"broadway.gif");		exp=getImage(getDocumentBase(),"exp.gif");		back=getImage(getDocumentBase(),"cartoon.gif");		temp=getImage(getDocumentBase(),"blair.gif");				buffer=createImage(768,576);		grdraw=buffer.getGraphics();		grdraw.setColor(Color.black);				randnum=new Random();						//generate random values for sprite position and movement		starter=Math.abs(randnum.nextInt()%626);		startery=Math.abs(randnum.nextInt()%380);		stmovx=Math.abs(randnum.nextInt()%10);		stmovy=Math.abs(randnum.nextInt()%10);		starter2=Math.abs(randnum.nextInt()%626);		startery2=Math.abs(randnum.nextInt()%380);		stmovx2=Math.abs(randnum.nextInt()%10);		stmovy2=Math.abs(randnum.nextInt()%10);				bob = new usesprite(starter,startery,stmovx,stmovy);		fred= new usesprite(starter2,startery2,stmovx2,stmovy2);		coll=new collisions(bob,fred,this);	}	public void paint(Graphics g)  //calls update	{		update(g);	}		public void Collide()	{	Image swap;		//swaps backgrounds using a temporary variable					//tells the program there's been a collision				yescoll=1;		swap=back;		back=temp;		temp=swap;	}			public void update(Graphics g)	{	/* Paints the animation images on the screen */			if(yescoll==0)		//if no collision, draws normally		{			grdraw.drawImage(back,0,0,this);			grdraw.drawImage(broadway, bob.xpos, bob.ypos,this);			grdraw.drawImage(goliath, fred.xpos, fred.ypos,this);			g.drawImage(buffer,0,0,this);		}		else		//if collision, draws the explosion		{			grdraw.drawImage(exp,0,0,this); 			g.drawImage(buffer,0,0,this);			try{Thread.sleep(200);} 			catch(InterruptedException e){};			yescoll=0;			}	}			public void start()	{	/* Begins the running of the thread for the animation. */			if(runner==null)		{			runner=new Thread(this);			runner.start();		}	}		public void stop()	{	/* Stops the running of the thread for the animation. */			if (runner!=null && runner.isAlive())			runner.stop();		runner=null;	}		public void run()	{	/* implements the painting of the sprite images with a 	lag time to slow it down. */			int i;				i=0;		while(true)		{			repaint();						try{Thread.sleep(20);} 			catch(InterruptedException e){};		}	}}public class collisions implements Runnable{/* Class for collision detection */	Thread runner=null;		//widths and heights of the edges of the sprite images	int endwidb,endheib,endwidf,endheif;			usesprite bob,fred;	drawsprite mainpro;		public collisions(usesprite bob1,usesprite fred1,drawsprite prog)	{	//Constructor for the collisions class.			bob=bob1;		fred=fred1;		mainpro=prog;		start();	}		public void start()	{	/* Begins the running of the thread for the collision detection. */			if(runner==null)		{			runner=new Thread(this);			runner.start();		}	}		public void stop()	{	/* Stops the running of the thread for the collision detection. */			if (runner!=null && runner.isAlive())			runner.stop();		runner=null;	}		public void run()	{	/* Checks for collisions of the sprites*/	int collide;				while(true)		{			collide=0;						endwidf=fred.xpos+122;		//calculates positions of ends of sprites			endheif=fred.ypos+165;		//uses shrunken rectangles			endheib=bob.ypos+158;			endwidb=bob.xpos+114; 						//check to see if edges of the sprites overlap			if(((bob.xpos>fred.xpos)&&(bob.xpos<endwidf)) &&((bob.ypos>fred.ypos)&&(bob.ypos<endheif))) collide=1;			if(((endwidb>fred.xpos)&&(endwidb<endwidf)) &&((endheib>fred.ypos)&&(endheib<endheif))) collide=1;			if(((endwidb>fred.xpos)&&(endwidb<endwidf)) &&((bob.ypos>fred.ypos)&&(bob.ypos<endheif))) collide=1;			if(((bob.xpos>fred.xpos)&&(bob.xpos<endwidf)) &&((endheib>fred.ypos)&&(endheib<endheif))) collide=1;						if(collide==1)	//if there's a collision, chooses new random positions for the sprites							//and activates a function in the main program			{				mainpro.Collide();								bob.xpos=Math.abs(mainpro.randnum.nextInt()%626);				bob.ypos=Math.abs(mainpro.randnum.nextInt()%380);								fred.xpos=Math.abs(mainpro.randnum.nextInt()%626);				fred.ypos=Math.abs(mainpro.randnum.nextInt()%380);								bob.xmove=Math.abs(mainpro.randnum.nextInt()%10);				bob.ymove=Math.abs(mainpro.randnum.nextInt()%10);								fred.xmove=Math.abs(mainpro.randnum.nextInt()%10);				fred.ymove=Math.abs(mainpro.randnum.nextInt()%10);			} 			try{Thread.sleep(10);} 		//allow time before a new collision can occur			catch(InterruptedException e){};		}	}}public class usesprite implements Runnable{//Class for controlling sprite movement	int xpos,ypos, xmove, ymove;	Thread runner=null;		public usesprite(int startx, int starty, int xmov, int ymov)	{		//Constructor for the usesprite class.				xpos=startx;		ypos=starty;		xmove=xmov;		ymove=ymov;		start();	}		public void start()	{	/* Begins the running of the thread for the animation. */			if(runner==null)		{			runner=new Thread(this);			runner.start();		}	}		public void stop()	{	/* Stops the running of the thread for the animation. */			if (runner!=null && runner.isAlive())			runner.stop();		runner=null;	}		public void run()	{	/* implements the painting of the sprite images with a 	lag time to slow it down. */			int i;				i=0;		while(true) //endless loop to keep the program running		{			//change x and y positions						xpos=xpos+xmove;				ypos=ypos+ymove;						//if the sprite hits a wall, reverse x and y movement to make it bounce off			if((ypos<=0) || (ypos>=380))				ymove=-ymove;			if((xpos<=0) || (xpos>=626))				xmove=-xmove;					try{Thread.sleep(40);} 		//keeps the movement from being too fast			catch(InterruptedException e){};		}	}}
