/* 
	Snow.java

	[ more java @ mlab - 10-17.1.2000 - juhuu@katastro.fi ]
*/

import java.applet.Applet;
import java.awt.*;
import java.net.*;
import java.util.*;
import Flake;

public class Snow extends Applet implements Runnable{	Image		imgDoubleBuffer;
	Graphics	gDoubleBuffer;
	Thread		engine = null;  	int			nMousex, nMousey;
	boolean 	bMouse_down;
		Image 		imgPic;
	Image 		imgBackground;

	int			nSnow_amount;
	Flake[]		flakes;
	int			nScr_width;
	int			nScr_height;
	public void init() {
		// initialize double buffering		imgDoubleBuffer = createImage(size().width, size().height);		gDoubleBuffer = imgDoubleBuffer.getGraphics();		gDoubleBuffer.setColor(Color.white);		gDoubleBuffer.fillRect(0, 0, size().width, size().height);

		nScr_width		= size().width;
		nScr_height		= size().height;
		nMousex 		= 0;
		nMousey 		= 0;
		bMouse_down		= false;

		// amount of snow flakes		nSnow_amount	= 450;
		// flakes
		flakes			= new Flake[nSnow_amount];
		// initialize flakes
		for (int i = 0 ; i < nSnow_amount; i++)		{
			flakes[i] = new Flake(nScr_width * Math.random(), nScr_height * Math.random());		}		// let's load the images
		imgPic				= null;

		String sBackground	= "background.jpg";
		String sPic			= "snow.jpg";

		MediaTracker med = new MediaTracker(this);

		imgPic = getImage(getDocumentBase(), sPic);
		med.addImage(imgPic, 0);
		imgBackground = getImage(getDocumentBase(), sBackground);
		med.addImage(imgBackground, 1);
		try		{			med.waitForAll();			}	catch (InterruptedException e) {		}  }

//----------------------------------------------------

	public void paint(Graphics g) {

	  // clear the screen

	//    	gDoubleBuffer.setColor(Color.white);
	//    	gDoubleBuffer.fillRect(0, 0, size().width, size().height);

		if (imgBackground != null)
		{
			gDoubleBuffer.drawImage(imgBackground, 0, 0, null);

			if (imgPic != null)
			{					int nX_coord;
				int nY_coord;

				for (int i = 0 ; i < nSnow_amount; i++)				{
					// get the current coordinate & parameters to change it					nX_coord = flakes[i].getX();
					nY_coord = flakes[i].getY();

					// draw the flake					gDoubleBuffer.drawImage(imgPic, nX_coord, nY_coord, null);

					// if the y coordinate is out of the screen area, create a new snow flake
					if(nY_coord > nScr_height)
					{						flakes[i] = new Flake(nScr_width * Math.random(), 0);					}
				}			}
		}

	  // draw all the stuff from the double buffer to the screen
	  g.drawImage(imgDoubleBuffer, 0, 0, null);				
	}


//----------------------------------------------------


	// start the applet
	public void start() {
		if (engine == null)		{
			engine = new Thread(this);
			engine.start();		}
		showStatus(getAppletInfo());
	} 

	// stop the applet
	public void stop() {
		if (engine != null && engine.isAlive())
		{
			engine.stop();
		}		engine = null; 
	}

	public void run()
	{                  
		while (true)
		{			try			{
				repaint();
				Thread.sleep(10);
			} catch (InterruptedException e) {
				stop();
			}
		}
	}

	public void update(Graphics g) {    
	  paint(g);
	}

	// print to the console & browserstatus
	private void print(String s) {
		System.out.println(s); 		// console
		showStatus(s);         		// browser status  
	}

//---

	public boolean mouseDown(Event e, int x, int y) {
		bMouse_down = true;
		return true;  
	}

	public boolean mouseUp(Event e, int x, int y) {
		bMouse_down = false;
		return true;  
	}

	public boolean mouseMove(Event e, int x, int y)
	{
		nMousex = x;
		nMousey = y;
		return true;  
	}

	public boolean mouseDrag(Event e, int x, int y)
	{
		nMousex = x;
		nMousey = y;
		return true;  
	}
//---
}
