?? chatapplet.java
字號:
package at.ac.uni_linz.tk.vchat;
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.*;
import symantec.itools.awt.*;
/**
* Provides the Chat's main client functionality, it administrates users and takes
* control over UserEvents. The user interface shown in the applet area is the
* LoginPanel only, the ChatPanel is part of an own Frame (ChatFrame).
*
* @author Arno Huetter
* (C)opyright by the Institute for Computer Science, Telecooperation Department, University of Linz
*/
public class ChatApplet extends Applet implements ChatRepository, Runnable {
private LoginPanel loginPanel;
private ChatFrame chatFrame;
private ChatClient chatClient;
private Image[] moodIcon, defaultAvatar;
private Image emptyIcon, unknownIcon, defaultBackAvatar, imgSlider;
private IntegerHashtable userTable, roomTable, userRoomTable;
private int currentUserId;
private Date histDate, chatStartDate;
private boolean historyMode, chatRunning;
private String status;
private Simulator simulator;
private HistoryPanel pnlHistory;
private Hashtable hashHistory;
private Thread thrMoodTimeout;
private ImageCanvas logoCanvas;
private MediaTracker tracker;
public static String SERVER_POTRAIT_FILENAME[][] = { { "smiley6.gif", "smiley0.gif", "smiley1.gif", "smiley2.gif", "smiley3.gif", "smiley4.gif", "smiley5.gif" },
{ "man1.gif", "man1.gif", "man1.gif", "man1.gif", "man1.gif", "man1.gif", "man1.gif" },
{ "man2.gif", "man2.gif", "man2.gif", "man2.gif", "man2.gif", "man2.gif", "man2.gif" },
{ "woman1.gif", "woman1.gif", "woman1.gif", "woman1.gif", "woman1.gif", "woman1.gif", "woman1.gif" },
{ "woman2.gif", "woman2.gif", "woman2.gif", "woman2.gif", "woman2.gif", "woman2.gif", "woman2.gif" },
{ "baghead.gif", "baghead.gif", "baghead.gif", "baghead.gif", "baghead.gif", "baghead.gif", "baghead.gif" },
{ "teddy.gif", "teddy.gif", "teddy.gif", "teddy.gif", "teddy.gif", "teddy.gif", "teddy.gif" },
{ "moonmask.gif", "moonmask.gif", "moonmask.gif", "moonmask.gif", "moonmask.gif", "moonmask.gif", "moonmask.gif" },
{ "devilmask.gif", "devilmask.gif", "devilmask.gif", "devilmask.gif", "devilmask.gif", "devilmask.gif", "devilmask.gif" },
{ "darkmask.gif", "darkmask.gif", "darkmask.gif", "darkmask.gif", "darkmask.gif", "darkmask.gif", "darkmask.gif" }
};
public static String SERVER_POTRAIT_NAME[] = { "Smiley", "Man 1", "Man 2", "Woman 1", "Woman 2", "Baghead", "Teddy", "Moon Mask", "Devil Mask", "Dark Mask" };
private class AvatarParam {
public String name;
public String[] image = new String[ChatRepository.PREDEFINED_NR_OF_MOODS + 1];
}
protected boolean getBooleanParam(String name, boolean defaultVal) {
return getParameter(name) == null ? defaultVal : new Boolean(getParameter(name)).booleanValue();
}
/**
* Initializes the ChatApplet.
*/
public void init() {
String param;
Vector avatars = new Vector();
for (int i = 0; (param = getParameter("PredefinedAvatar[" + i + "]")) != null; i++) {
StringTokenizer tok = new StringTokenizer(param, ":");
if (tok.countTokens() == 2) {
AvatarParam avatar = new AvatarParam();
avatar.name = tok.nextToken();
StringTokenizer tokAv = new StringTokenizer(tok.nextToken(), ",");
for (int j = 0; j <= ChatRepository.PREDEFINED_NR_OF_MOODS; j++) {
avatar.image[j] = tokAv.hasMoreTokens() ? tokAv.nextToken() : ( j > 1 ? avatar.image[1] : new String(""));
}
avatars.addElement(avatar);
}
}
if (avatars.size() > 0) {
SERVER_POTRAIT_NAME = new String[avatars.size()];
SERVER_POTRAIT_FILENAME = new String[avatars.size()][ChatRepository.PREDEFINED_NR_OF_MOODS + 1];
for (int i = 0; i < avatars.size(); i++) {
SERVER_POTRAIT_NAME[i] = ((AvatarParam)avatars.elementAt(i)).name;
for (int j = 0; j <= ChatRepository.PREDEFINED_NR_OF_MOODS; j++) {
SERVER_POTRAIT_FILENAME[i][j] = ((AvatarParam)avatars.elementAt(i)).image[j];
}
}
}
logoCanvas = new ImageCanvas(getImage(LOGO_FILENAME, true), LOGO_DIMENSION);
logoCanvas.showDocumentOnClick(this, LOGO_URL);
moodIcon = new Image[PREDEFINED_NR_OF_MOODS];
defaultAvatar = new Image[PREDEFINED_NR_OF_MOODS];
defaultBackAvatar = getImage(SERVER_POTRAIT_FILENAME[0][0]);
for (int i = 0; i < PREDEFINED_NR_OF_MOODS; i++) {
moodIcon[i] = getImage(MOOD_ICON_NAME[i]);
defaultAvatar[i] = getImage(SERVER_POTRAIT_FILENAME[0][i + 1]);
}
emptyIcon = getImage(EMPTY_ICON_NAME);
unknownIcon = getImage(UNKNOWN_ICON_NAME);
setBackground(CONTAINER_BACKGROUND);
imgSlider = getImage(IMAGE_SLIDER, true);
setLayout(new GridLayout());
loginPanel = new LoginPanel(this, getParameter("LoginMode") == null ? LoginPanel.MODE_DEFAULT : ChatUtil.getInt(getParameter("LoginMode")));
add(loginPanel);
chatClient = new ChatClient(getHost(), this);
startChat(getBooleanParam("DemoMode", true));
}
/**
* Called by the browser or applet viewer to inform this applet that it is
* being reclaimed.
*/
public void destroy() {
/*
* Stop the chat on Netscape browsers
*/
if (System.getProperty("java.vendor").indexOf("Netscape") != -1)
stopChat();
}
/**
* Called by the browser or applet viewer to inform this applet that it should
* stop its execution.
*/
public void stop() {
/*
* Netscape is buggy on the stop-method() - it might also be called when
* resizing the browser window
*/
if (System.getProperty("java.vendor").indexOf("Netscape") == -1)
stopChat();
}
public void setFrameVisibility(boolean visible) {
chatFrame.setVisible(visible);
}
/**
* Starts the chat. Initializes variables, opens the ChatFrame and runs the
* simulator.
*/
public void startChat(boolean demoMode) {
User robot[];
Random random;
Room room;
userTable = new IntegerHashtable();
roomTable = new IntegerHashtable();
hashHistory = new Hashtable();
chatStartDate = new Date();
for (int i = 0; i < STANDARD_ROOM_NAME.length; i++) {
room = new Room(i, STANDARD_ROOM_NAME[i], ChatRepository.ROOM_DIMENSION);
room.setPrivate(STANDARD_ROOM_PRIVATE[i]);
room.setDemo(i != 0);
if (STANDARD_ROOM_INVITED[i]) {
room.inviteUser("Guest");
}
addRoom(room);
}
/*
* Init the demo-version's Users
*/
User user = new User(UNKNOWN_USER_ID, "Guest", "", Color.red, new Point (ROOM_DIMENSION.width / 2, ROOM_DIMENSION.height / 2), 0, 0, User.HUMAN_RACE);
user.setBackAvatar(defaultBackAvatar);
for (int i = 0; i < PREDEFINED_NR_OF_MOODS; i++)
user.setAvatar(i, defaultAvatar[i]);
setCurrentUser(user);
if (demoMode) {
/*
* Start the simulator and add the Users
*/
simulator = new Simulator(this);
simulator.start();
random = new Random();
robot = new User[ROBOT_NAME.length];
for (int i = 0; i < ROBOT_NAME.length; i++) {
addUser(robot[i] = new User(ROBOT_USER_ID + i, ROBOT_NAME[i], "", new Color((int)(random.nextDouble() * MAX_COLOR_VALUE), (int)(random.nextDouble() * MAX_COLOR_VALUE), (int)(random.nextDouble() * MAX_COLOR_VALUE)), new Point ((int)(random.nextDouble() * 150), (int)(random.nextDouble() * 150)), (int)(random.nextDouble() * 360), 0, User.ROBOT_RACE));
robot[i].setAvatar(0, getImage(ROBOT_PORTRAIT_FILENAME[i]));
robot[i].setCommercialBanner(ROBOT_COMMERCIAL_BANNER[i]);
simulator.addUser(robot[i]);
}
}
chatFrame = new ChatFrame(this);
chatFrame.setSize(800, 620);
chatFrame.setVisible(demoMode);
historyMode = false;
setStatus("", true);
chatRunning = true;
}
/**
* Closes a possible server-connection, stops the simulator and closes the
* ChatFrame.
*/
public void stopChat() {
chatRunning = false;
chatClient.disconnect();
if (simulator != null && simulator.isAlive())
simulator.stop();
if (chatFrame != null) {
chatFrame.setVisible(false);
// chatFrame = null;
}
}
/**
* Returns true in case the ChatClient is running.
*/
public boolean chatRunning() {
return chatRunning;
}
/**
* Fills up the RoomListPanel's XList with the Rooms' data.
*/
public void fillRoomList() {
if (chatFrame != null)
chatFrame.fillRoomList();
}
/**
* Repaints the whole ChatFrame.
*/
public void repaintAll() {
if (chatFrame != null)
chatFrame.repaintAll();
}
/**
* Repaints the ChatFrame's ViewCanvas.
*/
public void repaintView() {
if (chatFrame != null)
chatFrame.repaintView();
}
/**
* Updates RoomListPanel's XList with the Rooms' data.
*/
public void updateRoomList() {
if (chatFrame != null)
chatFrame.updateRoomList();
}
/**
* Repaints the ChatFrame's RoomPanel.
*/
public void repaintRoom() {
if (chatFrame != null)
chatFrame.repaintRoom();
}
/**
* Repaints the ChatFrame's MoodCanvas.
*/
public void repaintMood() {
if (chatFrame != null)
chatFrame.repaintMood();
}
/**
* Repaints the current User.
*/
public void repaintUser() {
}
/**
* Adds a new User.
*
* @param userParam the User to be added
*/
public synchronized void addUser(User userParam) {
userTable.put(userParam.getId(), userParam);
moveUserToRoom(userParam.getId(), userParam.getRoom(), false);
if (userParam.getRoom() == getCurrentRoomId()) {
repaintRoom();
if (userParam.getId() == getCurrentUserId() || inVisualRange(getCurrentUserId(), userParam.getId()))
repaintView();
}
}
/**
* Adds a new Room.
*
* @param roomParam the Room to be added
*/
public synchronized void addRoom(Room roomParam) {
roomTable.put(roomParam.getId(), roomParam);
repaintAll();
fillRoomList();
}
/**
* Removes a User.
*
* @param idParam the id of the User to be removed
*/
public synchronized void removeUser(int idParam) {
int roomId;
if (idParam != getCurrentUserId() && userTable.containsKey(idParam)) {
roomId = getUser(idParam).getRoom();
getRoom(roomId).removeUser(getUser(idParam).getName());
userTable.remove(idParam);
updateRoomList();
if (roomId == getCurrentRoomId()) {
repaintRoom();
repaintView();
}
}
}
/**
* Removes a Room.
*
* @param idParam the Id of the Room to be removed
*/
public synchronized void removeRoom(int roomIdParam) {
if (getRoom(roomIdParam) != null && getRoom(roomIdParam).getNrOfUsers() == 0) {
roomTable.remove(roomIdParam);
repaintAll();
fillRoomList();
}
}
/**
* Removes all but the current User from the chat system. Needed when connecting/
* disconnecting to/from the chat server.
*/
public synchronized void removeAllExceptCurrentUser() {
User user;
Enumeration roomIds;
user = getCurrentUser();
userTable = new IntegerHashtable();
roomIds = getRoomIds();
while (roomIds.hasMoreElements())
getRoom(((Integer)roomIds.nextElement()).intValue()).removeAllUsers();
addUser(user);
repaintRoom();
repaintView();
}
/**
* Removes User with "unknown" id (demo User).
*/
public synchronized void removeUnknownUser() {
if (getCurrentUserId() != UNKNOWN_USER_ID)
removeUser(UNKNOWN_USER_ID);
}
/**
* Stops the simulator. Called when connecting to the chat server.
*/
public void stopSimulator() {
for (int i = 0; i < ROBOT_NAME.length; i++) {
if (getUser(ROBOT_USER_ID + i) != null)
removeUser(ROBOT_USER_ID + i);
}
}
/**
* Removes all ROoms except the default Room.
*/
public synchronized void removeAllExceptDefaultRoom() {
Enumeration roomEnum;
Room room;
roomEnum = roomTable.elements();
while (roomEnum.hasMoreElements()) {
room = (Room)roomEnum.nextElement();
if (room.getId() != 0)
forceRoomRemoval(room.getId(), false);
}
}
/**
* Returns the User with a certain id.
*
* @param idParam the User's id
*/
public synchronized User getUser(int idParam) {
Object object;
object = userTable.get(idParam);
if (object != null)
return (User)object;
else
return null;
}
/**
* Sets the current User by his id. This User has to exist already.
*
* @param idParam the User's id
*/
public synchronized void setCurrentUserId(int idParam) {
if (userTable.containsKey(idParam)) {
currentUserId = idParam;
moveUserToRoom(idParam, getUser(idParam).getRoom(), false);
repaintView();
repaintRoom();
repaintMood();
repaintCurrentUser();
}
}
/**
* Returns the current User's id.
*/
public synchronized int getCurrentUserId() {
return currentUserId;
}
/**
* Returns the current User.
*/
public synchronized User getCurrentUser() {
return getUser(currentUserId);
}
/**
* Sets the current User.
*
* @param userParam the User to be the new current User.
*/
public synchronized void setCurrentUser(User userParam) {
addUser(userParam);
setCurrentUserId(userParam.getId());
}
/**
* Returns all Users as an IntegerHashtable.
*/
public synchronized IntegerHashtable getAllUsers() {
return userTable;
}
/**
* Sets the position of a certain User.
*
* @param idParam the User's id
* @param position the new position (refer's to the room's dimension)
* @param send determines wheter the new position should be broadcasted
*/
public synchronized void setUserPosition(int idParam, Point positionParam, boolean send) {
Enumeration userIdEnum;
User userParam, user;
Room currentRoom;
boolean collides, wasInSight;
if ((currentRoom = getCurrentRoom()) != null) {
positionParam = new Point(Math.max(Math.min(positionParam.x, currentRoom.getSize().width - 2), 1), Math.max(Math.min(positionParam.y, currentRoom.getSize().height - 2), 1));
userParam = getUser(idParam);
collides = false;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -