
/*
* Applet sample - Simple animation effect by Handle pixel
* This software is for free use, copy, modify
* By La Trong Hung, latronghung@yahoo.com
* Last modified, 12/2000
* 	Summary:
*	Load base Image - grab pixel - handle pixels - create highlight image - 
*	draw base image and highlight image randomly.
*/

//===========================================================================================//

import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.lang.*;
import java.net.*;
import java.util.Random;

//===========================================================================================//

public class h_blink extends Applet implements Runnable{

	private Random rand;
	private Thread th = null;
	private MediaTracker mt;  
	private MemoryImageSource memS;				// MemoryImageSource for creating highlight image (lightImg)
	private Image img, lightImg, mainImg;			// 3 Image object needed in this applet. img: base image, lightImg: highlight of img, mainImg: image will be drawn to applet window
	private Graphics mainG;
	private int randFlag;
	private int h_Iw, h_Ih;
	private int pix[];					// Array to store all pixels of img, and will be operated to create lightImg
	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_blink(){}

   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_blink.jpg";
   }
   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);
       		h_Ih = img.getHeight(this);
       		pix = new int[h_Iw*h_Ih];
		grabPix(img, pix, h_Iw, h_Ih);
   }
   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;
       		}
   }
   private void handlePix(){
	 	LoadImg();
		initSetting();
		highlight(1.5, h_Iw, h_Ih, pix);
           	Init_Complete = true;
   }
   private void initSetting(){
	 	rand = new Random();
         	mainImg = createImage(h_Iw, h_Ih);
         	mainG = mainImg.getGraphics();
         	memS = new MemoryImageSource(h_Iw, h_Ih, pix, 0, h_Iw);

	 	mouseX = -1;
	 	mouseY = -1;
	 	linkImg = createMesImg(mesString);
   }
   public void highlight(double contrast, 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 *= contrast; if(r > 255) r = 255;
             g *= contrast; if(g > 255) g = 255;
             b *= (3*contrast/2); if(b > 255) b = 255;
             pix[i] = (r << 16) | (g << 8) | b | 0xFF000000;
       }
   }
   private void createblink(){
		if(Init_Complete == false){
			repaint();
			handlePix();
			lightImg = createImage(memS);
	 	      		mt.addImage(lightImg, 1);
 		      		try {
 		          		mt.waitForID(1);
 		      		}
 		      		catch(InterruptedException e){};
		}
		randFlag = Math.abs(rand.nextInt());
		if(randFlag % 3 == 0){
			mainG.drawImage(lightImg, 0, 0, h_Iw, h_Ih, this);
		}
		else{
			mainG.drawImage(img, 0, 0, h_Iw, h_Ih, this);
		}
		if(mouseX >= 0)
			mainG.drawImage(linkImg, mouseX, mouseY-16, this);
   }
//____________________________________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 {
  	             createblink();
                     repaint();
                     Thread.sleep(50);
                     System.gc();
            	} 
            	catch(InterruptedException E) {
                     E.printStackTrace();
            	}
       	}
   }
//____________________________________Applet Information_____________________________________//
   public String getAppletInfo() {
		return "Name: h_blink\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" },
		};
		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;
	}
}
