
/*
* Created 01/1998
* Sample, h_lake.java, compact version. (The applet is popular in web sites)
* For free use, copy, modify
* By La Trong Hung, latronghung@yahoo.com
* Last modified, 01/2001
* 	 SUMMARY:
*	 Create all necessary images -> createWaveImg
*		+ upImg
*		+ waveImg: make "wave area" more blue -> wave it -> draw overlay
*			# Plane wave (1D): Aplitude*Math.cos(omega*t - wavenumber*x)
*	 Create main image to draw into the applet window -> createMainImg()
*/

//===========================================================================================//

import java.applet.*;
import java.awt.*;
import java.lang.*;
import java.net.*;
import java.awt.image.*;

//===========================================================================================//

public class h_lake extends Applet implements Runnable{

	private Thread th = null;
	private MediaTracker mt;  
	private Image mainImg;
	private Graphics mainG;
	private boolean Init_Complete = false;          	// Flag that confirms the all initial methods are 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 String hung, link;
	private int  mouseX, mouseY;				// Mouse position

	private Image img, layImg, waveImg, upImg;
	private String imgName, layImgName;
	private int h_Iw, h_Ih;
	private int wavePos, wlcontrol;				// Position for waving image from there
	private int i, m = 0;
	private int delay;
	private int deltaColor;
	private final double pi = 0.0174533;

//---------------------------------Initialization------------------------------------------//
   public h_lake(){}

   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 : "wave.jpg";
         Bean = getParameter("layimagename");
         	layImgName = (Bean != null) ? Bean : null;
         Bean = getParameter("waveposition");
         	wavePos = (Bean != null) ? Integer.parseInt(Bean) : 100;
         Bean = getParameter("wavelengthcontrol");
         	wlcontrol = (Bean != null) ? Integer.parseInt(Bean) : 10;
				if(wlcontrol < 0) wlcontrol = 0;
         Bean = getParameter("deltacolor");
         	deltaColor = (Bean != null) ? Integer.parseInt(Bean) : 14;
   }
   private void createWaveImg(){
       		mt = new MediaTracker(this);
       		img = getImage(getCodeBase(), imgName);
           		mt.addImage(img, 0);
		if(layImgName != null){
       			layImg = getImage(getCodeBase(), layImgName);
           		mt.addImage(layImg, 0);
		}
       		try {
           		mt.waitForAll();
       		}
       		catch(InterruptedException e){};
       		h_Iw = img.getWidth(this);
       		h_Ih = img.getHeight(this);
								// create upImg
		Graphics g;
			upImg = createImage(h_Iw, wavePos + 2);
			g = upImg.getGraphics();
			g.drawImage(img, 0, 0, this);
								// Make "waved area" more blue
		Image im;
			im = createImage(h_Iw, h_Ih - wavePos);
			g = im.getGraphics();
			g.drawImage(img, 0, -wavePos, this);
		int pix[];
       			pix = new int[h_Iw*(h_Ih - wavePos)];
			grabPix(im, pix, h_Iw, h_Ih - wavePos);
			makeBlue(deltaColor, h_Iw, h_Ih - wavePos, pix);
		MemoryImageSource memS;
         		memS = new MemoryImageSource(h_Iw, h_Ih - wavePos, pix, 0, h_Iw);

		im = null;
		im = createImage(memS);
	 	      		mt.addImage(im, 1);
 		      		try {
 		          		mt.waitForID(1);
 		      		}
 		      		catch(InterruptedException e){};
		waveImg = createImage(13*h_Iw, h_Ih - wavePos);
		g = waveImg.getGraphics();
		g.drawImage(im, 0, 0, this);
								// Create waveImg
		int findex, k;
		double d1, d2, waveNumber;
		for(i = 0; i < 360; i += 30){
			findex = (i/30 + 1)*h_Iw;
			g.drawImage(img, findex, -wavePos, this);
			k = 0;
			for (int y = 0; y < h_Ih - wavePos; y++){
						// Plane wave (1D): cos(omega*t + wavenumber*y), in this case i*pi works as omega*t.
						// by changing waveNumber, it is posible to change wavelength.
				k++;
						// waveNumber = 20000.0/((double)k + 20.0);	//k*waveNumber || 200000.0/((double)k + 15.0)
				d1 = 2.6*Math.cos(((double)i + 8.0*k)*pi);
				d2 = (0.07*k + 2.0)*Math.cos(((double)i - 200000.0/((double)k + (double)wlcontrol))*pi) + d1;
				if(y - (int)d2 >= 0)
					g.copyArea(0, y - (int)d2, h_Iw, 1, findex, (int)d2);
			}
			if(layImgName != null){
				g.drawImage(layImg, findex, 0, this);
			}
		}
		mt.addImage(waveImg, 2);
	      		try {
 	         		mt.waitForID(2);
 	      		}
 	      		catch(InterruptedException e){};
		g.dispose();
		im = null;
		img = null;
		memS = null;
   }
   private void grabPix(Image im, int[] pix, int w, int h){
       		PixelGrabber h_PixGrab = new PixelGrabber(im, 0, 0, w, h, pix, 0, w);
       		try {
          		h_PixGrab.grabPixels();
       		}
       		catch (InterruptedException e){
           		System.out.println("grabber error" + e);
           		return;
       		}
       		if ((h_PixGrab.status() & ImageObserver.ABORT) != 0){
           		System.out.println("grabber error");
           		return;
       		}
   }
   public void makeBlue(int delta, int w, int h, int[] pix){
       int i, r, g, b;
       for(i=0; i<w*h; i++){
             r = (pix[i] >> 16) & 0xFF;
             g = (pix[i] >>  8) & 0xFF;
             b = (pix[i]      ) & 0xFF;
             r -= delta; if(r < 0) r = 0;
             g += delta/2; if(g > 255) g = 255;
             b += delta; if(b > 255) b = 255;
             pix[i] = (r << 16) | (g << 8) | b | 0xFF000000;
       }
   }

   private void initSetting(){
		createWaveImg();
		delay = 50;
         	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();
			initSetting();
		}
		mainG.drawImage(waveImg, -(m + 1)*h_Iw, wavePos, this);
		mainG.drawImage(upImg, 0, 0, this);
		if(mouseX >= 0)
			mainG.drawImage(linkImg, mouseX, mouseY-16, this);
		m++; if(m >= 12) m = 0;
   }
//____________________________________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(delay);
                     System.gc();
            	} 
            	catch(InterruptedException E) {
                     E.printStackTrace();
            	}
       	}
   }
//____________________________________Applet Information_____________________________________//
   public String getAppletInfo() {
		return "Name: h_lake\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" },
			{ "layimagename", "A String value", "Name of overlay image" },
			{ "waveposition", "An Integer value", "The position to make wave from there" },
			{ "deltacolor", "An Integer value", "The value changes color of wave area" },
			{ "wavelengthcontrol", "An Integer value", "The value to control wavelength" },
		};
		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;
	}
}
