//
/* ----------------------------------------------------------------------------------------------
    interlace.java		インターレース						ver. 1.00 (JDK 1.02)
    														ueyama@infonet.co.jp  2000.01.05
---------------------------------------------------------------------------------------------- */

import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;


public class interlace extends Applet implements Runnable
{
//	int			P=15,Pn=0;											// フレーム No.
	int			P=0,Pn=0;											// フレーム No.
	int			X=0,Y=0,Iy=0,Fx=0;									// ピクセル座標等
	boolean		Run=false;											// f: 停止
	boolean		Int=true;											// f: プログレッシブ
	boolean		Ply=false;											// t: 高速表示
	int			Wait=16;
	boolean		Rb;
	Image		Img, Btn;
	Graphics	gr;
	Thread		th=null;
	
	public void init()
	{
		String	bg=getParameter("BgColor");
		int		bc=Integer.valueOf(bg,16).intValue();
		setBackground(new Color(bc));
		
		MediaTracker mt=new MediaTracker(this);
		Img=getImage(getCodeBase(), getParameter("figure"));
		mt.addImage(Img, 0);
		Btn=getImage(getCodeBase(), getParameter("button"));
		mt.addImage(Btn, 0);
		try
		{
			mt.waitForID(0);
		}
		catch (InterruptedException ie) {}
		gr=getGraphics();
	}
	
	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 dsp_bt(int c, int n)								// ボタン、数字等の表示
	{
		int			x=0,y=0, w=0,h=0, ox=0,oy=0;
		Graphics	gx=gr.create();
		switch(c)
		{
			case  0: x= 60; y=273; w=26; h=23; ox= 78; oy= 23; break;	// << ボタン
			case  1: x= 90; y=273; w=26; h=23; ox=234; oy= 23; break;	// |<| ボタン
			case  2: x=120; y=273; w=26; h=23; ox=312; oy= 23; break;	// ■ ボタン
			case  3: x=150; y=273; w=26; h=23; ox=156; oy=  0; break;	// I ボタン
			case  4: x=150; y=273; w=26; h=23; ox=156; oy= 23; break;	// P ボタン
			case  5: x=180; y=273; w=26; h=23; ox=390; oy=  0; break;	// |>|> ボタン
			case  6: x=210; y=273; w=26; h=23; ox= 78; oy=  0; break;	// >> ボタン
			case  7: x=120; y=273; w=26; h=23; ox=  0; oy=  0; break;	// |> ボタン
			case  8: x=  0; y=240; w=17; h=24; ox=  0; oy= 46; break;	// 数字
			case  9: x= 52; y=247; w=43; h=10; ox=187; oy= 46; break;	// "Frame"
			case 10: x=167; y=247; w=27; h=10; ox=187; oy= 56; break;	// "Line"
		}
		if(x==0)
		{
			x=Fx;
			gx.clearRect(x,y,w,h);
		}
		gx.clipRect(x,y,w,h);
		gx.drawImage(Btn, x-(ox+n*w), y-oy, this);
		gx.dispose();
	}
	
	public void dsp_px(int p, int x, int y, boolean d)				// 画像を1ピクセル表示する
	{
		Graphics	gx=gr.create();
		gx.clipRect(x*4,y*4,4,4);
		gx.drawImage(Img, (d)?-296:0, -p*224, 592, 3584, this);
//		gx.drawImage(Img, (d)?-296:0, -p*224, this);				// for i_lace4.gif
		gx.dispose();
	}
	
	public void dsp_f(int p, boolean d)								// 画像を1フレーム表示する
	{
		int x,y;
		for(y=0;y<56;y++)
		{
			if((Int && Iy==0 && y%2==0) || (Int && Iy==1 && Y%2==1) || !Int)
			{
				for(x=0;x<74;x++) dsp_px(p,x,y,d);
			}
		}
	}
	
	public void dsp_n()												// フレーム、走査線 No. の表示
	{
		dsp_bt(9,0); dsp_bt(10,0);
		Fx=102; dsp_bt(8,(Run && P>=9)?1:10);
		Fx=121; dsp_bt(8,(Run)?(P+1)%10:10);
		if(Run)
		{
			Fx=202; dsp_bt(8,((Y+1)/10>0)?(Y+1)/10:10);
			Fx=222; dsp_bt(8,(Y+1)%10);
		}
		else gr.clearRect(198,240,51,24);
	}
	
