
/*
* Sample, h_snow.java
* For free use, copy, modify
* By La Trong Hung, latronghung@yahoo.com
* Last modify, 01/2001
* SUMMARY:
*	Initial setting: generate random coordinates -> initSetting()
*		for(i = 0; i < numDot; i++){
*			x[i] = rand.nextInt() % h_Iw; if (x[i] < 0) x[i] = -x[i];
*			y[i] = rand.nextInt() % h_Ih; if (y[i] < 0) y[i] = -y[i];
*		}
*	Change coordinate: change coordinates of each dot everywhen the applet window is re-drawn -> setCoor()
*		for(i = 0; i < numDot; i++){
*			y[i] += Math.abs((rand.nextInt() % 3)) + 1;
*			if((y[i] >= 0) && (y[i] <= h_Ih/2))
*				x[i] += rand.nextInt() % 2;
*			if((y[i] > h_Ih/2) && (y[i] <= h_Ih))
*				x[i] += rand.nextInt() % 3;
*			if(y[i] > h_Ih)
*				y[i] = 0;
*			if(x[i] > h_Iw) x[i] = Math.abs(rand.nextInt() % h_Iw);
*			if(x[i] < 0) x[i] = Math.abs(rand.nextInt() % h_Iw);
*		}
*	Draw: Draw all dots by set coordinate -> createMainImg()
*		setCoor();
*		for(i = 0; i < numDot; i++){
*			mainG.setColor(Color.white);
*             		mainG.drawLine(x[i], y[i], x[i], y[i]);
*		}
*/

//===========================================================================================//

import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.net.*;
import java.util.Random;

//===========================================================================================//

public class h_snow extends Applet implements Runnable{

	private Random rand;
	private Thread th = null;
	private MediaTracker mt;  
	private Image img, mainImg;
	private Graphics mainG;
	private int h_Iw, h_Ih;
	private int numDot;
	private int x[], y[];
	private int constantY[];
	private int i;
	private String imgName;
	private String hung, link;
	private boolean Init_Complete = false;          	// Flag that confirms the handlePix() method is completely worked.
	private boolean thSuspended = false;            	// Flag that confirm the thread th is in suspended or resumed state.

	private Image linkImg;			      		// message
	private String mesString;				// Message String
	private int  mouseX, mouseY;				// Mouse position

//---------------------------------Initialization------------------------------------------//
   public h_snow(){}

   public void init(){
         						String Bean;
         Bean = getParameter("link");
         	link = (Bean != null) ? Bean : null;
		mesString = (link != null) ? link : "Sample by La Trong Hung";
         Bean = getParameter("hung");
         	hung = (Bean != null) ? Bean : "_self";
         Bean = getParameter("imagename");
         	imgName = (Bean != null) ? Bean : "h_snow.jpg";
	 Bean = getParameter("numberdot");
	 	numDot = (Bean != null) ? Integer.parseInt(Bean) : 400;
   }
   private void LoadImg(){
       		mt = new MediaTracker(this);
       		img = getImage(getCodeBase(), imgName);
           		mt.addImage(img, 0);
       		try {
           		mt.waitForID(0);
       		}
       		catch(InterruptedException e){};

       		h_Iw = img.getWidth(this); if(h_Iw == 0) h_Iw = size().width;
       		h_Ih = img.getHeight(this); if(h_Ih == 0) h_Ih = size().height;
   }
   private void initSetting(){
	 	rand = new Random();
		x = new int[numDot];
		y = new int[numDot];
		constantY = new int[numDot];
		for(i = 0; i < numDot; i++){
			x[i] = rand.nextInt() % h_Iw; if (x[i] < 0) x[i] = -x[i];
			y[i] = rand.nextInt() % h_Ih; if (y[i] < 0) y[i] = -y[i];
		}
         	mainImg = createImage(h_Iw, h_Ih);
         	mainG = mainImg.getGraphics();

	 	mouseX = -1;
	 	mouseY = -1;
	 	linkImg = createMesImg(mesString);
		Init_Complete = true;
   }
   private void createMainImg(){
		if(Init_Complete == false){
			repaint();
			LoadImg();
			initSetting();
		}
		mainG.setColor(Color.black);
		mainG.fillRect(0, 0, h_Iw, h_Ih);
		mainG.drawImage(img, 0, 0, this);
		setCoor();
		for(i = 0; i < numDot; i++){
			mainG.setColor(Color.white);
              		mainG.drawLine(x[i], y[i], x[i], y[i]);
		}
		if(mouseX >= 0)
			mainG.drawImage(linkImg, mouseX, mouseY-16, this);
   }
   private void setCoor(){
		for(i = 0; i < numDot; i++){
			y[i] += Math.abs((rand.nextInt() % 3)) + 1;
			if((y[i] >= 0) && (y[i] <= h_Ih/2))
				x[i] += rand.nextInt() % 2;
			if((y[i] > h_Ih/2) && (y[i] <= h_Ih))
				x[i] += rand.nextInt() % 3;
			if(y[i] > h_Ih)
				y[i] = 0;
			if(x[i] > h_Iw) x[i] = Math.abs(rand.nextInt() % h_Iw);
			if(x[i] < 0) x[i] = Math.abs(rand.nextInt() % h_Iw);
		}
   }
//____________________________________State of thread_______________________________________//
   public void start(){
      	if(th == null){
         	th = new Thread(this);
         	th.start();
      	}
   }

