/*

Java Graphics Demos: 

by Orion Lawlor,  1/1999

olawlor@acm.org



This code is totally free, and

should be considered in the public domain.

Use and modify it at will.

*/

import java.applet.Applet;

import java.awt.*;

import java.awt.image.*;

//import image;//My "just the bytes" 2D image class

//import Image2image;//Creates a "just the bytes" image from a Java Image.

class bouncer

{

	double x,y;//Location

	double w,h;//Size

	double vx,vy;//Velocity

	

	//Return a random number on [min,max)*/

	double rand(double min,double max)

		{return min+Math.random()*(max-min-0.0000001);}

	

	image img;

	public void init(int screenW,int screenH,image Nimg)

	{

		img=Nimg;

		w=img.w;h=img.h;

		vx=rand(-screenW/30.0,screenW/30.0);

		vy=rand(-screenH/30.0,screenH/30.0);

		x=rand(0,screenW-w);

		y=rand(0,screenH-h);

	}

	

	public void advance(int screenW,int screenH,double gravity,double drag,double elasticity)

	{

		//Apply drag.  Drag=0 -> no drag; drag=1 -> no motion

		vx*=(1.0-drag);

		vy*=(1.0-drag);

		//Accellerate downward (gravity)

		vy+=gravity;

		//Move with new velocity

		x+=vx;

		y+=vy;

		//Bounce off screen edges, if need be

		if (x<0) {x-=vx;vx*=-elasticity;}

		if (x+w>=screenW) {x-=vx;vx*=-elasticity;}

		if (y<0) {y-=vy;vy*=-elasticity;}

		if (y+h>=screenH) {y-=vy;vy*=-elasticity;}

	}

	public image getImage() {return img;}

	public Rectangle getRect() {return img.getRect((int)x,(int)y);}

}



class DemoInt 

{

	image dest;

	int nBounce=4;

	bouncer bouncers[]=new bouncer[nBounce];

	public void init(int pix[],int w,int h,Applet app)

	{

		dest=new image();

		dest.init(pix,w,h,0);

		

		Image2image img1=new Image2image();

		img1.init(app.getImage(app.getCodeBase(),"home.jpg"));

		img1.andBy(0xfeFEfeFF);

		

		Image2image img2=new Image2image();

		img2.init(app.getImage(app.getCodeBase(),"furred.jpg"));

		img2.andBy(0xfeFEfeFF);

		

		bouncers[0]=new bouncer();

		bouncers[0].init(w,h,img1);

		

		for (int b=1;b<nBounce;b++)

		{

			bouncers[b]=new bouncer();

			bouncers[b].init(w,h,img2);

		}

	}

	public void fillBuffer(int pix[])

	{

		int x,y;

		bouncers[0].advance(dest.w,dest.h,0.1,0.0,1.0);

		

		dest.fill(0);

		Rectangle r=bouncers[0].getRect();

		image i=bouncers[0].getImage();

		int inPix[]=i.pix,width=r.width;

		for (y=0;y<r.height;y++)

		{

			int inDex=y*i.scan;

			int outDex=(y+r.y)*dest.scan+r.x;

			for (x=0;x<width;x++)

				pix[outDex+x]=inPix[inDex+x];

		}

		

		for (int b=1;b<nBounce;b++)

		{

			bouncers[b].advance(dest.w,dest.h,0.1,0.0,1.0);

			r=bouncers[b].getRect();

			i=bouncers[b].getImage();

			inPix=i.pix;width=r.width;

			for (y=0;y<r.height;y++)

			{

				int inDex=y*i.scan;

				int outDex=(y+r.y)*dest.scan+r.x;

				for (x=0;x<width;x++)

					pix[outDex+x]=0xfeFEfeFF&((inPix[inDex+x]+pix[outDex+x])>>1);

			}

		}

		/* Limit animation speed to 30fps */
		try {Thread.sleep(30);} catch (InterruptedException e) {}
	}

}

