home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch18 / TextClient.java < prev    next >
Text File  |  1997-02-21  |  5KB  |  160 lines

  1. // The Text Client applet
  2.  
  3. // Written by Bernie Roehl, January 1997
  4.  
  5. import java.applet.*;
  6. import java.awt.*;
  7. import java.net.*;
  8. import java.io.*;
  9. import java.util.*;
  10. import multi.*;
  11.  
  12. public class TextClient extends Applet {
  13.  
  14.     public static final String version = "0.1";
  15.  
  16.     TextField inputField;
  17.     TextArea outputArea;
  18.  
  19.     TextReceiver textReceiver;
  20.     TextSender textSender;
  21.  
  22.     World world = null;  // reference to the world in the multi-user client
  23.  
  24.     public String getAppletInfo() {
  25.         return "Multi-User Chat Text Client" + version + ", written by Bernie Roehl";
  26.     }
  27.  
  28.     public String[][] getParameterInfo() {
  29.         String[][] info = {
  30.             { "host", "string", "name or IP address of filtering host" },
  31.             { "port", "integer", "IP port to which to send text" }
  32.         };
  33.         return info;
  34.     }
  35.  
  36.     public void init() {
  37.  
  38.         // read applet parameters
  39.  
  40.         String hostname = getParameter("host");
  41.         int port = Integer.parseInt(getParameter("port"));
  42.  
  43.         if (hostname == null)
  44.             hostname = getDocumentBase().getHost();
  45.  
  46.         // create the text fields, and add them to our layout
  47.  
  48.         inputField = new TextField(100);
  49.         outputArea = new TextArea(10, 100);
  50.         outputArea.setEditable(false);  // output only
  51.         outputArea.setBackground(Color.yellow);
  52.  
  53.         setLayout(new BorderLayout());
  54.  
  55.         add("Center", outputArea);
  56.         add("South", inputField);
  57.  
  58.         // create the TextSender and TextReceiver objects
  59.  
  60.         try { textSender = new TextSender(hostname, port); }
  61.         catch (Exception ex) {
  62.             addLine("Could not create text sender!");
  63.             addLine("Reason: " + ex);
  64.             return;
  65.         }
  66.  
  67.         try { textReceiver = new TextReceiver(); }
  68.         catch (Exception ex) {
  69.             addLine("Could not create text receiver!  ");
  70.             addLine("Reason: " + ex);
  71.             return;
  72.         }
  73.  
  74.         // generate and set a random 32-bit text id
  75.  
  76.         textSender.setTextId((new Random()).nextInt());
  77.  
  78.         // start a thread to watch for incoming text
  79.  
  80.         new ReceiveText(textReceiver, this);
  81.  
  82.     }
  83.  
  84.     public void start() {
  85.         MultiUserClient muClient = null;
  86.         while (muClient == null)
  87.             muClient = (MultiUserClient) getAppletContext().getApplet("controlPanel");
  88.         while (world == null)
  89.             world = muClient.getWorld();
  90.         world.setTextPort(textReceiver.getPort());
  91.         LocalEntity avatar = null;
  92.         while (avatar == null)
  93.             avatar = muClient.getAvatar();
  94.         avatar.setTextId(textSender.getTextId());
  95.         try { avatar.updateRegistry(); }
  96.         catch (IOException ex) {
  97.             System.out.println("couldn't set text id for my avatar: " + ex);
  98.         }
  99.         catch (PermissionDeniedException ex) {
  100.             System.out.println("couldn't set text id for my avatar: " + ex);
  101.         }
  102.     }
  103.  
  104.     void addLine(String s) {   // add a line to the outputArea
  105.         outputArea.appendText(s + "\n");
  106.     }
  107.  
  108.     public boolean action(Event evt, Object arg) {
  109.         if (evt.target instanceof TextField) {
  110.             if (evt.target == inputField) {
  111.                 try { textSender.sendText((String) arg); }
  112.                 catch (IOException ex) {
  113.                     addLine("Error while sending text: " + ex);
  114.                 }
  115.                 inputField.setText("");  // clear the input field
  116.             }
  117.             return true;
  118.         }
  119.         return false;
  120.     }
  121.  
  122. }
  123.  
  124. class ReceiveText extends Thread {
  125.  
  126.     protected TextReceiver receiver;
  127.     protected TextClient client;
  128.  
  129.     public ReceiveText(TextReceiver rcvr, TextClient cli) {
  130.         receiver = rcvr;
  131.         client = cli;
  132.         start();
  133.     }
  134.  
  135.     public void run() {
  136.         TextMessage msg;
  137.         while (true) {
  138.             try { msg = receiver.getMessage(); }
  139.             catch (IOException ex) {
  140.                 client.addLine("Error while receiving message: " + ex);
  141.                 continue;
  142.             }
  143.             int textid = msg.getTextId();
  144.             String name = null;
  145.             if (client.world != null) {
  146.                 Entity ent = client.world.getEntityByTextId(textid);
  147.                 if (ent != null) 
  148.                     name = ent.getNickName();
  149.                 else if (ent.isMuted())  // ignore muted entities
  150.                     continue;
  151.             }
  152.             if (name == null)
  153.                 name = "entity #" + textid; 
  154.             client.addLine(name + ": " + msg.getText());
  155.         }
  156.     }
  157.  
  158. }
  159.  
  160.