/*
* Created 01/1998
* Sample, h_calendar.java, release version, compact source
* For free use, copy, modify (noncommercial)
* By La Trong Hung, latronghung@yahoo.com
* Last modify, 01/2001
* SUMMARY: The other sample of time effect, see h_clock.java for detail
*	1) Create background for h_calendar (Not be changed everywhen re-draw) -> createCalBg()
*	2) Get date -> getDat()
*	3) Draw date -> createMainImg()
*/

//===========================================================================================//

import java.applet.*;
import java.awt.*;
import java.lang.*;
import java.util.*;
import java.net.*;

//===========================================================================================//

public class h_calendar extends Applet implements Runnable{

	private Thread th = null;
	private Image mainImg;
	private Graphics mainG;
	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
// for h_calender itself
	private MediaTracker mt;
	private Image bgImg, img;
	private int i, k;
	private int w, h;
	private Color bgC, myC, myC1;
	private String imgname;
	private String thu[];
	private int colIndex, numdate;
	private int dat, mon, yy, thu1;
	private String mmyyS, datS;
	private int hh, mm;					// Integer values of hour, min..
	private String hhS, mmS;
	private int coun = 0;
	private Date d;

//---------------------------------Initialization------------------------------------------//
   public h_calendar(){}

   public void init(){
         						String Bean;
         Bean = getParameter("link");
         	link = (Bean != null) ? Bean : null;
		mesString = (link != null) ? link : "Sample by Hung";
         Bean = getParameter("hung");
         	hung = (Bean != null) ? Bean : "_self";
         Bean = getParameter("imagename");
         	imgname = (Bean != null) ? Bean : "calendar.gif";
	 bgC = new Color(136, 136, 187);
	 try{
		bgC = new Color(Integer.parseInt(getParameter("bgcolor"), 16));
	 }
	 catch (Exception E) { }
   }
   private void LoadImg(){
       		mt = new MediaTracker(this);
       		img = getImage(getCodeBase(), imgname);
           		mt.addImage(img, 0);
       		try {
           		mt.waitForID(0);
       		}
       		catch(InterruptedException e){};
   }
   private void initSetting(){
		w = 154;
		h = 228;
		myC = new Color(255, 0, 100);
		myC1 = new Color(40, 40, 90);
		thu = new String[7];
			thu[0] = "Su";
			thu[1] = "Mo";
			thu[2] = "Tu";
			thu[3] = "We";
			thu[4] = "Th";
			thu[5] = "Fr";
			thu[6] = "Sa";
         	mainImg = createImage(w, h);
         	mainG = mainImg.getGraphics();

	 	mouseX = -1;
	 	mouseY = -1;
	 	linkImg = createMesImg(mesString);
		createCalBg();
		Init_Complete = true;
   }
   private void createMainImg(){
		if(Init_Complete == false){
			repaint();
			initSetting();
		}
		mainG.drawImage(bgImg, 0, 0, this);
		getDat();
 // Format YY, MM
		if(yy < 2000) yy = yy + 1900;
		if(mon == 1){
			if((yy%4 == 0) && (yy%100 != 0) || (yy%400 == 0))
				numdate = 29;
			else
				numdate = 28;
		}
		else if((mon == 0) || (mon == 2) || (mon == 4) || (mon == 6) || (mon == 7) || (mon == 9) || (mon == 11))
			numdate = 31;
		else
			numdate = 30;

		mon = mon + 1;
			mmyyS = "YY" + " " + yy + " " + "MM" + " " + mon;
		mainG.setFont(new Font("TimesRoman", Font.PLAIN, 11));
		mainG.setColor(myC1);
		mainG.drawString(mmyyS, 5, 108);
		mainG.drawString(mmS, 135, 108);
		mainG.drawString(hhS, 120, 108);
		if((coun % 2) == 0)
			mainG.setColor(myC1);
		else
			mainG.setColor(Color.white);
		mainG.drawString(":", 131, 107);
// Draw mon, tue, wed...
		for(i=0; i<=6; i++){
			mainG.setFont(new Font("TimesRoman", Font.PLAIN, 12));
			if(i == 0)
				mainG.setColor(myC);
			else if(i == 6)
				mainG.setColor(Color.white);
			else
				mainG.setColor(myC1);
			mainG.drawString(thu[i], i*20+12, 122);
		}
// Draw date
		boolean me = true;
		if(dat <= 7){
			dat = dat + 7;
			me = false;
		}
		colIndex = (dat - thu1)%7;
		if(colIndex != 0)
			colIndex = 7 - colIndex;
		if(me == false)
			dat = dat - 7;
		k = 0;
		for(i = 1; i <= numdate; i++){    
			datS = "" + i;
			mainG.setFont(new Font("TimesRoman", Font.PLAIN, 11));
			if(colIndex == 0)
				mainG.setColor(myC);
			else if(colIndex == 6)
				mainG.setColor(Color.white);
			else
				mainG.setColor(myC1);
			if(i == dat){
				if((coun % 2) == 0)
					mainG.setColor(myC);
				else
					mainG.setColor(Color.white);
				mainG.setFont(new Font("TimesRoman", Font.PLAIN, 12));
			}
			mainG.drawString(datS, colIndex*20 + 12, 135 + k*15);
			colIndex++;
			if(colIndex > 6){
				colIndex = 0;
				k++;
			}
		}
		coun++;
		if(coun >= 100) coun = 0;

		if(mouseX >= 0)
			mainG.drawImage(linkImg, mouseX, mouseY-16, this);
   }
   private void getDat(){
// get date everywhen re-draw
		d = new Date();
		mon = d.getMonth();
		dat = d.getDate();
		yy = d.getYear();
		thu1 = d.getDay() + 1;
         	mm = d.getMinutes();
          	hh = d.getHours();
		if(hh < 10) hhS = "0" + hh;
		else hhS = "" + hh;
		if(mm < 10) mmS = "0" + mm;
		else mmS = "" + mm;
   }
//____________________________________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...", 5, 20);
	}
   }

   public void run(){
       	while(Thread.currentThread() == th){  
           	try {
  	             createMainImg();
                     repaint();
                     Thread.sleep(500);
                     System.gc();
            	} 
            	catch(InterruptedException E) {
                     E.printStackTrace();
            	}
       	}
   }
//____________________________________Applet Information_____________________________________//
   public String getAppletInfo() {
		return "Name: h_calendar\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" },
			{ "bgcolor", "An Integer value (HEX)", "Background color" },
		};
		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 = 7*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;
	}
//_____________________________________Message______________________________________________//
	public void createCalBg(){
	 	LoadImg();
		Graphics g;
		bgImg = createImage(w, h);
		g = bgImg.getGraphics();
		g.setColor(bgC);
		g.fillRect(0, 0, w, h);
		g.setColor(Color.black);	// button effect
		g.drawLine(0, 0, w-1, 0);
		g.drawLine(0, 0, 0, h);
		g.setColor(Color.white);
		g.drawLine(w-1, 0, w-1, h);
		g.drawLine(0, h-1, w, h-1);
		g.drawImage(img, 5, 5, 144, 92, this);
		g.setColor(myC1);
		g.drawLine(6, 212, 147, 212);
		g.drawRect(118, 98, 30, 12);
		g.setFont(new Font("TimesRoman", Font.ITALIC, 11));
		g.drawString("Printed in:", 12, 222);
		g.setFont(new Font("TimesRoman", Font.PLAIN, 11));
		g.drawString(" " + "Seiun Dom.N116", 60, 222);

           	mt.addImage(bgImg, 1);
       		try {
           		mt.waitForID(1);
       		}
       		catch(InterruptedException e){};
	}
}
