/** *Class Chat is the GUI for this Chat application that implements ActionListener *to register user actions. *Internetprogrammering 1 - Course *@author Martin Carlsson */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; public class Chat extends JFrame implements ActionListener { private String host; // Server/host to connect to private int port; // Port to be used for the connection private Socket theSocket; private Thread receiveactivity; private static String usage = "usage: java Chat host port\n"+ "example:java Chat this.is.the.host.address 7000"; private JTextField writeField = new JTextField(46); // Text field for writing messages in private JTextArea messageArea = new JTextArea(); // Text area for messages received from the server private JScrollPane jsp = new JScrollPane(messageArea); // Scroll pane with the message area private JPanel up = new JPanel(),down=new JPanel(); // North and south panels private JButton sendButton = new JButton(), quit = new JButton(); // Send and Quit buttons private Receiver receiver; private Sender sender; /*Constructor*/ public Chat(String host, int port)throws IOException { this.host=host; this.port=port; /*Container to put everything in*/ Container con = getContentPane(); // Layout of the north panel up.setLayout(new FlowLayout(FlowLayout.RIGHT)); up.setBackground(Color.lightGray); up.add(quit); // Layout of the south panel down.setLayout(new FlowLayout(FlowLayout.RIGHT)); down.setBackground(Color.lightGray); down.add(writeField); down.add(sendButton); // Prevent changes in the message area messageArea.setEditable(false); // Layout of the text field writeField.setText(" "); writeField.requestFocus(); // Text for the buttons sendButton.setText("SEND"); quit.setText("QUIT"); /*Adds listener*/ sendButton.addActionListener(this); quit.addActionListener(this); writeField.addActionListener(this); // Adds panels con.add(down,BorderLayout.SOUTH); con.add(up,BorderLayout.NORTH); //Adds scroll area with text area con.add(jsp,BorderLayout.CENTER); //Sets the window size and makes it all visible setSize(600,300); show(); // Tries to create a socket try { theSocket = new Socket(host, port); } catch(UnknownHostException uHe) { messageArea.setText("Unable to connect to "+ host + " on " + port +".....closing"); System.err.println("Unable to connect to "+ host + " on " + port +".....closing"); setTitle("Java Chat Client"); try { Thread.sleep(3000); // Sleeps awhile to show messages System.exit(0); // Then closes } catch(InterruptedException e) { } } catch (IOException iE) { messageArea.setText("Unable to connect to " + host + " on " + port + ".....closing"); System.err.println("Unable to connect to " + host + " on " + port + ".....closing"); try{ Thread.sleep(3000); // Sleeps awhile to show messages System.exit(0); // Then closes } catch(InterruptedException e) { } } sender=new Sender(theSocket); // Creates an instance of class Sender receiver=new Receiver(this, theSocket); // Creates an instance of class Receiver receiveactivity= new Thread(receiver); receiveactivity.start(); // Starts the Receiver thread } // Used to clear the message field public void clearWriteField() { writeField.setText(""); } // Message from server public void setIncomingMessage(String s) { messageArea.append("\n" + s); } /*Decides what will happend when an user action is performed*/ public void actionPerformed(ActionEvent ae) { if(ae.getSource()==writeField || ae.getSource()==sendButton) { try { sender.sendAway(writeField.getText().trim()); clearWriteField(); } catch(Exception e) { messageArea.append("\n Not able to send message at this time"); } } else if(ae.getSource()==quit) { try { sender.closeOutStream(); receiver.closeInStream(); if(theSocket!=null) theSocket.close(); } catch(IOException iE) { messageArea.append("\n Please wait...having some trouble closing.."); } catch(NullPointerException nE) { messageArea.append("\n Please wait...having some trouble closing.."); } System.exit(0); } } // Main public static void main (String[] args) { int port=2000; // default port String host="localhost"; // default host String portText = "port must be a number between"+ "0 and 65535"; System.out.println("Welcome to Chat\n" +usage+"\n"); try { //If there are 2 or more arguments if(args.length>=2){ host=args[0]; port =Integer.parseInt(args[1]); } //If there is 1 argument, i.e. host else if(args.length==1){ host=args[0]; } // To make sure the port is within the valid range if(port > 65535 || port < 0) System.err.println(portText); else new Chat(host, port); // starts } catch(NumberFormatException nFe) { System.err.println(portText); } catch(Exception e){ System.err.println(usage); } } }//End class Chat