//
/* ----------------------------------------------------------------------------------------------
    watch.java		WATCH								ver. 2.00 (JDK 1.02)
    														* タイマー機能追加    2002.02.19
    														ueyama@infonet.co.jp  1999.11.13
---------------------------------------------------------------------------------------------- */

import java.applet.*;
import java.awt.*;
import java.util.*;
import java.lang.Math;


public class watch extends Applet implements Runnable
{
	int			X=90, Y=90;													// 文字盤の中心座標
	double		Sl=(double)X*0.83, Ml=(double)X*0.75, Hl=(double)X*0.5;		// 秒・分・時針の長さ
	boolean		Tw=false;											// true: Timer,  false: Watch
	boolean		Ts=false;											// true: タイマー設定中
	boolean		Tm=false;											// true: 作動時間設定中
	int			Tc=0, Tb=0;											// タイマー設定時間(秒)
	int			Sn;													// 現在時刻
	AudioClip	Pi, Pipipi;											// ブザー音
	int			H,M,S, dH=0,dM=0,dS=0, Sb=0;
	String		Tp="", Th="", Ti="";
	Image		Buf=null;
	Image		Img;
	Thread		th=null;
	int			bgc;
	Date		Td=new Date();
	
	//			講義時間 1時限  2時限  3時限  4時限  5時限  6時限  7時限
	int			Cs[][]={{ 9,10},{10,55},{12,40},{14,25},{16,10},{17,50},{19,25}};	// 開始 時,分
	int			Ce[][]={{10,40},{12,25},{14,10},{15,55},{17,40},{19,20},{20,55}};	// 終了 時,分
	int			Cq=7;																// 講義時間数
	
	public void init()
	{
		String	Bg=getParameter("BgColor");
		bgc=Integer.valueOf(Bg,16).intValue();
		setBackground(new Color(bgc)); 
		Buf=createImage(size().width,size().height);
		MediaTracker mt=new MediaTracker(this);
		Img=getImage(getCodeBase(), getParameter("figure"));
		mt.addImage(Img, 0);
		try
		{
			mt.waitForID(0);
		}
		catch(InterruptedException e){};
		Pi		=getAudioClip(getCodeBase(), "pi.au");				// 効果音データの読み込み
		Pipipi=getAudioClip(getCodeBase(), "pipipipi.au");
	}
	
	public void start()
	{
		if(th==null)
		{
			th=new Thread(this);
			th.start();
		}
	}
	
	public boolean inside(int x, int a, int b)
	{
		return (x<=Math.max(a,b) && x>=Math.min(a,b)) ? true:false;
	}
	
	public int t2s(int h, int m, int s)								// 時刻を秒数に変換する
	{
		return h*3600+m*60+s;
	}
	
	public String s2t(int s)										// 秒数を時刻文字列に変換する
	{
		String hr,min,sec;
		if(s%60==0) sec="00"; else if(s%60<10) sec="0"+s%60; else sec=""+s%60;
		s/=60;
		if(s%60==0) min="00"; else if(s%60<10) min="0"+s%60; else min=""+s%60;
		s/=60;
		if(s==0) hr="00"; else if(s<10) hr="0"+s; else hr=""+s;
		return hr+":"+min+":"+sec;
	}
	
	public void dsp_s(int r, Graphics g)							// 秒針の表示
	{
		int x0,y0,x1,y1;
		x0=(int)(X+Sl*Math.sin((double)r*3.14159/180.0));
		y0=(int)(Y-Sl*Math.cos((double)r*3.14159/180.0));
		x1=(int)(X-Sl*Math.sin((double)r*3.14159/180.0)*0.25);
		y1=(int)(Y+Sl*Math.cos((double)r*3.14159/180.0)*0.25);
		g.setColor(new Color(153,0,0));
		g.drawLine(x0, y0, x1, y1);
	}
	
	public void dsp_m(int r, int w, int l, Graphics g)				// 分・時針の表示
	{
		int i;
		double x0,y0,x1,y1;
		g.setColor(new Color(0,0,0));
		for(i=0; i < l ;i++)
		{
			x0=X+i*Math.sin((double)r*3.14159/180.0)-w/2;
			y0=Y-i*Math.cos((double)r*3.14159/180.0)-w/2;
			g.fillOval((int)x0, (int)y0, w, w);
		}
	}
	
	public void dsp_w(Graphics g)									// 時計文字盤の表示
	{
		int		r, x0=0,y0=0,x1=0,y1=0;
		String	s=Td.toString();
		
		showStatus(s);
		g.setColor(new Color(51,51,0));
		g.fillOval(0,0,X*2,Y*2);
		g.setColor(new Color(220,220,170));
		g.fillOval(3,3,X*2-6,Y*2-6);
		g.setColor(new Color(255,255,255));
		g.setFont(new Font("TimesRoman", Font.PLAIN, 16));
		r=s.length();
		g.drawString(s.substring(r-4),75,60);						// 年の表示
		g.drawString(s.substring(0,10),58,130);						// 日付の表示
		g.setColor(new Color(51,51,0));
		for(r=6;r<360;r+=6)											// 目盛りの表示
		{
			x0=(int)(X+(X-6)*Math.cos((double)r*3.14159/180.0));
			y0=(int)(Y+(Y-6)*Math.sin((double)r*3.14159/180.0));
			x1=(int)(X+(X-12)*Math.cos((double)r*3.14159/180.0));
			y1=(int)(Y+(Y-12)*Math.sin((double)r*3.14159/180.0));
			g.drawLine(x0, y0, (r%30==0)?x1:x0, (r%30==0)?y1:y0);
		}
		g.fillRect(X-1,5,3,9);
		g.fillRect(5,Y-1,9,3);
		g.fillRect(X-1,Y*2-14,3,9);
		g.fillRect(X*2-14,Y-1,9,3);
	}
	
