import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.Point;
import java.awt.Dimension;
import java.awt.TextComponent;
import java.util.*;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class Client extends Frame
    implements ActionListener, ClientInterface
{
    // GUI stuff
    private Letter letter;
    private Vector lettersOnPanel = new Vector();
    private Vector lettersOnBoard = new Vector();

    private String url=new String();

    private boolean firstTime, flag, clickedOnLetter;
    private Button getLetters;
    private TextArea chatArea, clientList;
    private TextField tf;
    private Button submit, done;
    // private Panel p,p1;
    private Picture can;

    // networking stuff
    private String name;
    private char[][] boardArray=new char[15][15];

    private NewVector words;
    private Vector existingWords;
    private ServerInterface server;
    // score stuff
    private int score;
    private int letScore;
    private int [] wordScore;
    private int mulFactor;
    private int [][] letterScoreArray=
    {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
     {1, 1, 3, 1, 1, 1, 1, 3, 1, 1, 1, 1, 3, 1, 1},
     {1, 1, 1, 2, 1, 3, 1, 1, 1, 3, 1, 2, 1, 1, 1},
     {1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1},
     {1, 1, 1, 3, 1, 1, 1, 2, 1, 1, 1, 3, 1, 1, 1},
     {1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1},
     {1, 1, 3, 1, 1, 2, 1, 1, 1, 2, 1, 1, 3, 1, 1},
     {1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1},
     {1, 1, 1, 3, 1, 1, 1, 2, 1, 1, 1, 3, 1, 1, 1},
     {1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1},
     {1, 1, 1, 2, 1, 3, 1, 1, 1, 3, 1, 2, 1, 1, 1},
     {1, 1, 3, 1, 1, 1, 1, 3, 1, 1, 1, 1, 3, 1, 1},
     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};

    private int [][] wordScoreArray=
    {{3, 1, 1, 1, 1, 3, 1, 1, 1, 3, 1, 1, 1, 1, 3},
     {1, 2, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 2, 1},
     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
     {3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3},
     {1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1},
     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
     {1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1},
     {3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3},
     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
     {1, 2, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 2, 1},
     {3, 1, 1, 1, 1, 3, 1, 1, 1, 3, 1, 1, 1, 1, 3}};
    // implement remote methods
    public String getClientName() {return name;}
    public void receiveMessage(String msg) {chatArea.append(msg+"\n");}
    public void setFirstTime(boolean state) {firstTime=state;}
    public int  getClientScore() {return score;}

    public void receiveData(char[][] bA, Vector existingWords, String[] nameArray, int[] scoreArray)
    {
	int i;
	this.boardArray=bA;
	this.existingWords = existingWords;
	System.out.print("received data - existing words are");
	for (i=0; i<existingWords.size();i++)
	    System.out.print((String)existingWords.elementAt(i)+" ");
	String tempString = new String();
	for (i=0;i<nameArray.length;i++)
	    if (nameArray[i]!=null)
		tempString=tempString+nameArray[i]+"     "+scoreArray[i]+"\n";
	
	clientList.replaceRange(tempString,0,100);
	repaint();
    }

    public Client(String url)
    {
	
	this.url = url;
	
	
	this.setResizable(false);
	//this.setSize(900,660);
	
	// initialize variables
	flag = false;
	clickedOnLetter=false;
	firstTime=false;
	lettersOnPanel = new Vector();
	//boardArray=new char[15][15];

	existingWords= new Vector();

	name="No Name";
	words = new NewVector();
	score = 0;
	wordScore = new int [200];
	letScore = 0;
	mulFactor= 1;
	
	
	// create text fields and text areas

	chatArea = new TextArea();
	chatArea.setForeground(Color.darkGray);
	chatArea.setEditable(false);
	chatArea.setSize(300,275);
	chatArea.setLocation(600,325);
	add(chatArea);
	
	clientList = new TextArea("Best of Luck",20,46,TextArea.SCROLLBARS_NONE);
	clientList.setSize(300,275);
	clientList.setLocation(600,0);
	clientList.setEditable(false);
	add(clientList);
	

	tf = new TextField(20);
	tf.setLocation(600,600);
	tf.setSize(260,660);
	add(tf);
	
	// create buttons and add listeners
	getLetters = new Button("Pass");
	getLetters.setBackground(Color.gray.darker());
	getLetters.setForeground(Color.gray.brighter());
	getLetters.addActionListener(this);
	getLetters.setSize(40,60);
	getLetters.setLocation(0,600);
	add(getLetters);

	submit = new Button("Login");
	submit.setBackground(Color.gray.darker());
	submit.setForeground(Color.gray.brighter());
	submit.addActionListener(this);
	submit.setSize(40,60);
	submit.setLocation(860,600);
	add(submit);

	done = new Button("Done");
	done.setBackground(Color.gray.darker());
	done.setForeground(Color.gray.brighter());
	done.addActionListener(this);
	done.setSize(40,60);
	done.setLocation(560,600);
	add(done);

	can = new Picture();
	can.setLocation(0,0);
	can.setSize(900,660);
	add(can);
	
	this.setResizable(false);
	this.setSize(900,660);

	repaint();

    }

    
    public static void main(String[] args)
    {
	Client c;

	if (args.length==0)
	    {
	       c = new Client("englab-75.umd.edu");
	       c.show();
	    }
	else if (args.length==1)
	    {
		c = new Client(args[0]);
		c.show();
	    }
	else
	    {
		System.out.println("Usage: java Client <serverhostname>");
		System.exit(-1);
	    }
    }


    public void actionPerformed(ActionEvent e)
    {
	// remember to disable the other buttons before login	
	if (e.getActionCommand()=="Login")
	    {
		name=tf.getText();
		tf.setText("");

		// connect to server
		url="rmi://"+url+":1099/Server";
		try
		    {
			UnicastRemoteObject.exportObject(this);
			System.out.println("looking up "+url);
			server = (ServerInterface) Naming.lookup(url);
			server.connect(this);
			System.out.println("done");
		    }
		catch(Exception e1)
		    {
			e1.printStackTrace();
			System.exit(-1);
			return;
		    }
		submit.setLabel("Submit");
	    }
	// remember to disable the other buttons before login
	else if (e.getActionCommand()=="Pass")
	    {
		int ranval, i;
		Random ran = new Random();
		Letter tempLetter;
		lettersOnPanel.removeAllElements();
		
		
		for (i=0; i<7; i++)
		    {
			tempLetter = new Letter((char)((int)((ran.nextDouble()*26))+65),
						new Point(40+80*i,610));
			lettersOnPanel.addElement(tempLetter);
		    }
		flag = true;
		repaint();
		try{server.advanceClient();}
		catch (Exception e2)
		    {
			e2.printStackTrace();
			System.exit(-1);
			return;
		    }
	    }
	else if (e.getActionCommand()=="Submit")
	    {
		String str;
		str = tf.getText();
		tf.setText("");
		
		try {server.sendMessage(this, str);}
		catch (Exception re)
		    {
			re.printStackTrace();
			System.exit(-1);
			return;
		    }
	    }
	else if (e.getActionCommand()=="Done")
	    {
		int i,j;
		boolean wrongWord1=false,wrongWord2=false,wrongWord3=false, wrongWord4=false;
		int usesExisting=0;
		repaint();
		words = new NewVector();
		String tempWord=new String();
		// create a copy of existing array
		char[][] tempArray = new char[15][15];
		
		for (i=0;i<15;i++)
		    for (j=0;j<15;j++)
			tempArray[i][j]=boardArray[i][j];
		
		
		NewSet rowSet=new NewSet(), columnSet=new NewSet();
		int rowVal=-1, colVal=-1;
		for(i=0;i<lettersOnBoard.size();i++)
		    {
			Integer x,y;
			x = new Integer((int)(((Letter)lettersOnBoard.elementAt(i)).position.getX()/40));
			y = new Integer((int)(((Letter)lettersOnBoard.elementAt(i)).position.getY()/40));
			rowSet.add(y);
			columnSet.add(x);
		    }

		if (rowSet.size()==1)
		    {
			{
			    System.out.println("Letters are in one row");
			    boolean found=false;
			    for (j=0; j<15; j++)
				if (rowSet.contains(new Integer(j))) rowVal=j;
			    if ((columnSet.minValue()!=-1)&&(rowVal!=-1))
				for (i=columnSet.minValue(); i<=columnSet.maxValue(); i++)
				    {
					if (rowVal!=0)
					    if (boardArray[i][rowVal-1]!='0') found=true;
					if (rowVal!=14)
					    if (boardArray[i][rowVal+1]!='0') found=true;
				    }
			    if ((columnSet.minValue()>0)&&(boardArray[columnSet.minValue()-1][rowVal]!='0')) found = true;
			    if ((columnSet.maxValue()<14)&&(boardArray[columnSet.maxValue()+1][rowVal]!='0')) found = true;
			    
			    if (!found) {wrongWord3=true;System.out.println("line 293");}
			}
		    }
		else if (columnSet.size()==1)
		    {
			{
			    System.out.println("Letters are in one column");
			    boolean found=false;
			    for (i=0; i<15; i++)
				if (columnSet.contains(new Integer(i)))
				    {
					colVal=i;
					System.out.println("column = "+colVal);
				    }
			    System.out.println("min row = "+rowSet.minValue()+", max row = "+rowSet.maxValue());
			    if ((rowSet.minValue()!=-1)&&(colVal!=-1))
				for (j=rowSet.minValue(); j<=rowSet.maxValue(); j++)
				    {
					if (colVal!=0)
					    if (boardArray[j][colVal-1]!='0') found=true;
					if (colVal!=14)
					    if (boardArray[j][colVal+1]!='0') found=true;
				    }
			    if ((rowSet.minValue()>0)&&(boardArray[colVal][rowSet.minValue()-1]!='0')) found = true;
			    if ((rowSet.maxValue()<14)&&(boardArray[colVal][rowSet.maxValue()+1]!='0')) found = true;
			    
			    if (!found) {wrongWord3=true;System.out.println("line 319");}
			}
		    }
		else {wrongWord4=true;System.out.println("line 322");}
		
		
		
		if (firstTime) System.out.println("FirstTime");
		if (firstTime)
		    {
			if(rowSet.size()==1)
			    {
				for (i=0; i<15; i++)
				    if (columnSet.contains(new Integer(i)))
					{
					    colVal=i;
					    System.out.println("column = "+colVal);
					}
			
				if((rowVal!=7)&&((7-columnSet.minValue())*(columnSet.maxValue()-7))<0)
				    {
					System.out.print("Invalid 1st move ");
					{wrongWord4=true;System.out.println("line 332");}
				    }
			    }
		    
			if(columnSet.size()==1)
			    {
				for (j=0; j<15; j++)
				    if (rowSet.contains(new Integer(j)))
					{
					    rowVal=j;
					    System.out.println("row = "+rowVal);
					}
				
				if((colVal!=7)&&((7-columnSet.minValue())*(rowSet.maxValue()-7))<0)
				    {
					System.out.print("Invalid 1st move ");
					{wrongWord4=true;System.out.println("line 357");}
				    }
			    }
			
		    }
		System.out.println("row,col="+rowSet.size()+","+columnSet.size());
		if ((rowSet.size()!=1)&&(columnSet.size()!=1)) {wrongWord4=true;System.out.println("line 342");}
		
		// then add the letters On Board to boardArray
		for (i=0;i<lettersOnBoard.size();i++)
		    {
			int x,y;
			boolean flag;
			x = (int)(((Letter)lettersOnBoard.elementAt(i)).position.getX()/40);
			y = (int)(((Letter)lettersOnBoard.elementAt(i)).position.getY()/40);
			boardArray[x][y]=((Letter)lettersOnBoard.elementAt(i)).c;
		    }
		
		
		for (i=0;i<15;i++)
		    if (columnSet.contains(new Integer(i)))
			{
			    System.out.println("Column "+i);
			    j=0;
			    letScore = 0;
			    mulFactor = 1;
			    while (j<15)
				{
				    if (boardArray[i][j]!='0')
					{
					    tempWord=tempWord+boardArray[i][j];

					    //Computing the letter scores
					    Letter temp = new Letter (boardArray[i][j]);
					    letScore = letScore + (temp.getLetterScore())
						*letterScoreArray[i][j];
					    mulFactor = mulFactor * wordScoreArray[i][j];
					    
					    
					    if (tempArray[i][j]!='0')
						{
						    usesExisting++;
						    //System.out.print(" usesExisting = "+usesExisting+ " ");
						}
					    j++;
					    //System.out.print(j);
					    
					    if ((j==15)&&(tempWord.length()>1))
						{
						    if(usesExisting==0)
							{
							    wrongWord1=true;
							    //System.out.print(" Wrong Word 1 ");
							}
						    if (tempWord.length()!=usesExisting)
							{
							    if(words.contains(tempWord)||existingWords.contains(tempWord))
								{
								    wrongWord2=true;
								    usesExisting=0;
								    System.out.print(" Wrong Word 2 ");
								    System.out.print(" occurances= "+words.numberOfOccurances(tempWord));
								}
							    else 
								{
								    System.out.print(" Added "+tempWord+" ");
								    words.addElement(tempWord);
								    wordScore[words.size()-1]=letScore*mulFactor;
								   
								}
							    
							}
						    else usesExisting=0;
						    tempWord = new String();
					
						}
					}
				    
				    else
					{
					    //System.out.print("Found '0' ");
					    if ((tempWord.length()>1)&&(j!=0))
						{
						    if(usesExisting==0)
							{
							    wrongWord1=true;
							    //System.out.print(" Wrong Word 1 ");
							}
						    
						    if (tempWord.length()!=usesExisting)
							{
							    if(words.contains(tempWord)||existingWords.contains(tempWord))
								{
								    wrongWord2=true;
								    usesExisting=0;
								    System.out.print(" Wrong Word 2 ");
								    System.out.print(" occurances= "+words.numberOfOccurances(tempWord));
								}
							    else 
								{
								    System.out.println("added "+tempWord);
								    words.addElement(tempWord);
								    wordScore[words.size()-1]=letScore*mulFactor;
								}
							    
							}
						    else usesExisting=0;
						    tempWord = new String();	
						}
					    
					    tempWord = new String();
					    while ((j<15)&&(boardArray[i][j]=='0'))
						{
						    j++;
						    //System.out.print(j);
						    if (j==15)
							{
							    //System.out.println(" end of row");
							}
						    else if (boardArray[i][j]!='0')
							{
							    //System.out.print(" Found Word ");
							}
						}
					    usesExisting=0;
					}
				}
			}
		usesExisting=0;
		mulFactor =1;
		letScore = 0;
		tempWord=new String();
		for (j=0;j<15;j++)
		    if (rowSet.contains(new Integer(j)))
			{
			    System.out.println("Row "+j);
			    i=0;
			    letScore = 0;
			    mulFactor = 1;
			    while (i<15)
				{
				    if (boardArray[i][j]!='0')
					{
					    //System.out.print(boardArray[i][j]+" ");
					    tempWord=tempWord+boardArray[i][j];
					    
					    //Computing the letter scores
					    Letter temp = new Letter (boardArray[i][j]);
					    letScore = letScore + (temp.getLetterScore())
						*letterScoreArray[i][j];
					    mulFactor = mulFactor * wordScoreArray[i][j];
					    
					    if (tempArray[i][j]!='0')
						{
						    usesExisting++;
						    //System.out.print(" usesExisting= "+usesExisting);
						}
					    i++;
					    //System.out.print(i);
					    
					    if ((i==15)&&(tempWord.length()>1))
						{
						    if(usesExisting==0)
							{
							    wrongWord1=true;
							    // System.out.print(" Wrong Word 1");
							}
						    if (tempWord.length()!=usesExisting)
							{
							    if(words.contains(tempWord)||existingWords.contains(tempWord))
								{
								    wrongWord2=true;
								    usesExisting=0;
								    System.out.print(" Wrong Word 2");
								}
							    else 
								{
								    System.out.print(" Added "+tempWord+ " ");
								    words.addElement(tempWord);
								    wordScore[words.size()-1]=letScore*mulFactor;
								}
							    
							}
						    else usesExisting=0;
						    tempWord = new String();	
						}
					}
				    else
					{
					    //System.out.print(" Found '0' ");
					    if ((tempWord.length()>1)&&(i!=0))
						{
						    if(usesExisting==0)
							{
							    wrongWord1=true;
							    //System.out.print(" Wrong Word 1");
							}
						    
						    if (tempWord.length()!=usesExisting)
							{
							    
							    if((words.numberOfOccurances(tempWord)>=
								(tempWord.length()-usesExisting))||
							       (existingWords.contains(tempWord)))
								{
								    wrongWord2=true;
								    usesExisting=0;
								    System.out.print(" Wrong Word 2");
								    System.out.print(" Occurances= "+words.numberOfOccurances(tempWord));
								}
							    else 
								{
								    System.out.println("added "+tempWord);
								    words.addElement(tempWord);
								    wordScore[words.size()-1]=letScore*mulFactor;
								}
							    
							}
						    else usesExisting=0;
						    tempWord = new String();	
						}
					    
					    tempWord = new String();
					    while ((i<15)&&(boardArray[i][j]=='0'))
						{
						    i++;
						    //System.out.print(i);
						    //if (i==15) System.out.println(" end of row");
						    //else if (boardArray[i][j]!='0') System.out.print(" Found Word ");
						}
					    usesExisting=0;
					}
				}
			}
		
		//System.out.println("words.size()= " +words.size());
		for(j=0;j<words.size();j++)
		    {
			try
			    {
				if (!(server.validString((String)words.elementAt(j))))
				    {
					wrongWord4=true;
					System.out.println("line 547");
				    }
			    }
			catch (Exception e1)
			    {
				e1.printStackTrace();
				System.exit(-1);
				return;
			    }
			
			System.out.println("word "+j+"is "+((String)words.elementAt(j)));
		    }
		
		
		if ((wrongWord4)||((!firstTime)&&((wrongWord1)||(wrongWord2)||(wrongWord3))))
		    {
			if (wrongWord1) System.out.println("wrong1");
			if (wrongWord2) System.out.println("wrong2");
			if (wrongWord3) System.out.println("wrong3");
			if (wrongWord4) System.out.println("wrong4");
			
			boardArray=tempArray;
			int onboard = lettersOnBoard.size();
			
			for(int cnt=0;cnt<onboard;cnt++)
			    {
				lettersOnPanel.addElement(lettersOnBoard.elementAt(cnt));
				int existing=lettersOnPanel.size();
				((Letter)lettersOnPanel.elementAt(existing-1)).setPosition(new Point
				    (40+80*(existing-1),610));
				
				words=new NewVector();
			    }
			lettersOnBoard.removeAllElements();
		    }
		else
		    {
			firstTime=false;
			for(i=0;i<words.size();i++) score = score + wordScore[i];
			try
			    {
				System.out.println("sending data to server");
				server.receiveData(this, boardArray, words);
			    }
			catch (Exception e1)
			    {
				e1.printStackTrace();
				System.exit(-1);
				return;
			    }
		    }

		// get letters for what has been used
		int ranval;
		Random ran = new Random();
		Letter tempLetter;
		int existing = lettersOnPanel.size();
		for (i=0; i<existing; i++)
		    {
			tempLetter = (Letter) lettersOnPanel.elementAt(i);
			tempLetter.setPosition ( new Point(40+80*i, 610));
		    }
		for (i=0; i<(7-existing); i++)
		    {
			tempLetter = new Letter((char)((int)((ran.nextDouble()*26))+65),
						new Point(40+80*existing+(80*i),610));
			lettersOnPanel.addElement(tempLetter);
		    }
		flag = true;
		
		usesExisting=0;
		wrongWord1=wrongWord2=wrongWord3=wrongWord4=false;
		lettersOnBoard.removeAllElements();
		repaint();
	    }
    }
	
   

    public void update(Graphics g)
    {
	can.repaint();
    }
    
  //    public void update(Graphics g)
//      {
//  	int i,j;
	
//          Dimension d = getSize();
//  	backImage = createImage(d.width, d.height);
//  	backGraphics = backImage.getGraphics();

//  	backGraphics.setColor(Color.black);
//  	backGraphics.fillRect(0,0,d.width,d.height);

//  	backGraphics.drawImage(board, 0, 0, this);
//  	backGraphics.drawImage(panel, 0, 600, this);
//  	backGraphics.drawImage(icon,730,280,this);

//  	// here write stuff for reading boardArray and displaying the images!
//  	for(i=0;i<15;i++)
//  	    for(j=0;j<15;j++)
//  		{
//  		    Letter scrLetter = new Letter(boardArray[i][j], new Point(40*i, 40*j));
//  		    if (scrLetter.c!='0') scrLetter.draw(backGraphics, this);
//  		}
//  	// panel stuff
//  	for(i=0;i<lettersOnPanel.size();i++)
//  	    {
//  		Letter scrLetter = new Letter();
//  		scrLetter = (Letter) lettersOnPanel.elementAt(i);
//  		scrLetter.draw(backGraphics, this);
//  	    }
//  	// temp board stuff
//  	for(i=0;i<lettersOnBoard.size();i++)
//  	    {
//  		Letter scrLetter = new Letter();
//  		scrLetter = (Letter) lettersOnBoard.elementAt(i);
//  		scrLetter.draw(backGraphics, this);
//  	    }
//  	paint(g);
//      }

    //public void paint(Graphics g){g.drawImage(backImage, 0, 0, this);}

    public Letter pointToLetter(Point p)
    {
	int i;
	Letter l;

	for(i=0; i<lettersOnPanel.size(); i++)
	    {
		l = (Letter) lettersOnPanel.elementAt(i);
		Rectangle R = new Rectangle (l.position, new Dimension(40,40));
		if(R.contains(p))
		    {
			clickedOnLetter = true;
			return(l);
		    }
	    }
	for(i=0; i<lettersOnBoard.size(); i++)
	    {
		l = (Letter) lettersOnBoard.elementAt(i);
		Rectangle R = new Rectangle (l.position, new Dimension(40,40));
		if(R.contains(p))
		    {
			clickedOnLetter = true;
			return(l);
		    }
	    }
	return(new Letter());
    }
    public void deactivate()
    {
	getLetters.setEnabled(false);
	done.setEnabled(false);
    }

    public void activate()
    {
	getLetters.setEnabled(true);
	done.setEnabled(true);
    }
     public void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy)
    {
	gbc.gridx=gx;
	gbc.gridy=gy;
	gbc.gridwidth=gw;
	gbc.gridheight=gh;
	gbc.weightx=wx;
	gbc.weighty=wy;
    }

    

    public class Letter
    {
	public char c;
	public Point position;
	public Point oldposition;
	private int letterScore;
	public Letter() {}
	public Letter(char chr) {c=chr;}
	
	public Letter(char chr, Point p)
	{
	    c = chr;
	    position = p;
	    oldposition = p;
	}
	public void setPosition(Point p)
	{
	    position = p;
	    oldposition = p;
	}
	public void draw(Graphics g, Picture client)
	{
	    Image letter = Toolkit.getDefaultToolkit().getImage("images/"+c+".jpg");
	    g.drawImage(letter, (int) position.getX(), (int) position.getY(), client);
	}
	public int getLetterScore()
	{
	    
	    if((c=='A')||(c=='E')||(c=='I')||
	       (c=='L')||(c=='N')||(c=='O')||
	       (c=='R')||(c=='S')||(c=='T')||(c=='U'))return 1;
	    if((c=='B')||(c=='D')||(c=='G'))return 2;
	    if((c=='C')||(c=='M')||(c=='P'))return 3;
	    if((c=='F')||(c=='H')||
	       (c=='V')||(c=='W')||(c=='Y'))return 4;
	    if(c=='K')return 5;
	    if((c=='J')||(c=='X'))return 8;
	    if((c=='Q')||(c=='Z'))return 10;
	    return 0;
	}
	
	
    }



    private class Picture extends Canvas
	implements MouseListener, MouseMotionListener
    {
	private Image backImage;
	private Graphics backGraphics;
	private Dimension backDimension;
	private Image board = Toolkit.getDefaultToolkit().getImage("images/boardpic.jpg");
	private Image panel = Toolkit.getDefaultToolkit().getImage("images/let_panel.jpg");
	private Image icon = Toolkit.getDefaultToolkit().getImage("images/icon1.jpg");
    

	public Picture()
	{
	    // add mouse listeners
	    addMouseListener(this);
	    addMouseMotionListener(this);
	    
	}    
	 
	public void update(Graphics g)
	{

	    int i,j;
	    
	    Dimension d = getSize();
	    backImage = createImage(d.width, d.height);
	    backGraphics = backImage.getGraphics();
	    
	    backGraphics.setColor(Color.black);
	    backGraphics.fillRect(0,0,d.width,d.height);

	    backGraphics.drawImage(board, 0, 0, this);
	    backGraphics.drawImage(panel, 0, 600, this);
	    backGraphics.drawImage(icon,730,275,this);
	    
	    // here write stuff for reading boardArray and displaying the images!
	    for(i=0;i<15;i++)
		for(j=0;j<15;j++)
		{
		    Letter scrLetter = new Letter(boardArray[i][j], new Point(40*i, 40*j));
		    if (scrLetter.c!='0') scrLetter.draw(backGraphics, this);
		}
	    // panel stuff
	    for(i=0;i<lettersOnPanel.size();i++)
		{
		    Letter scrLetter = new Letter();
		    scrLetter = (Letter) lettersOnPanel.elementAt(i);
		    scrLetter.draw(backGraphics, this);
		}
	    // temp board stuff
	    for(i=0;i<lettersOnBoard.size();i++)
		{
		Letter scrLetter = new Letter();
		scrLetter = (Letter) lettersOnBoard.elementAt(i);
		scrLetter.draw(backGraphics, this);
		}
	    paint(g);
	}
	public void paint(Graphics g)
	{
	    if (backImage!=null)
		g.drawImage(backImage, 0, 0, this);
	}

	public void mousePressed(MouseEvent e)
	{
	    letter = pointToLetter(e.getPoint());
	    if (clickedOnLetter)
		{
		    flag=false;
		    Point p = e.getPoint();
		    p.translate(-20,-20);
		    letter.position = p;
		}
	}
	
	public void mouseReleased(MouseEvent e)
	{
	    if (clickedOnLetter)
		{
		    clickedOnLetter=false;
		    if (new Rectangle(0,0,600,600).contains(letter.position))
			{
			    
			    letter.setPosition(new Point(((int)(e.getX()/40))*40, ((int)(e.getY()/40))*40));
			    
			    
			    if(lettersOnPanel.contains(letter))
				{
				    {
					lettersOnBoard.addElement(letter);
					lettersOnPanel.removeElement(letter);
					int existing = lettersOnPanel.size();
					for(int i=0;i<existing;i++)
					    ((Letter)lettersOnPanel.elementAt(i)).setPosition(new Point(40+80*i,610));
				    }
				    
				}
			    
			}
		    else if(new Rectangle(0,600,600,60).contains(letter.position))
			{
			    
			    if (lettersOnPanel.contains(letter))
				for(int i=0;i<lettersOnPanel.size();i++)
				    ((Letter)lettersOnPanel.elementAt(i)).setPosition(new Point(40+80*i,610));
			    if (lettersOnBoard.contains(letter))
				{
				    lettersOnPanel.addElement(letter);
				    lettersOnBoard.removeElement(letter);
				}
			}
		    else
			{
			    letter.position = letter.oldposition;
			}
		    letter = new Letter();
		    repaint();
		}
	}
	
	
	public void mouseDragged(MouseEvent e)
	{
	    if (clickedOnLetter)
		{
		    Point p = e.getPoint();
		    p.translate(-20,-20);
		    letter.position = p;
		    repaint();
		}
	}
	public void mouseClicked(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public void mouseMoved(MouseEvent e){}	
    }
}

    

