/**********************************************************
* Filename      : client.java 
* Version               : Beta V1.0
* written by    : Dr. Roger Webster 
*
* This is the applet portion of the client.
* The game image and the port should be changed
* This calls clientwindow.java which does most of the work.
**********************************************************/
import java.awt.*;
import java.applet.Applet;
import java.net.*;

public class client extends Applet 
{
        clientwindow window;    
        LoginWindow loginwin;
        String Username;
        String param;
        int port = 7992;        // YOU MUST CHANGE THIS
        Image GameImage = null;

public void init ()
{
        GameImage = this.getImage(this.getDocumentBase(), "title.gif");
        System.out.println("port is " + port);
        loginwin = new LoginWindow(this, "Login Window");
        loginwin.show();
}

public void stop () 
{
                System.out.println("stop");
                window.dispose(); 
}

public void SendInfo(String text) 
{
        if (text.equals(""))
                Username = "DrewBledsoe";
        else
                Username = text;
        System.out.println ("username is " + Username);
        window = new clientwindow(this, this.getDocumentBase(), port, Username, GameImage);  
}
} // end class

class LoginWindow extends Frame
{
        public client parent;
        public Panel buttonPanel;
        public Button login;
        public Label user_label;
        public TextField user, password;

        public LoginWindow(client parent, String wintitle)
        {
                super(wintitle);
                this.parent = parent;
                user_label = new Label("What's Your name:");
                user = new TextField(25);

                buttonPanel = new Panel();
                login = new Button("Connect to Server");

                //Add button to panel and panel to window
                setLayout(new FlowLayout());
                buttonPanel.setLayout(new GridLayout(7,1));
                buttonPanel.add(new Label("Please enter your name ..."));
                buttonPanel.add(user_label);    
                buttonPanel.add(user);          
                buttonPanel.add(new Label(""));
                buttonPanel.add(login);
                add(buttonPanel);

                //Size window to preferred size
                this.reshape(300, 300, 300, 300);
                move(150,300);
                user.requestFocus();
                this.show();
        }//end LoginWindow


        //  uses the jdk1.0.2 event handler...
        public boolean action(Event e, Object arg)
        {

                        if(e.target == login || e.target == user)
                        {
                                if(this.isShowing())
                                {
                                        try
                                        {       
                                                parent.SendInfo(user.getText());
                                        }
                                        catch(Exception exceptn)
                                        {
                                                exceptn.printStackTrace();
                                                System.out.println("Error:" + exceptn);
                                        }
                                        finally
                                        {
                                           dispose();
                                        }
                                }
                        }
                return true;
        } //end action(Event, Object)
}//end class definition


