/*
* Sample, h_zoom.java
* For free use, copy, modify
* By La Trong Hung, latronghung@yahoo.com
* Last modify, 01/2001
* SUMMARY: Change size of image
*/

//=============================================================================================//

import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.net.*;

//=============================================================================================//

public class h_zoom extends Applet implements Runnable{
	private Thread th = null;
	private MediaTracker mt;
	private Graphics mainG;
	private Image mainImg;
	private Image img[];
	private String imgname[];
	private String hung, link;
	private int numimg;
	private int w, h, wid, hei;
	private int x, y;
	private final int step = 30;
	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 int  mouseX, mouseY;				// Mouse position

//=============================================================================================//
  public h_zoom(){};

  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("numimg");
	 numimg = (Bean != null) ? Integer.parseInt(Bean) : 3;
		imgname = new String[numimg];
		img = new Image[numimg];
	 for (int i = 0; i < numimg; i++)
		imgname[i] = getParameter("imgname"+ i);
	 mainImg = createImage(size().width, size().height);
	 mainG = mainImg.getGraphics();
	 	mouseX = -1;
	 	mouseY = -1;
	 	linkImg = createMesImg(mesString);
	w = size().width; if((w % step) != 0) w = w + (step - w % step);
	h = size().height; if((h % step) != 0) h = h + (step - h % step);
	wid = 0; hei = 0;
  }
  private void LoadImg(){
		int i;
       		mt = new MediaTracker(this);
		for (i = 0; i < numimg; i++){
			img[i] = getImage(getCodeBase(), imgname[i]);
			mt.addImage(img[i], 1);
		}
       		try{
           		mt.waitForAll();
       		}
       		catch(InterruptedException e){};
		Init_Complete = true;
  }
  private void createMainImg(){
	if(Init_Complete == false){
		repaint();
		LoadImg();
	}
	else{
		x = (int)((w - wid)/2);
		y = (int)((h - hei)/2);
		mainG.drawImage(img[0], 0, 0, w, h, this);
		mainG.drawImage(img[1], x, y, wid, hei, this);   
		limFlag();
	}
	if(mouseX >= 0)
		mainG.drawImage(linkImg, mouseX, mouseY-16, this);
  }
  private void limFlag(){
		wid += (int)(w/step); hei += (int)(h/step);
		if(wid > w){
			wid = 0; hei = 0;
			Image thay;
			thay = img[0];
			for (int i = 0; i < numimg; i++){
				if (i == numimg-1) img[i] = thay;
				else img[i] = img[i+1];
			}
			thay = null;
		}
  }
  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(100);
		}
		catch(InterruptedException E) {
			E.printStackTrace();
		}
        }
   }
//____________________________________Applet Information_____________________________________//
   public String getAppletInfo() {
		return "Name: simpleanimation\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" },
			{ "imgnamei", "A String value", "Name of Image[i]" },
			{ "numimg", "An Integer value", "Number of Images" },
		};
		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;
	}
}
