//******************************************************************************
// blocks2.java:	Applet
//
//******************************************************************************
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.util.*;

//==============================================================================
// Main Class for applet blocks2
//
//==============================================================================
public class blocks2 extends Applet
{
	Image card,delete,fish,bell,note,sad;
	AudioClip alarm,ambi1,gitar23,hate,sardine,tape;

	
	int table[][];
	int clickx,clicky;
	boolean Clickt;
	int selectX,selectY,selectVal;
	Button Upbtn,Downbtn,Leftbtn,Rightbtn,Playbtn,PlayAllbtn;
	int listsub=0;
	int list[];

	
	public blocks2()
	{
		// TODO: Add constructor code here
	}

	// APPLET INFO SUPPORT:
	//		The getAppletInfo() method returns a string describing the applet's
	// author, copyright date, or miscellaneous information.
    //--------------------------------------------------------------------------
	public String getAppletInfo()
	{
		return "Name: blocks\r\n" +
		       "Author: Dan Schierl\r\n" +
		       "Created with Microsoft Visual J++ Version 1.1";
	}


	// The init() method is called by the AWT when an applet is first loaded or
	// reloaded.  Override this method to perform whatever initialization your
	// applet needs, such as initializing data structures, loading images or
	// fonts, creating frame windows, setting the layout manager, or adding UI
	// components.
    //--------------------------------------------------------------------------
	public void init()
	{

    resize(250, 250);
	URL codeBase=getCodeBase();
	
	alarm=getAudioClip(codeBase,"alarm.au");
	ambi1=getAudioClip(codeBase,"ambi1.au");
	gitar23=getAudioClip(codeBase,"gitar23.au");
	hate=getAudioClip(codeBase,"hate.au");
	sardine=getAudioClip(codeBase,"sardine.au");
	tape=getAudioClip(codeBase,"tape.au");

	alarm.play();
		wastetime();
	ambi1.play();
		wastetime();
	gitar23.play();
		wastetime();
	hate.play();
		wastetime();
	sardine.play();
		wastetime();
	tape.play();
	
    list=new int[7];
	card=getImage(codeBase, "card.gif");
	delete=getImage(codeBase, "delete.gif");
	fish=getImage(codeBase, "fish.gif");
	bell=getImage(codeBase, "bell.gif");
	note=getImage(codeBase, "note.gif");
	sad=getImage(codeBase, "sad.gif");
	Clickt=false;
	Upbtn=new Button("Up");
	Downbtn=new Button("Down");
	Leftbtn=new Button("Left");
	Rightbtn=new Button("Right");
	Playbtn=new Button("Play");
	PlayAllbtn=new Button("Play All");
	
	add(Upbtn);
	add(Downbtn);
	add(Leftbtn);
	add(Rightbtn);
	add(Playbtn);
	add(PlayAllbtn);

	table=new int[6][3];
	for (int loopx=0;loopx<6;loopx++)
		for (int loopy=0;loopy<3;loopy++)
			table[loopx][loopy]=0;

     table [3][0]=1;
	 table [4][1]=2;
	 table [5][2]=3;
	 table [3][1]=4;
	 table [2][2]=5;
	 table [1][0]=6;

	 selectX=-1;
	 selectY=-1;
	 selectVal=0;

		// TODO: Place additional initialization code here
	}

	// Place additional applet clean up code here.  destroy() is called when
	// when you applet is terminating and being unloaded.
	//-------------------------------------------------------------------------
	public void destroy()
	{
		// TODO: Place applet cleanup code here
	}

	// blocks Paint Handler
	//--------------------------------------------------------------------------
	public void paint(Graphics g)
	{
		
		for (int x=0;x<4;x++)
		{
			g.setColor(Color.darkGray);
			g.drawLine(24,(x*24)+48,168,(x*24)+48);
			g.setColor(Color.white);
			g.drawLine(24,(x*24)+49,168,(x*24)+49);
		}
		for (int x=0;x<7;x++)
		{	
			g.setColor(Color.darkGray);
			g.drawLine(24+(x*24),48,24+(x*24),120);
			g.setColor(Color.white);
			g.drawLine(25+(x*24),48,25+(x*24),120);
		}
		
		listsub=1;
		for (int loopy=0;loopy<3;loopy++)
			for (int loopx=0;loopx<6;loopx++)
			{	
				if (table[loopx][loopy]!=0)
					{
					 list[listsub]=table[loopx][loopy];
					 listsub++;
					 drawblock(g,loopx,loopy,table[loopx][loopy]);
					}
			}

		if (Clickt)
		{
		  g.setColor(Color.blue);
		  g.drawRect(26+(24*selectX),50+(24*selectY),20,20);
		  Clickt=false;
		}
		
	}