	public void paint(Graphics g)
	{
		int x,y;
		
		g.clearRect(0,276,400,340);
		for(y=0;y<56;y++) for(x=0;x<74;x++) dsp_px(P,x,y,(Pn>0)?true:false);
		dsp_n();
		dsp_bt(0, 0);												// << ボタンの表示
		dsp_bt(1, 0);												// |<| ボタンの表示
		dsp_bt((Run)?2:7, 0);										// ■ ボタンの表示
		dsp_bt((Int)?4:3, 0);										// P ボタンの表示
		dsp_bt(5, (Ply)?1:0);										// |>|> ボタンの表示
		dsp_bt(6, (Wait==4)?2:0);									// >> ボタンの表示
		if(Pn==0) P=0;
	}
	
	public void th_sleep(int t)
	{
		try
		{
			Thread.sleep(t);
		}
		catch (InterruptedException e){}
	}
	
	public void run()
	{
		int w;
		
		while(true)
		{
			if(Run)
			{
				dsp_px(P,X,Y,false);
				X++; X%=74;
				if(X==0)
				{
					Y+=(Int)?2:1;
					if(Y>=56)										// 1フレーム走査完了
					{
						th_sleep((Ply)?100:1000);
						if(!Ply) dsp_f(P,true);
						P+=(Int)?Iy:1; P%=16; Pn++;
						if(Int)
						{
							Iy++; Iy%=2;
							Y=(Iy==0)?0:1;
						}
						else Y=0;
						Fx=102; dsp_bt(8,(Run && P>=9)?1:10);		// フレーム No. の表示
						Fx=121; dsp_bt(8,(Run)?(P+1)%10:10);
					}
					Fx=202; dsp_bt(8, ((Y+1)/10>0)?(Y+1)/10:17);
					Fx=222; dsp_bt(8, (Y+1)%10);
				}
				if(!Ply) th_sleep((Run)?Wait:256);
			}
			else th_sleep(500);
		}
	}
	
	public boolean mouseDown(Event e, int x, int y)
	{
		int i,j;
		
		if(y>273)
		{
			if(inside(x, 60, 86))
			{
				dsp_bt(0,1); dsp_bt(6,0); Wait*=2;
			}
			if(inside(x, 90, 116))
			{
				dsp_bt(1, 1); P=0; X=0; Y=0;
				for(i=0;i<56;i++) for(j=0;j<74;j++) dsp_px(15,j,i,true);
			}
			if(inside(x, 120, 146))
			{
				dsp_bt((Run)?2:7, 1); Run=!Run;
				if(Pn==0) for(i=0;i<56;i++) for(j=0;j<74;j++) dsp_px(15,j,i,true);
				if(Run) dsp_n();
			}
			if(inside(x, 150, 176))
			{
				dsp_bt((Int)?4:3,1); Int=!Int;
			}
			if(inside(x, 180, 206))
			{
				Ply=!Ply; dsp_bt(5, (Ply)?1:0);
				if(Ply) {Rb=Run; Run=true;}
				else	Run=Rb;
			}
			if(inside(x, 210, 236) && Wait>4)
			{
				dsp_bt(6,1); Wait/=2;
			}
		}
		return true;
	}
	
	public boolean mouseUp(Event e, int x, int y)
	{
		if(y>236)
		{
			if(inside(x,  60,  86))	dsp_bt(0, 0);
			if(inside(x,  90, 106))	dsp_bt(1, 0);
			if(inside(x, 120, 146))	dsp_bt((Run)?2:7, 0);
			if(inside(x, 150, 176))	dsp_bt((Int)?4:3, 0);
			if(inside(x, 210, 236))	dsp_bt(6, (Wait==4)?2:0);
		}
		return true;
	}
	
	public void stop()
	{
		if(th!=null)
		{
			th.stop();
			th=null;
		}
	}
}
//
戻る