/*
=======================================================================
                   Kiss Applet version 0.6 (TestVersion)

                    E-mail     HGB03437@niftyserve.or.jp  BakuretuKen
=======================================================================
*/

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

// =====================================================================

class Cell {
	Image image;
	int no;
	int w;
	int h;
	int move;
	String lbl;
}

// =====================================================================

public class KissApplet extends Applet implements Runnable {

	int xx[], yy[], move[];
	MediaTracker mt;
	Thread thread;
	String imagedir;
	String imageprefix;
	String kstitle;
	int last, num = -1, oldx, oldy;
	int masx, masy;
	int sx, sy;
	Graphics bg;
	Cell cells[], celltmp;
	Image backimage;
	int r, g, b;
	Color bc;
	int nolast;

// --------------------------------------------------------------
	public void init() {
		int i;
		String s, param;
		String imgname = null;
		s = getParameter("imagedir");
		if (s != null) imagedir = s;
		s = getParameter("title");
		if (s != null) kstitle = s;
		
		for ( i = 0; i < 100; i++) {
			s = getParameter("cel"+(i));
			if (s == null) break;
		}
		last = i;
		
		System.out.println(" ");
		System.out.println("Kiss Applet Version 0.6 (c)BakuretuKen 1995-1997" );
		System.out.println(" Kiss data Title : " + kstitle );
		System.out.println(" ");
		System.out.println(" ");
		
		// 初期設定
		mt = new MediaTracker(this);
		cells = new Cell[last];
		xx = new int[last];
		yy = new int[last];
		move = new int[last];
		for ( i=0; i<last; i++) move[i]=0;
		Dimension d = size();
		backimage = createImage( d.width, d.height );
		bg = backimage.getGraphics();
		
		// 画像＆座標 読み込み
		StringTokenizer st;
		for ( i=0 ; i<last ;i++){
			celltmp = new Cell();
			s = getParameter("cel"+(i));
			if (s != null) imgname = s;
			st = new StringTokenizer(s);
			if ( st.hasMoreTokens() )
				celltmp.no = Integer.parseInt(st.nextToken());// Ｎｏ
			if ( st.hasMoreTokens() ){
				param = st.nextToken();
				celltmp.image = getImage(getDocumentBase(), imagedir + "/" + param + ".gif"); //画像
			celltmp.lbl = param;
		}
		
		if ( st.hasMoreTokens() )
			move[celltmp.no] = Integer.parseInt(st.nextToken());// ｍｏｖｅ
			cells[i] = celltmp;
		}
		
		// Media Tracker
		for ( i=0 ; i<last ;i++)  mt.addImage( cells[i].image, 0);
		s = getParameter("backcolor");
		if (s != null) imgname = s;
		st = new StringTokenizer(s);
		r = Integer.parseInt(st.nextToken());
		g = Integer.parseInt(st.nextToken());
		b = Integer.parseInt(st.nextToken());
		bc = new Color( r, g, b );
		s = getParameter("object");
		if (s != null) imgname = s;
		st = new StringTokenizer(s);
		nolast=0;
		while (st.hasMoreTokens()) {
			if ( st.hasMoreTokens() )
				xx[nolast] = Integer.parseInt(st.nextToken());
			if ( st.hasMoreTokens() )
				yy[nolast] = Integer.parseInt(st.nextToken());
			if (nolast==last) break;
			nolast++;
		}
		
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {}
	} // init()
	
// --------------------------------------------------------------
// イメージサイズ取得
	void getSize2(){
		int i;
		for ( i = 0; i < last ; i++) {
			cells[i].w = cells[i].image.getWidth(this);
			cells[i].h = cells[i].image.getHeight(this);
		}
	return;
	} // setSize2()

// --------------------------------------------------------------
// ｐａｉｎｔ（ ）
	public void paint(Graphics g) {
		if (mt.isErrorID(0)) {
			g.setColor(Color.red);
			g.fillRect(0, 0, size().width, size().height);
			g.setColor(Color.yellow);
			g.drawString("Error", 20, 60);
			return;
		}
		if (mt.checkID(0)) {
			Dimension d = size();
			bg.setColor( bc );
			bg.fillRect( 0, 0, d.width, d.height );
			bg.setColor(Color.black);
			bg.setFont(new Font("TimesRoman", Font.ITALIC, 13 ));
			bg.drawString( kstitle, 2, 15);
			for (int i = last-1; i >= 0; i--) {
				int myno = cells[i].no;
				bg.drawImage(cells[i].image, xx[myno], yy[myno], this);
			}
			g.drawImage( backimage, 0, 0, null );
		} else {
			Dimension d = size();
			g.setColor( bc );
			g.fillRect( 0, 0, d.width, d.height );
			g.setColor(Color.red);
			g.setFont(new Font("TimesRoman", Font.ITALIC, 15 ));
			g.drawString("Now Loading .....", size().width/2-50, size().height/2);
			g.setColor(Color.black);
			g.setFont(new Font("TimesRoman", Font.ITALIC, 15 ));
			g.drawString("Kiss Applet Ver0.6 (c)BakuretuKen", 2, 10);
		}
	} // paint()
	
// --------------------------------------------------------------
// ｕｐｄａｔｅ（ ）
	public void update(Graphics g) {
		paint( g );
	} // repaint()

// ----------------------------------------------------------------------
// マウスダウンイベント
	public boolean mouseDown(Event e, int x, int y) {
		num = -1; oldx = x; oldy =y; sx=x; sy=y;
		int cellno;
		for(int i = last-1; i >= 0; i--){
			cellno = cells[i].no;
			if( xx[cellno]< x && x < xx[cellno]+cells[i].w ) {
				if( yy[cellno]< y && y < yy[cellno]+cells[i].h ) {
					if ( haveColor(cells[i].image, x-xx[cellno], y-yy[cellno]) ){
						num = i;
						masx = xx[cells[num].no];
						masy = yy[cells[num].no];
						getFrame().setCursor(Frame.HAND_CURSOR);
					}
				}
			}
		}
		return true;
	} // mouseDown()

// --------------------------------------------------------------
//	マウスドラッグイベント