	//--------------------------------------------------------------------------
	
	void drawblock(Graphics g, int x, int y, int num)
	{
		int coordx=25+(x*24);
		int coordy=50+(y*24);
		if (num==1)
			g.drawImage(card,coordx,coordy,24,24, this);
		if (num==2)
			g.drawImage(delete,coordx,coordy,24,24, this);
		if (num==3)
			g.drawImage(fish,coordx,coordy,24,24, this);
		if (num==4)
			g.drawImage(bell,coordx,coordy,24,24, this);
		if (num==5)
			g.drawImage(note,coordx,coordy,24,24, this);
		if (num==6)
			g.drawImage(sad,coordx,coordy,24,24, this);
	}
	public void start()
	{
		// TODO: Place additional applet start code here
	}
	
	//		The stop() method is called when the page containing the applet is
	// no longer on the screen. The AppletWizard's initial implementation of
	// this method stops execution of the applet's thread.
	//--------------------------------------------------------------------------
	public void stop()
	{
	}

	public boolean mouseDown(Event evt, int x, int y)
	{
		if ((x>=24) && (x<=167) && (y>=48) && (y<=119))

		{
		 clickx=(x-24)/24;
		 clicky=(y-48)/24;
		 if (table[clickx][clicky]!=0)
			{
			  selectX=clickx;
			  selectY=clicky;
			  selectVal=table[clickx][clicky];
			  Clickt=true;
			}
		 else
			{
			  selectX=-1;
		      selectY=-1;
			  selectVal=0;
			 }
		 
         repaint();
		}
         return true;
       }

	public boolean action(Event evt, Object arg)
	{
		if (evt.target instanceof Button)
			HandleButtons(arg);
		return true;
	}

	void HandleButtons(Object label)
	{
	int tempX, tempY;
		 
	if (label=="Play All")
		{
		  for(int loop=0;loop<7;++loop)
			{
			if (list[loop]==1)
				alarm.play();
			if (list[loop]==2)
				ambi1.play();
			if (list[loop]==3)
				gitar23.play();
			if (list[loop]==4)
				hate.play();
			if (list[loop]==5)
				sardine.play();
			if (list[loop]==6)
				tape.play();
			wastetime();
			}
		}
	if ((selectX!=-1)&&(selectY!=-1))
	{
		tempX=selectX;
		tempY=selectY;
		if (label=="Up")
			tempY--;
		if (label=="Down")
			tempY++;
		if (label=="Left")
			tempX--;
		if (label=="Right")
			tempX++;
		if (label=="Play")
		{
			if (selectVal==1)
				alarm.play();
			if (selectVal==2)
				ambi1.play();
			if (selectVal==3)
				gitar23.play();
			if (selectVal==4)
				hate.play();
			if (selectVal==5)
				sardine.play();
			if (selectVal==6)
				tape.play();
		}
		
		if ((tempX>=0)&&(tempX<=5)&&(tempY>=0)&&(tempY<=2))
		{
			
			if (table[tempX][tempY]==0)
			{
				table[tempX][tempY]=selectVal;
				table[selectX][selectY]=0;
				selectX=tempX;
				selectY=tempY;
				Clickt=true;
				repaint();
			}
		}
	}

    
	}

	void wastetime()
	{
		Date today1=new Date();
		Date today2;

		int then=today1.getSeconds();
		int now=then;
		while (now-then<2)
		{
			for (int loop =0;loop<1000;loop++)
			{
				//
			}
			today2=new Date();
			now=today2.getSeconds();
			if ((now-then)<0)
				then=then-60;
		}

	}


}
