// File: ChatExample.java (Module 13) // // Author: Rahul Simha // Created: Nov 28, 1998 // Modified: Nov 13, 2000. // // Template for Ex. 13.7: A chat program using sockets. import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.net.*; class ChatClient extends JFrame implements Runnable { OutputStream outStream; // The streams. InputStream inStream; PrintWriter pw; JTextField tf; // For the user to write in. JTextField tf; // For the user to write in. JTextArea messageBoard; // To display the conversation. JScrollPane sc; public ChatClient (OutputStream outStream, InputStream inStream) { // Store the stream references and create a PrintWriter this.outStream = outStream; this.inStream = inStream; pw = new PrintWriter (outStream, true); // Create the frame. this.setSize (400,200); this.setLocation (0,100); this.setTitle ("Chat Client"); Container cPane = this.getContentPane(); // cPane.setLayout (new BorderLayout()); // This is where messages will be displayed: messageBoard = new JTextArea(); sc = new JScrollPane (messageBoard); cPane.add (sc, BorderLayout.CENTER); tf = new JTextField (); tf.setForeground (Color.blue); tf.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent a) { if (tf.getText().equalsIgnoreCase ("Bye")) { try { pw.println ("Bye"); ChatClient.this.outStream.close (); ChatClient.this.dispose (); } catch (IOException e) { System.out.println (e); } } else { displayText ("ME: " + tf.getText(), Color.blue); pw.println (tf.getText()); tf.setText(""); } } } ); cPane.add (tf, BorderLayout.SOUTH); this.setVisible (true); } // Display text on a message panel using Label's. void displayText (String s, Color c) { messageBoard.setForeground (c); // Add to the message panel. messageBoard.setText ( messageBoard.getText() + "\n" + s); // Scroll to the bottom. JViewport vp = sc.getViewport(); Dimension D = vp.getViewSize(); vp.setViewPosition ( new Point (0, D.height) ); } // Must implement the run() method. public void run () { try { // Only reading needs to be done here. InputStreamReader isr = new InputStreamReader (inStream); LineNumberReader lnr = new LineNumberReader (isr); // Read line by line. String s = lnr.readLine(); while (s != null) { s = s.trim(); displayText ("YOU: " + s, Color.red); if (s.equalsIgnoreCase ("Bye")) break; s = lnr.readLine(); } inStream.close (); } catch (IOException e) { System.out.println (e); } } } // This is an independent quit button to quit the application. class QuitButton extends JFrame { public QuitButton () { this.setSize (80,50); this.setLocation (0, 0); this.setTitle ("Quit button"); Container cPane = this.getContentPane(); // cPane.setLayout (new BorderLayout()); JButton quitb = new JButton ("QUIT"); quitb.setBackground (Color.red); quitb.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent a) { System.exit (0); } } ); cPane.add (quitb, BorderLayout.CENTER); this.setVisible (true); } } public class ChatExample { public static void main (String[] argv) { // Create an independent quit button. QuitButton q = new QuitButton (); try { if ( (argv==null) || (argv.length == 0) ) { // Server // INSERT YOUR CODE HERE. } else { // Client form - connect elsewhere. // Extract address and port from command line. String address = argv[0]; int port = Integer.parseInt (argv[1]); // INSERT YOUR CODE HERE. } } catch (IOException e) { System.out.println (e); } catch (NumberFormatException e) { System.out.println (e); } } }