//
/* ----------------------------------------------------------------------------------------------
    scan.java		ディスプレイ(走査)					ver. 1.00 (JDK 1.02)
    														ueyama@infonet.co.jp  1998.09.09
---------------------------------------------------------------------------------------------- */

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


public class scan extends Applet implements Runnable
{
	int			Bd=9;											// ビーム直径
	int			Bx=0,By=0;										// ビーム座標
	int			Wait=32;										// 待ち時間 (msec)
	int			i;
	int			pixels[]=new int[320*240];
	boolean		Frun=false;
	Button		bt_fine,bt_rough,bt_fast,bt_slow,bt_run;
	Color		Col[][]=new Color[2][4];
	Image		scan_a;
	Graphics	gl,gs;
	Thread		th=null;
	
	public void start()
	{
		if(th==null)
		{
			th=new Thread(this);
			th.start();
		}
	}
	
	public void init()
	{
		int r,g,b;
		
		r=Integer.parseInt(getParameter("bg_red"));
		g=Integer.parseInt(getParameter("bg_green"));
		b=Integer.parseInt(getParameter("bg_blue"));
		setBackground(new Color(r,g,b));
		gl=getGraphics();
		gs=getGraphics();
		MediaTracker mt=new MediaTracker(this);
		scan_a=getImage(getCodeBase(),"scan_a.gif");			// 画像ファイルの読込
		mt.addImage(scan_a, 0);
		try
		{
			mt.waitForID(0);
		}
		catch(InterruptedException e){};
		
		PixelGrabber pg=new PixelGrabber(scan_a,0,0,320,240,pixels,0,320);	// 画像データの取得
		try
		{
			pg.grabPixels();
		}
		catch (InterruptedException ie)	{return;}
		if((pg.status() & ImageObserver.ABORT) != 0) {return;}
		
		for(i=0,r=255,g=255,b=255; i<4; i++,r/=2,g/=2,b/=2)	Col[1][i]=new Color(r,g,b);
		for(i=0,r=0,g=96,b=224; i<4; i++,g=g*2/3,b=b*2/3)	Col[0][i]=new Color(r,g,b);
		
		Panel p=new Panel();
		p.add(bt_fine=	new	Button("FINE"));
		p.add(bt_rough=	new	Button("ROUGH"));
		p.add(bt_slow=	new	Button("SLOW"));
		p.add(bt_fast=	new	Button("FAST"));
		p.add(bt_run=	new	Button("RUN"));
		setLayout(new BorderLayout());
		add("South",p);
	}
	
	public void paint(Graphics g)
	{
		g.setColor(Color.black);
		if(Frun)	g.fillRect(0,0,320,240);					// 背景:真っ黒
		else		g.drawImage(scan_a,0,0,320,240,this);		// 背景:GIF画像 (A)
		disp_line();
	}
	
	public void disp_spot()										// ビームスポットの描画
	{
		gs.clipRect(0,0,320,240);
		for(int i=3;i>=0;i--)									// 表示色を変えて残像の雰囲気 (?)
		{
			gs.setColor(Col[pixels[By*320+Bx] & 1][i]);
			gs.fillOval(Bx-3-i*Bd/2,By,Bd,Bd);
		}
	}
	
	public void disp_line()										// 走査線の表示
	{
		gl.setColor(Color.black);
		gl.fillRect(0,242,320,21);
		if(Frun)
		{
			for(int i=0;i<320;i+=Bd*5/10)						// 画像データの表示
			{
				gl.setColor(Col[pixels[By*320+i] & 1][0]);
				gl.fillRect(i-3,252-Bd/2,Bd,Bd);
			}
		}
	}
	
	public void run()
	{
		while(th!=null)
		{
			if(Frun)
			{
				if(Bx==0) disp_line();
				disp_spot();
			}
			try
			{
				Thread.sleep(Wait);
			}
			catch (InterruptedException e){}
			if(Frun)
			{
				gs.setColor(Color.black);
				gs.fillRect(Bx-3-i*Bd/2,By,Bd*3,Bd);			// スポットを消去する
				Bx+=Bd*5/10;
				if(Bx>=320)
				{
					Bx=0; By+=Bd;
				}
				if(By>=240)
				{
					Frun=false; Bx=0; By=0;
					repaint();
					th.suspend();
				}
			}
		}
	}
	
	public boolean action(Event e, Object o)
	{
		if(e.target==bt_fine && Bd>3)			Bd-=2;
		else if(e.target==bt_rough && Bd<21)	Bd+=2;
		else if(e.target==bt_fast && Wait>4)	Wait=Wait*3/4;
		else if(e.target==bt_slow)				Wait=Wait*4/3;
		else if(e.target==bt_run)
		{
			Frun=true;
			repaint();
			th.resume();
		}
		if(Wait==4) bt_fast.disable();							// ボタンの制御
		else		bt_fast.enable();
		if(Bd==3)	bt_fine.disable();
		else		bt_fine.enable();
		if(Bd==21)	bt_rough.disable();
		else		bt_rough.enable();
		return true;
	}
	
	public void stop()
	{
		if(th==null)
		{
			th.stop();
		}
	}
}
//
戻る