
/*
* Created 01/1998
* Sample, h_flag2.java, compact version.
* For free use, copy, modify
* By La Trong Hung, latronghung@yahoo.com
* Last modified, 01/2001
* 	 Summary: Other way to create flag by using pixel_handle
*	 Create necessary images -> createWaveImg
*		+ Create image to wave (Defalt: Vietnam Flag)
*		+ Wave image
*	 Create main image to draw into applet window -> createMainImg()
*		+ Slide bgimage
*		+ Draw waved image
*/

//===========================================================================================//

import java.applet.*;
import java.awt.*;
import java.net.*;
import java.awt.image.*;

//===========================================================================================//

public class h_flag2 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
//h_flag itself
	private Image waveImg[];
	private Image bgImg;
	private String imgName, bgImgName;
	private int h_Iw, h_Ih, w, h;
	private int i, m = 0, m1 = 0;
	private int delay;
	private Color myC;
	private final double pi = 0.0174533;

//---------------------------------Initialization------------------------------------------//
   public h_flag2(){}

   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 : null;
         Bean = getParameter("bgimagename");
         	bgImgName = (Bean != null) ? Bean : "bgimg.jpg";
   }
   private void createWaveImg(){				// create waveImg
								// loading
		Image img1=null, img2 = null;
		Graphics g;
       		mt = new MediaTracker(this);
		img2 = getImage(getCodeBase(), bgImgName);
			mt.addImage(img2, 0);
		if(imgName != null){
       			img1 = getImage(getCodeBase(), imgName);
           		mt.addImage(img1, 0);
		}
       		try {
           		mt.waitForAll();
       		}
       		catch(InterruptedException e){};
		bgImg = createImage(w, h);
		g = bgImg.getGraphics();
		g.drawImage(img2, 0, 0, w, h, this);
								// create image for waving
			img2 = createImage(h_Iw, h_Ih);
			g = img2.getGraphics();
			int a[] = {(int)(0.5*h_Iw), (int)(3.0*h_Iw/5.0), (int)(h_Iw/3.0), (int)(2.0*h_Iw/3.0), (int)( 2.0*h_Iw/5.0), (int)( 0.5*h_Iw)};
			int b[] = {(int)(h_Ih/5.0  ), (int)(2.0*h_Ih/3  ), (int)(h_Ih/3.0+3.0), (int)(h_Ih/3.0+3.0), (int)(2.0*h_Ih/3.0  ), (int)(h_Ih/4.0  )};
			int a1[] = {(int)( 0.5*h_Iw), (int)( 3.0*h_Iw/5.0), (int)( h_Iw/3.0), (int)( 0.5*h_Iw), (int)( 0.5*h_Iw)};
			int b1[] = {(int)(h_Ih/5.0  ), (int)(2.0*h_Ih/3.0  ), (int)(h_Ih/3.0+3.0), (int)(h_Ih/3.0+3.0), (int)(h_Ih/4.0  )};
			g.setColor(new Color(200, 0, 0));
			g.fillRect(0, 0, h_Iw, h_Ih);
			g.setColor(Color.yellow);
			g.fillPolygon(a, b, a.length);
			g.fillPolygon(a1, b1, a1.length);
			if(imgName != null){
				g.drawImage(img1, 0, 0, h_Iw, h_Ih, this);
			}
								// waves
		int pix[], wpix[], pix1[];
       		pix1 = new int[h_Iw*h_Ih];
       		pix = new int[h_Iw*h_Ih];
		grabPix(img2, pix1, h_Iw, h_Ih);
		MemoryImageSource wmemS;
		int ik, y, x, dx, dy;
		for(i = 0; i < 360; i += 30){
			wpix = new int[h_Iw*(h_Ih + 25)];
			wmemS = new MemoryImageSource(h_Iw, (h_Ih + 25), wpix, 0, h_Iw);
			ik = i/30;
			System.arraycopy(pix1, 0, pix, 0, pix1.length);
			makeLight(i, h_Iw, h_Ih, pix);

			y = 0;
       			for(int j=0; j<h_Iw*(h_Ih + 25); j++){
				if((j != 0) && (j % h_Iw== 0)) y++;
				x = j - y*h_Iw;
				dx = (int)(0.05*x*Math.sin(((double)i - 4.5*x)*pi) + 3*Math.sin(((double)i - 4.5*y)*pi + 0.7854));
				dy = (int)(0.1*x + 0.09*x*Math.sin(((double)i - 4.5*x)*pi));
				if((x - dx + 8 >= 0) && (x - dx + 8 < h_Iw) &&  (y - dy >= 0) && (y - dy < h_Ih)){
             				wpix[j] = pix[(x - dx + 8) + (y - dy)*h_Iw];
				}
       			}
			waveImg[ik] = createImage(wmemS);
			mt.addImage(waveImg[ik], ik);
			try{
				mt.waitForID(ik);
			}
			catch(InterruptedException e) {};
		}
		g.dispose();
		wmemS = null;
		img2 = null;
		img1 = 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 makeLight(int runItem, int w, int h, int[] pix){
	int i, r, g, b;
	int y = 0, x, del;
       for(i=0; i<w*h; i++){
		if((i != 0) && (i % h_Iw == 0)) y++;
		x = i - y*h_Iw;
		del = (int)(0.35*x*Math.sin(((double)runItem - 4.5*x)*pi));	//From z1
		if(del > 0) del = 0;
             r = (pix[i] >> 16) & 0xFF;
             g = (pix[i] >>  8) & 0xFF;
             b = (pix[i]      ) & 0xFF;
             r += del; if(r < 0) r = 0; if(r > 255) r = 255;
             g += del; if(g < 0) g = 0; if(g > 255) g = 255;
             b += del; if(b < 0) b = 0; if(b > 255) b = 255;
             pix[i] = (r << 16) | (g << 8) | b | 0xFF000000;
       }
   }
   private void initSetting(){
		w = size().width;
		h = size().height;
		waveImg = new Image[12];
       		h_Iw = 130;
       		h_Ih = 90;
		myC = new Color(102, 102, 153);
		createWaveImg();
		delay = 50;
         	mainImg = createImage(w, h);
         	mainG = mainImg.getGraphics();

	 	mouseX = -1;
	 	mouseY = -1;
	 	linkImg = createMesImg(mesString);
           	Init_Complete = true;
   }

   private void createMainImg(){
		if(Init_Complete == false){
			repaint();
			initSetting();
		}
		mainG.setColor(myC);
		mainG.fillRect(0, 0, w, h);
		mainG.drawImage(bgImg, m1, 0, this);
		mainG.drawImage(bgImg, m1 - w, 0, this);
		mainG.drawImage(waveImg[m], 20, 20, this);
/*
		mainG.setColor(new Color(204, 153, 0));
		mainG.drawLine(0, 21, 23, 21);
		mainG.drawLine(0, 108, 23, 108);
		mainG.setColor(new Color(170, 120, 0));
		mainG.drawLine(0, 20, 23, 20);
		mainG.drawLine(0, 22, 23, 22);
		mainG.drawLine(0, 107, 23, 107);
		mainG.drawLine(0, 109, 23, 109);
*/
//		mainG.drawString(""+m, 30, 30);
		if(mouseX >= 0)
			mainG.drawImage(linkImg, mouseX, mouseY-16, this);
		m++; if(m >= 12) m = 0;
		m1++; if(m1 > (w - 1)) m1 = 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_flag2\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" },
			{ "bgimagename", "A String value", "Name of background image" },
		};
		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;
	}
}
