//
/* ----------------------------------------------------------------------------------------------
    diode.java		ダイオード								ver. 1.00 (JDK 1.02)
    														ueyama@infonet.co.jp  1999.03.06
---------------------------------------------------------------------------------------------- */

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

public class diode extends Applet implements Runnable
{
	int			Dir=0;										// 0: 逆方向 1: 順方向
	int			Sw=0;										// 0: OFF     1: ON
	int			Wait=256;									// 表示変更待ち時間 (msec)
	int			Epx[]=new int[40], Epy[]=new int[40];		// 電子位置座標
	int			Hpx[]=new int[40], Hpy[]=new int[40];		// 正孔位置座標
	int			Qty0=20;									// キャリア初期数
	int			Qmax=40;									// キャリア最大数
	int			Qtye=20,Qtyh=20;							// 電子、正孔数
	boolean		Run=true;
	boolean		Er[]=new boolean[40], Hr[]=new boolean[40];
	Image		Gif;
	Image		buf=null;
	Thread		th=null;
	int			R,G,B;
	Random		Rnd=new Random();
	
	public void init()
	{
		int i,j;
		boolean c;
		R=Integer.parseInt(getParameter("bg_red"));
		G=Integer.parseInt(getParameter("bg_green"));
		B=Integer.parseInt(getParameter("bg_blue"));
		setBackground(new Color(R,G,B));
		
		MediaTracker mt=new MediaTracker(this);
		Gif=getImage(getCodeBase(),"diode.gif");
		mt.addImage(Gif, 0);
		try
		{
			mt.waitForID(0);
		}
		catch(InterruptedException e){};
		buf=createImage(size().width,size().height);
		carrier_initialize();
	}
	
	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 void carrier_initialize()
	{
		int i,j,k,x,y;
		
		for(i=0;i94) return 2;
		else return 1;
	}
	
	public int move_y()									// 10%は上に、10%は下に、80%は動かない
	{
		int x=Math.abs(Rnd.nextInt())%100;
		if(x<10) return 1;
		else if(x>89) return -1;
		else return 0;
	}
	
	public void carrier_move()
	{
		int i,j,k,q,x,y=0;
		
		for(i=0;i178 && Hr[i]==false && i=315) Hpx[i]=48;
		}
	}
	
	public void dsp_m(int n, int f, Graphics g)							// 回路記号等の表示
	{
		int			x=0,y=0, w=0,h=0, ox=0,oy=0;
		Graphics	gx=g.create();
		switch(n)
		{
			case  0: x= 78; y=189; w=93; h=47; ox=470; oy=  0; break;	// 電池
			case  1: x=224; y=188; w=55; h=32; ox=540; oy=124; break;	// スイッチ
			case  2: x=400; y= 98; w=69; h=69; ox=470; oy= 94; break;	// 電球
			case  3: x= 13; y=  9; w=31; h=31; ox=564; oy=  0; break;	// P 極性
			case  4: x=329; y=  9; w=31; h=31; ox=564; oy=  0; break;	// N 極性
			case  5: x=385; y=213; w=26; h=23; ox=595; oy=  0; break;	// 低速ボタン
			case  6: x=443; y=213; w=26; h=23; ox=595; oy= 69; break;	// 高速ボタン
			case  7: x=414; y=213; w=26; h=23; ox=595; oy=138; break;	// 停止ボタン
			case  8: x=414; y=213; w=26; h=23; ox=595; oy=184; break;	// 移動ボタン
			case  9: x= 12; y=109; w=17; h=44; ox=540; oy=188; break;	// 電流矢印(上向)
			case 10: x=343; y=111; w=17; h=44; ox=557; oy=188; break;	// 電流矢印(上向)
		}
		gx.clipRect(x,y,w,h);
		gx.drawImage(Gif,x-ox,y-(oy+f*h),this);
		gx.dispose();
	}
	
	public void paint(Graphics g)
	{
		int i;
		
		g.setColor(new Color(R,G,B));
		g.fillRect(0,0,size().width,size().height);
		g.drawImage(Gif,0,0,this);
		dsp_m(0,Dir,g);												// 電池や電球を描く
		dsp_m(1,Sw,g);
		dsp_m(2,(Dir*Sw==1 && Run)?1:0,g);
		dsp_m(3,Sw*2+(1-Dir),g);
		dsp_m(4,Sw*2+Dir,g);
		dsp_m(5,0,g);
		dsp_m(6,(Wait==64)?2:0,g);
		dsp_m((Run)?7:8,0,g);
		if(Sw==1 && Dir==1 && Run) dsp_m(9,0,g);
		if(Sw==1 && Dir==1 && Run) dsp_m(10,0,g);
		g.clipRect(57,3,259,98);
		g.setColor(new Color(51,102,255));							// 電子を描く
		for(i=0;i189) Dir=(Dir==0)?1:0;				// 電池をクリック
		if(inside(x,224,279) && y>188)								// スイッチをクリック
		{
			Sw=(Sw==0)?1:0;
			Qtye=Qty0;Qtyh=Qty0;
			if(Sw==0) carrier_initialize();
			else
			{
				for(i=0;i213)								// 低速ボタンをクリック
		{
			Graphics gb=getGraphics();
			dsp_m(5,1,gb);
			Wait*=2;
		}
		if(inside(x,443,469) && y>213 && Wait>64)					// 高速ボタンをクリック
		{
			Graphics gb=getGraphics();
			dsp_m(6,1,gb);
			Wait/=2;
		}
		if(inside(x,414,440) && y>213)								// 停止・再開ボタンをクリック
		{
			Graphics gb=getGraphics();
			dsp_m((Run)?7:8,1,gb);
			Run=!Run;
		}
		return true;
	}

	public void stop()
	{
		if(th!=null)
		{
			th.stop();
			th=null;
		}
	}
}
//
戻る