   public void stop(){
      	if(th != null){
          	th.stop();
          	th = null;
      	}
   }

   public void destroy(){
        //All destroy
   }

   public void update(Graphics g){
  	    	paint(g);
   }

   public void paint(Graphics g){
	if(Init_Complete == true){
	    	g.drawImage(mainImg, 0, 0, this);
	}
	else{
		g.setColor(Color.red);
		g.drawString("Initializing...", 10, 20);
	}
   }

   public void run(){
       	while(Thread.currentThread() == th){  
           	try {
  	             createMainImg();
                     repaint();
                     Thread.sleep(20);
                     System.gc();
            	} 
            	catch(InterruptedException E) {
                     E.printStackTrace();
            	}
       	}
   }
//____________________________________Applet Information_____________________________________//
   public String getAppletInfo() {
		return "Name: h_snow\r\n" + "Author: La Trong Hung\r\n" + "Created with JDK 1.02";
   }

   public String[][] getParameterInfo()
   {
		String[][] info =
		{
			{ "link", "A String value", "URL to jump" },
			{ "imagename", "A String value", "Name of Image" },
			{ "nunberdot", "An Integer value", "Name of Dot" },
		};
		return info;		
   }
//____________________________________Mouse activity_________________________________________//
   public boolean mouseDown(Event evt, int x, int y){
      	if (link != null){
	         try{
        		    URL url = new URL(getDocumentBase(), link);
		            getAppletContext().showDocument(url, hung);
		            if (hung.equals("_self")){
               				stop();
            	 	    }
         	 }
         	 catch (MalformedURLException E){
            		    E.printStackTrace();
         	 }
      	}
      	else{
		 if (thSuspended) th.resume();
		 else  th.suspend();
		 thSuspended = !thSuspended;
      	}
      	return(true);
   }
   public boolean mouseEnter(Event evt, int x, int y){
         	showStatus(mesString);
		mouseX = x;
		mouseY = y;
      		return(true);
   }
   public boolean mouseMove(Event evt, int x, int y){
		mouseX = x;
		mouseY = y;
		return(true);
   }
   public boolean mouseExit(Event evt, int x, int y){
         	showStatus("");
		mouseX = -1;
		mouseY = -1;
      		return(true);
   }
//_____________________________________Message______________________________________________//
	public Image createMesImg(String mes){
		Image linkImage;
		Graphics g;
		int mw;
		int mh = 16;
		mw = 6*mes.length();
		linkImage = createImage(mw, mh);
		g = linkImage.getGraphics();
		g.setColor(new Color(255, 255, 190));
		g.fillRect(0, 0, mw, mh);
		g.setColor(Color.black);
		g.drawRect(0, 0, mw-1, mh-1);
		g.drawString(mes, 4, 12);
		return linkImage;
	}
}
