All pastes #506919 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-05-24 08:45 UTC
rendered paste body
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
/**
 * What the user runs to play the game.  Connects to the server, and provides an interface for playing.
 * 
 * @author Thomas Horner 
 * @version 5-16-07
 */
public class Client implements ActionListener
{
    Button loginButton, registerButton, tempLogOut, tempSend;
    TextField nameField,serverField, tempChat;
    TextArea tempChatbox,tempPlayerList;
    Label title,nfH,pfH,sfH;
    Socket sSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;
    boolean cc = false;
    
    public static void main (String[] args)
    {
	Client me = new Client();
    }
    public Client()
    {   
        title = new Label("Login");
        title.setForeground(new Color (200,180,180));
        nfH = new Label("Login Name");
        nfH.setForeground(new Color (200,180,180));
        sfH = new Label("Server IP");
        sfH.setForeground(new Color (200,180,180));
        title.setAlignment(1);
        
        
        Frame gameWindow = new Frame("IMMURE - Login");
        gameWindow.setTitle("IMMURE - Login");
        gameWindow.setLayout(new BorderLayout());
        gameWindow.setBackground(Color.black);
        gameWindow.setSize (450,200);
        
        
        nameField = new TextField (20);
        serverField = new TextField (20);
        
        loginButton = new Button("Login");
        
        loginButton.addActionListener(this);
        
        registerButton = new Button("New Account");
        
        nameField.setBackground(new Color (80,60,60));
        nameField.setForeground(new Color (200,180,180));
        nameField.setFont(new Font("Verdana", Font.BOLD, 14));
        
        serverField.setBackground(new Color (80,60,60));
        serverField.setForeground(new Color (200,180,180));
        serverField.setFont(new Font("Verdana", Font.BOLD, 14));
        
        loginButton.setBackground(new Color (80,60,60));
        loginButton.setForeground(new Color (200,180,180));
        
        registerButton.setBackground(new Color (80,60,60));
        registerButton.setForeground(new Color (200,180,180));
        
        
        Panel l = new Panel();
        Panel ls = new Panel();
        ls.add(nfH);
        ls.add(nameField);
        l.setLayout(new BorderLayout());
        l.add("Center",ls);
        Panel ls2 = new Panel();
        ls2.add(sfH);
        ls2.add(serverField);
        l.add("North",ls2);
        
        
        Panel p = new Panel();
        p.add(loginButton);
        p.add(registerButton);
        
        
        gameWindow.add("Center", l);
        gameWindow.add("South", p);
        gameWindow.add("North", title);
        
        
        gameWindow.pack();
        gameWindow.show();
        
 gameWindow.addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent we){
          //tells the server to exit
          if (cc) out.println("EXIT");
        System.exit(0);
      }
    });
    
    
    }
    
    public void actionPerformed(ActionEvent ae)
    {
            if (ae.getSource().equals(loginButton))
            {
                String ip;
                ip = serverField.getText();
                try {
                login(ip);
                } catch (IOException e) { System.out.println ("Network connection to " + ip + " failed!"); }
            }
    }
    
    
    public void login(String ip) throws IOException
    {
        try
        {
            sSocket = new Socket (ip,6188);
            out = new PrintWriter(sSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                                        sSocket.getInputStream()));
        }
        catch (UnknownHostException e) 
        {
            System.out.println("Couldn't connect to " + ip + " on port 6188.");
        }
        cc = true;
        out.println ("LOGIN " + nameField.getText());
        String ret = null;
        try {
            boolean quit = false;
            while (!quit)
            {
                ret = in.readLine();
                if (ret != null)
                {
                    quit = true;
                }
            }
        } catch (Exception e) { System.out.println ("Something happened..."); }
        handleLogin(ret,ip);
        out.close();
        in.close();
        sSocket.close();
    }
    
    public createMainRoom (String ip)
    {
    	  try
        {
            sSocket = new Socket (ip,6188);
            out = new PrintWriter(sSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                                        sSocket.getInputStream()));
        }
        catch (UnknownHostException e) 
        {
            System.out.println("Couldn't connect to " + ip + " on port 6188.");
        }
	  
        gameWindow.removeAll();
	  gameWindow.setTitle("IMMURE - Testing Multiple Clients");
	  
        tempChatBox = newTextArea ("Chat Box",20,60);
	  tempPlayerList = newTextArea ("Chatters",20,20);

        gameWindow.add ("WEST",tempChatBox);
	  gameWindow.add ("EAST",tempPlayerList);

	   Panel ChatFunctions = new Panel();
 	  tempChat = new TextField (60);
	  tempSend = new Button ("Send");
	  tempLogOut = new Button ("Log Out");
        ChatFunctions.add (tempChat);
        ChatFunctions.add (tempSend);
        ChatFunctions.add (tempLogOut);

        gameWindow.add ("SOUTH",ChatFunctions);
    }
        
        
    public void handleLogin (String code, String ip)
    {
           title.setText (code);
	     createMainRoom(ip);
    }
}