import java.applet.*; import java.awt.*; import java.io.*; import java.net.*; public class Chapplet extends Applet { public AudioClip laugh; public AudioClip slaugh; public AudioClip wakeup; private String serverName = ManyServer.class; private final String defaultServerName = "193.40.238.40"; private String serverPortStr = null; private final String defaultServerPortStr = "3012"; private String chatChannel = null; private final String defaultChatChannel = "Default"; private int serverPort; ChatConnection chatConnection; Thread chatConnectionThread; TextArea textDisplayArea; TextArea textInputArea; Button sendButton; Panel bottomPanel; private int previousEvKey = 0; public void init() { try { serverName=getParameter("server"); serverPortStr=getParameter("port"); chatChannel=getParameter("channel"); } catch ( Exception e ) { serverName = defaultServerName; serverPortStr = defaultServerPortStr; chatChannel = defaultChatChannel; } serverPort = Integer.parseInt(serverPortStr); bottomPanel = new Panel(); bottomPanel.setLayout(new BorderLayout()); bottomPanel.add("North", textInputArea = new TextArea(5, 80)); bottomPanel.add("South", sendButton = new Button("Send")); setLayout(new BorderLayout()); add("South", bottomPanel); add("Center", textDisplayArea = new TextArea(20, 80)); textDisplayArea.setEditable(false); try { laugh = getAudioClip(getDocumentBase(),"laugh.au"); slaugh = getAudioClip(getDocumentBase(),"slaugh.au"); wakeup = getAudioClip(getDocumentBase(),"wakeup.au"); } catch ( Exception e ) { laugh = null; slaugh = null; wakeup = null; } chatConnection = new ChatConnection( serverName, serverPort, chatChannel, this, textDisplayArea ); chatConnectionThread = new Thread(chatConnection); chatConnectionThread.start(); } public boolean action(Event ev, Object ob) { if (ev.target.equals(sendButton)) { String message = textInputArea.getText(); chatConnection.write(message+"\n"); textInputArea.setText(""); return true; } return false; } public static void main ( String args[] ) { MainFrame frame = new MainFrame (" C H A P P L E T"); Chapplet chapplet = new Chapplet(); chapplet.init(); chapplet.start(); frame.add("Center",chapplet); frame.pack(); frame.show(); } } class MainFrame extends Frame { public MainFrame( String str ) { super(str); } public boolean handleEvent(Event ev) { if ( ev.id == Event.WINDOW_DESTROY ) { System.exit(0); } return super.handleEvent(ev); } } class ChatConnection extends Thread { Socket socket; DataOutputStream outputs; DataInputStream inputs; String serverName; int serverPort; String chatChannel; TextArea textDisplayArea; Chapplet chapplet; public ChatConnection( String _serverName, int _serverPort, String _chatChannel, Chapplet _chapplet, TextArea _textDisplayArea ) { serverName = _serverName; serverPort = _serverPort; chatChannel = _chatChannel; chapplet = _chapplet; textDisplayArea = _textDisplayArea; } public void run() { try { socket = new Socket(serverName,serverPort); outputs = new DataOutputStream(socket.getOutputStream()); inputs = new DataInputStream(socket.getInputStream()); String message; this.write("CHANNEL " + chatChannel); this.write("HISTORY"); boolean go = true; while (go) { message = inputs.readLine(); if ( message == null ) { go = false; } else { textDisplayArea.appendText(message+"\n"); if (message.indexOf( ":-)" ) > -1 ) { if ( chapplet.laugh != null ) { chapplet.laugh.play(); } } if (message.indexOf( ":)" ) > -1 ) { if ( chapplet.slaugh != null ) { chapplet.slaugh.play(); } } if (message.indexOf( "!!!" ) > -1 ) { if ( chapplet.wakeup != null ) { chapplet.wakeup.play(); } } } } } catch (Exception e) { textDisplayArea.appendText("CONNECTION ERROR :"+"\n"); textDisplayArea.appendText(e.getMessage()+"\n"); } } void write( String mess ) { try { outputs.writeBytes(mess+"\n"); outputs.flush (); } catch (Exception e) { textDisplayArea.appendText("OUTPUT ERROR :"+"\n"); textDisplayArea.appendText(e.getMessage()+"\n"); } } }