home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Late Night VRML 2.0 with Java CD-ROM
/
code.zip
/
Ch18
/
TextClient.java
< prev
next >
Wrap
Text File
|
1997-02-21
|
5KB
|
160 lines
// The Text Client applet
// Written by Bernie Roehl, January 1997
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.util.*;
import multi.*;
public class TextClient extends Applet {
public static final String version = "0.1";
TextField inputField;
TextArea outputArea;
TextReceiver textReceiver;
TextSender textSender;
World world = null; // reference to the world in the multi-user client
public String getAppletInfo() {
return "Multi-User Chat Text Client" + version + ", written by Bernie Roehl";
}
public String[][] getParameterInfo() {
String[][] info = {
{ "host", "string", "name or IP address of filtering host" },
{ "port", "integer", "IP port to which to send text" }
};
return info;
}
public void init() {
// read applet parameters
String hostname = getParameter("host");
int port = Integer.parseInt(getParameter("port"));
if (hostname == null)
hostname = getDocumentBase().getHost();
// create the text fields, and add them to our layout
inputField = new TextField(100);
outputArea = new TextArea(10, 100);
outputArea.setEditable(false); // output only
outputArea.setBackground(Color.yellow);
setLayout(new BorderLayout());
add("Center", outputArea);
add("South", inputField);
// create the TextSender and TextReceiver objects
try { textSender = new TextSender(hostname, port); }
catch (Exception ex) {
addLine("Could not create text sender!");
addLine("Reason: " + ex);
return;
}
try { textReceiver = new TextReceiver(); }
catch (Exception ex) {
addLine("Could not create text receiver! ");
addLine("Reason: " + ex);
return;
}
// generate and set a random 32-bit text id
textSender.setTextId((new Random()).nextInt());
// start a thread to watch for incoming text
new ReceiveText(textReceiver, this);
}
public void start() {
MultiUserClient muClient = null;
while (muClient == null)
muClient = (MultiUserClient) getAppletContext().getApplet("controlPanel");
while (world == null)
world = muClient.getWorld();
world.setTextPort(textReceiver.getPort());
LocalEntity avatar = null;
while (avatar == null)
avatar = muClient.getAvatar();
avatar.setTextId(textSender.getTextId());
try { avatar.updateRegistry(); }
catch (IOException ex) {
System.out.println("couldn't set text id for my avatar: " + ex);
}
catch (PermissionDeniedException ex) {
System.out.println("couldn't set text id for my avatar: " + ex);
}
}
void addLine(String s) { // add a line to the outputArea
outputArea.appendText(s + "\n");
}
public boolean action(Event evt, Object arg) {
if (evt.target instanceof TextField) {
if (evt.target == inputField) {
try { textSender.sendText((String) arg); }
catch (IOException ex) {
addLine("Error while sending text: " + ex);
}
inputField.setText(""); // clear the input field
}
return true;
}
return false;
}
}
class ReceiveText extends Thread {
protected TextReceiver receiver;
protected TextClient client;
public ReceiveText(TextReceiver rcvr, TextClient cli) {
receiver = rcvr;
client = cli;
start();
}
public void run() {
TextMessage msg;
while (true) {
try { msg = receiver.getMessage(); }
catch (IOException ex) {
client.addLine("Error while receiving message: " + ex);
continue;
}
int textid = msg.getTextId();
String name = null;
if (client.world != null) {
Entity ent = client.world.getEntityByTextId(textid);
if (ent != null)
name = ent.getNickName();
else if (ent.isMuted()) // ignore muted entities
continue;
}
if (name == null)
name = "entity #" + textid;
client.addLine(name + ": " + msg.getText());
}
}
}