	public void dsp_p(int x, int y, int n, int c, Graphics g)		// 数字・記号の表示
	{
		int			w=0, h=0, ox=0, oy=0;
		Graphics	gx=g.create();
		switch(c)
		{
			case 0: w=17; h=27; ox=  0; oy= 0; break;				// 時刻表示用数字
			case 1: w=14; h=21; ox=  0; oy=27; break;				// 経過時間表示用数字
			case 2: w=14; h=21; ox=  0; oy=48; break;				// 残り時間表示用数字
			case 3: w= 7; h=13; ox=  0; oy=69; break;				// 時間範囲表示用数字
			case 4: w=23; h=11; ox=105; oy=69;						//「設定」/「表示」
					g.setColor(new Color(255,255,255));
					g.fillRect(x+26, y-4, 41, 19);
					break;
			case 5: w=14; h=15; ox=  0; oy=82; break;				// タイマー設定ボタン用数字
			case 6: w=24; h=24; ox=154; oy=46; break;				// タイマー設定ボタン
			case 7: w=19; h=19; ox=154; oy=27;						// [T] / [W]
		}
		gx.clipRect(x,y,w,h);
		gx.drawImage(Img,x-(ox+n*w),y-oy,this);
		gx.dispose();
	}
	
	public void dsp_t(String t, int c, Graphics g)					// 時刻・時間の表示
	{
		int	i,j=0,n=0,p=0,x=0,y=0;
		String	s;
		switch(c)
		{
			case	0: x=19; y=192; p=20;	break;					// 現在時刻
			case	1: x=(Tw && Tb<=3600)?54:32; y=250; p=16;	break;	// 経過時間
			case	2: x=(Tw && Tb<=3600)?54:32; y=280; p=16;	break;	// 残り時間
			case	3: x=12; y=230; p= 8;	break;					// 時間割
			case	4: x= 78-t.length()*8; if(t.length()>2) x+=3; y=285; p= 8; c=3; break;	// 時間割
			case	5: x=156-t.length()*8; if(t.length()>2) x+=3; y=285; p= 8; c=3; break;	// 時間割
		}
		for(i=0;i600 && (Tc==300 || Tc==302)) Pipipi.play();	// 5分前予鈴
					if(Tc==0) Pipipi.play();
					if(Tc>0)  Tc--;
				}
			}
 			try
			{
				th.sleep(128);
			}
			catch (InterruptedException e){}
		}
	}
	
	public void stop()
	{
		if(th!=null)
		{
			th.stop();
			th=null;
		}
	}
	
	public boolean mouseDown(Event e, int mx, int my)
	{
		int	n, h=0,m=0,s=0;
		String ss;
		if(Tc==0)
		{
			if(inside(mx, 161, 180) && inside(my, 282, 301))		// T or W (時計/タイマー切替)
			{
				if(Tw)												// 
				{
					Tw=false;
					dH=0; dM=0; dS=0;
				}
				else
				{
					Ts=true;
					Tp=""; Th="";
					Pi.play();										// 効果音「ピッ」
				}
			}
			if(inside(mx, 13, 167) && inside(my, 227, 277))			// タイマー設定ボタン
			{
				if(mx<142)											// 数字ボタン
				{
					n=(mx-13)/26;
					if(my<252) n+=5;
					if(Ts && Tp.length()<5)
					{
						Tp+=String.valueOf(n);						// タイマー
						if(Tp.length()==2) Tp+=":";
					}
					if(Tm && Th.length()<5)
					{
						Th+=String.valueOf(n);						// 作動表示時間
						if(Th.length()==2) Th+=":";
					}
					Sb--;											// 即時画面表示のため  (see line 267)
				}
				else if(my>253)										// [S] (Set or Start)
				{
					if(Ts && Tp.length()==5)						// タイマー設定完了
					{
						Ts=false; Tm=true;
					}
					else if(Tm)
					{
						if(Th.length()%5==0)
						{
							Tm=false;
							m=Integer.parseInt(Tp.substring(0,2));
							s=Integer.parseInt(Tp.substring(3));
							h=m/60; m%=60;
							Tc=Tb=t2s(h,m,s);
							Tw=true;
						}
						if(Th.length()==0)
						{
							Ts=false;
							ss=s2t(t2s(H+h,M+m,S+s));				// 終了時間
							Th=ss.substring(0,5);
						}
						if(Th.length()==5)							// 
						{
							h=Integer.parseInt(Th.substring(0,2));
							m=Integer.parseInt(Th.substring(3));
							n=t2s(h,m,0)-Sn-Tb;
							Ti=s2t(Sn+n);
							dS=n%60; n/=60; dM=n%60; dH=n/60;
						}
					}
				}
				else												// [C] (Clear)
				{
					if(Ts) Tp="";
					if(Tm) Th="";
				}
				Pi.play();											// 効果音「ピッ」
			}
		}
		else if(my>308)
		{
			Tp=""; Th=""; Tc=0; Tw=false; dH=0; dM=0; dS=0;
			Pi.play();
		}
		return true;
	}
}
//
戻る