//
/* ----------------------------------------------------------------------------------------------
    packet.java		パケット								ver. 1.01 (JDK 1.02)
    														ueyama@infonet.co.jp  1999.08.01
---------------------------------------------------------------------------------------------- */

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


public class packet extends Applet implements Runnable
{
	int			Lp=49;
	int			Dq=50;
	int			Tno;
	int			Sn[]=new int[6];									// 送信パケット数
	int			Rn[]=new int[6];									// 受信パケット数
	int			Pq[]=new int[6];									// パケット数
	int			Dx[]=new int[50];									// X座標値
	int			Dy[]=new int[50];									// Y座標値
	int			Act[]={9,9,9,9,9,9,9};								// クライアント No.
	boolean		Pkt=false;											// true: パケットモード
	boolean		Run=false;
	int			Tn=6;
	int			R,G,B;
	int			Wait=64;
	Thread		th=null;
	Graphics	gr;
	Image		Gif;
	Random		Rnd=new Random();
	
	public void start()
	{
		if(th==null)
		{
			th=new Thread(this);
			th.start();
		}
	}
	
	public void init()
	{
		R=Integer.parseInt(getParameter("bg_red"));
		G=Integer.parseInt(getParameter("bg_green"));
		B=Integer.parseInt(getParameter("bg_blue"));
		setBackground(new Color(R,G,B));
		gr=getGraphics();
		
		MediaTracker mt=new MediaTracker(this);
		Gif=getImage(getCodeBase(), getParameter("figure"));
		mt.addImage(Gif, 0);
		try
		{
			mt.waitForID(0);
		}
		catch(InterruptedException e){};
	}
	
	public boolean inside(int x, int a, int b)
	{
		return (x<=Math.max(a,b) && x>=Math.min(a,b)) ? true:false;
	}
	
	public int selno()
	{
		int i;
		for(i=0;i<6;i++) if(Act[i]>=0 && Act[i]<6) break;
		return i;
	}
	
	public void dsp_g(int x, int y, int n, int c)					// 画像の表示
	{
		int			w=0,h=0, ox=0,oy=0;
		Graphics	gx=gr.create();
		switch(n)
		{
			case  0: w= 11; h= 11; ox=281; oy= 0; break;			// 宛先 ヘッダ
			case  1: w= 11; h= 11; ox=347; oy= 0; break;			// 送り主 ヘッダ
			case  2: w= 11; h= 11; ox=367; oy=11; break;			// データ
			case  3: w= 11; h= 11; ox= 61; oy= 0; break;			// packet No.
			case  4: w= 61; h= 60; ox=  0; oy= 0; break;			// クライアント
			case  5: w= 17; h= 21; ox= 61; oy=11; break;			// クライアント名 白
			case  6: w= 17; h= 21; ox=163; oy=11; break;			// クライアント名 青
			case  7: w= 17; h= 21; ox=265; oy=11; break;			// クライアント名 赤
			case  8: w= 73; h= 27; ox= 61; oy=32; break;			// "PACKET"
			case  9: w= 26; h= 23; ox=207; oy=34; break;			// "<<" ボタン
			case 10: w= 26; h= 23; ox=259; oy=34; break;			// ">>" ボタン
			case 11: w= 26; h= 23; ox=337; oy=34; break;			// "||" ボタン
			case 12: w= 26; h= 23; ox=389; oy=34; break;			// "|>" ボタン
		}
		if(n==8)
		{
			gx.setColor(new Color(R,G,B));
			gx.fillRect(x,y,w,h);
		}
		if(n<4 && y>33) h=44-y;
		gx.clipRect(x,y,w,h);
		gx.drawImage(Gif,x-(ox+w*c),y-oy,this);
		gx.dispose();
	}
	
	public void dsp_c(int c)										// 通信回線の表示
	{
		int	i;
		if(c==0)	gr.setColor(new Color(102,102,102));
		else		gr.setColor(new Color(128,102,102));
		gr.fillRect(0,0,501,11);
		for(i=0;i<6;i++) gr.fillRect(i*88+25, 11, 11, 33);
	}
	
	public void dsp_pkt(int n, int q, int c)						// 送・受信パケット数の表示
	{
		int i;
		gr.setColor(new Color(R,G,B));
		gr.fillRect(n*88,110,61,19);
		if(c==0)	gr.setColor(new Color(51,204,204));
		else		gr.setColor(new Color(255,102,51));
		for(i=0; i" ボタンの表示
		dsp_g(262, 134, 10, (Wait>16)? 0:2);						// ">>" ボタンの表示
		if(selno()<6) dsp_p();
	}
	
	public boolean dsp_p()
	{
		int i,k=0,x=0,y=0, dir=9;
		
		k=(Tno44 && Dx[i]==88*Act[Tno]+25 && i>1 && !Pkt)	// 非パケット時 無限ループにする
			{
				Dx[i]=88*Tno+25;
				Dy[i]=Dy[Lp]+11;
				Lp=i;
			}
		}
		if(Pkt)
		{
			if(x==88*Act[Tno]+25) dir=5;
			else if(y==0) dir=6;
			else dir=4;
			if(Tno44)? true:false;
	}
	
	public void run()
	{
		int	i,j,k;
		while(th!=null)
		{
			if(Run)
			{
				if(dsp_p())
				{
					Sn[Tno]++; Rn[Act[Tno]]++;
					dsp_pkt(Tno, Pq[Tno]-Sn[Tno], 0);
					dsp_pkt(Act[Tno], Rn[Act[Tno]], 1);
					if(Sn[Tno]>=Pq[Tno])
					{
						dsp_g(88*Tno+22, 53, 5, Tno);				// 端末名表示色を白に変更
						dsp_g(88*Act[Tno]+22, 53, 5, Act[Tno]);		// 端末名表示色を白に変更
						Act[Tno]=9; Sn[Tno]=0;
						Pq[Tno]=(Pkt)? Math.abs(Rnd.nextInt())%16+5:0;	// パケット数
						dsp_pkt(Tno, Pq[Tno], 0);
						if(selno()==6)
						{
							Run=false;
							dsp_c(0);
						}
					}
					while(true)
					{
						Tno++;
						if(Tno==6) Tno=0;
						if(inside(Act[Tno],0,5)) break;
					}
					for(j=0;j" ボタン
			{
				Run=!Run;
				dsp_g(232, 134, (Run)? 12:11, 1);
			}
			if(inside(mx,262,288) && Wait>16)						// ">>" ボタン
			{
				Wait/=2;
				dsp_g(262, 134, 10, 1);
			}
		}
		return true;
	}
	
	public boolean mouseUp(Event e, int mx, int my)
	{
		if(inside(my,134,157))
		{
			if(inside(mx,202,228)) dsp_g(202, 134, 9, 0);				// "<<" ボタン
			if(inside(mx,232,258)) dsp_g(232, 134, (Run)? 11:12, 0);	// "||" or "|>" ボタン
			if(inside(mx,262,288)) dsp_g(262, 134, 10, (Wait>16)? 0:2);	// ">>" ボタン
		}
		return true;
	}
	
	public void stop()
	{
		if(th!=null)
		{
			th.stop();
			th=null;
		}
	}
}
//
戻る