	public boolean mouseDrag(Event e, int x, int y) {
		if ( num != -1 && move[cells[num].no] < 20 ){
			xx[cells[num].no] = xx[cells[num].no] + ( x - oldx );
			yy[cells[num].no] = yy[cells[num].no] + ( y - oldy );
			// 動きずらい
			if ( move[cells[num].no]>0 ){
				if ( ((sx-x)*(sx-x)+(sy-y)*(sy-y)) > 53 ){
					xx[cells[num].no] = masx;
					yy[cells[num].no] = masy;
					move[cells[num].no]--;
					num = -1;
				}
			}
			// System.out.println(">> " + ((sx-x)*(sx-x)+(sy-y)*(sy-y)) );
			repaint(); // 作画
		}
		oldx = x; oldy =y;
		return true;
	} // mouseDrag

// --------------------------------------------------------------
// マウスアップイベント
	public boolean mouseUp(Event e, int x, int y) {
		if ( num!=-1 && move[cells[num].no]>0 ){
			xx[cells[num].no] = masx;
			yy[cells[num].no] = masy;
			repaint(); // 作画
		}
		getFrame().setCursor(Frame.DEFAULT_CURSOR);
		num = -1;
		return true;
	} // mouseUp()

// --------------------------------------------------------------
// ｒｕｎ（ ）
	public void run() {
		try {
			mt.waitForID(0);
		} catch (InterruptedException e) {
			return;
		}
		getSize2();
		repaint();
		if (thread != null) {
			thread.stop();
			thread = null;
		}
	} // run()


	public void start() {
		if (thread == null) {
			thread = new Thread(this);
			thread.start();
		}
	} // start()


	public void stop() {
		if (thread != null) {
			thread.stop();
			thread = null;
		}
	} // stop()
 
// --------------------------------------------------------------
// キーダウン イベント
	public boolean keyDown(Event evt,int key){
		int i;
		if(key=='e' || key=='E'){
			for ( i = 0; i < last ; i++) 
				System.out.print( xx[i]+" "+yy[i]+" ");
		}
		System.out.println(" "); System.out.println(" ");
		return(true);
	} // KeyDown()

// --------------------------------------------------------------
// クリックされた部分が透明かどうかのチェック
	public boolean haveColor( Image im, int xp, int yp ) {
		int[] pixels = new int[1];
		PixelGrabber pg = new PixelGrabber( im, xp, yp, 1, 1, pixels, 0, 1);
		try {
			pg.grabPixels();
		} catch (InterruptedException e) {
			System.err.println("interrupted waiting for pixels!");
			return false;
		}
		if ( pixels[0] < 0 ) return true;
		return false;
	} // haveColor()

// --------------------------------------------------------------
// カーソルを手の形にするためフレームを取得する(IE4.0用)
	public Frame getFrame() {
		Component parent=getParent();
		while (parent!=null&&!(parent instanceof Frame)) {
			parent=parent.getParent();
		}
		return((Frame)parent);
	} // getFrame()

// --------------------------------------------------------------
}  // End of Class

// 1998.09.14 KissApplet v0.6    BakuretuKen
// E-mail:hgb03437@niftyserve.or.jp
//[EOF]
