📄 chatapplet.java
字号:
if (userParam != null) {
/*
* Check if the new position collides with any other User. Should be done by the ChatServer,
* this is just for double security in case of inconsistant states.
*/
userIdEnum = getRoomUserIdVector(getCurrentRoomId()).elements();
while (userIdEnum.hasMoreElements() && !collides) {
user = getUser(((Integer)userIdEnum.nextElement()).intValue());
if (user != null)
collides = new Rectangle(user.getPosition().x - USER_SIZE / 2 - MINIMUM_DISTANCE, user.getPosition().y - USER_SIZE / 2 - MINIMUM_DISTANCE, USER_SIZE + MINIMUM_DISTANCE * 2, USER_SIZE + MINIMUM_DISTANCE * 2).contains(positionParam) && user != userParam;
}
if (!collides) {
wasInSight = inVisualRange(getCurrentUser().getId(), userParam.getId());
userParam.setPosition(positionParam);
if (wasInSight || inVisualRange(getCurrentUser().getId(), userParam.getId()) || userParam == getCurrentUser())
repaintView();
repaintRoom();
if (idParam != getCurrentUserId() && userParam.getRoom() == getCurrentRoomId())
generateHistoryEntry();
}
/*
* Send the position, if the User collides send the last valid position
*/
if (userParam == getCurrentUser() && send && isConnected()) {
chatClient.send(new UserPositionEvent(userParam.getId(), userParam.getPosition()));
}
}
}
}
/**
* Sets the heading of a certain User.
*
* @param idParam the User's id
* @param heading the new heading
* @param send determines wheter the new heading should be broadcasted
*/
public synchronized void setUserHeading(int idParam, int headingParam, boolean send) {
User userParam;
if ((userParam = getUser(idParam)) != null) {
userParam.setHeading(headingParam);
if (userParam == getCurrentUser() || inVisualRange(getCurrentUser().getId(), userParam.getId()))
repaintView();
repaintRoom();
if (idParam != getCurrentUserId() && userParam.getRoom() == getCurrentRoomId())
generateHistoryEntry();
if (userParam == getCurrentUser() && send && isConnected())
chatClient.send(new UserHeadingEvent(userParam.getId(), userParam.getHeading()));
}
}
/**
* Returns the distance between a User's position and a certain point.
*
* @param idParam the User
* @param position the position to calculate the distance to
*/
public int getDistance(int idParam, Point position) {
return getDistance(getUser(idParam).getPosition(), position);
}
/**
* Returns the distance between a User's position and a certain point.
*
* @param idParam the User
* @param position the position to calculate the distance to
*/
public int getDistance(Point position1, Point position2) {
return (int)Math.sqrt(Math.pow(position1.x - position2.x, 2) + Math.pow(position1.y - position2.y, 2));
}
/**
* Returns true if a position can be seen by a User.
*
* @param idParam the id of the User watching
* @param position position to be seen
*/
public boolean inVisualRange(int idParam, Point position) {
try {
return inVisualRange(getUser(idParam).getPosition(), getUser(idParam).getHeading(), position);
}
catch (Exception excpt) {
return false;
}
}
public boolean inVisualRange(Point position1, int headingParam, Point position2) {
int distance, angle;
distance = getDistance(position1, position2);
angle = getAngle(position1, position2);
return (distance <= VISUAL_RANGE && ChatUtil.inAngleRange(angle, ChatUtil.subAngle(headingParam, VISUAL_ANGLE / 2), ChatUtil.addAngle(headingParam, VISUAL_ANGLE / 2)));
}
/**
* Returns true if one User can be seen by another User.
*
* @param idParam1 the id of the User watching
* @param idParam2 the id of the User to be seen
*/
public boolean inVisualRange(int idParam1, int idParam2) {
User user1, user2;
try {
user1 = getUser(idParam1);
user2 = getUser(idParam2);
if ((user1 != user2) && (user1.getRoom() == user2.getRoom())) {
return inVisualRange(user1.getPosition(), user1.getHeading(), user2.getPosition());
}
}
catch (Exception excpt) {
}
return false;
}
/**
* Returns true one User can listen to another User (means his avatar's balloon
* is visible for the other User).
*
* @param idParam1 the User who listens
* @param idParam2 position User to be listened to
*/
public boolean inPhonicalRange(int idParam1, int idParam2) {
User user1, user2;
user1 = getUser(idParam1);
user2 = getUser(idParam2);
if ((user1 != user2) && (user1.getRoom() == user2.getRoom()))
return inPhonicalRange(idParam1, user2.getPosition());
else
return false;
}
/**
* Returns true one User can listen to another User (means his avatar's balloon
* is visible for the other User).
*
* @param idParam1 the User who listens
* @param idParam2 position User to be listened to
*/
public boolean inPhonicalRange(int idParam, Point pointParam) {
User user;
int distance, angle;
user = getUser(idParam);
distance = getDistance(idParam, pointParam);
angle = getAngle(idParam, pointParam);
return (distance <= PHONICAL_RANGE && ChatUtil.inAngleRange(angle, ChatUtil.subAngle(user.getHeading(), PHONICAL_ANGLE / 2), ChatUtil.addAngle(user.getHeading(),PHONICAL_ANGLE / 2)));
}
/**
* Determines the angle between a User and a point (< 180 degrees).
*
* @param idParam the id of the User
* @param position the point to calculate the angle within
*/
public int getAngle(int idParam, Point position) {
return ChatUtil.getAngle(getUser(idParam).getPosition(), position);
}
/**
* Determines the angle between a User and a point (< 180 degrees).
*
* @param idParam the id of the User
* @param position the point to calculate the angle within
*/
public int getAngle(Point position1, Point position2) {
return ChatUtil.getAngle(position1, position2);
}
/**
* Determines the angle between two Users (< 180 degrees).
*
* @param idParam1 the id of one User
* @param idParam2 the id of another User to calculate the angle within
*/
public int getAngle(int idParam1, int idParam2) {
return ChatUtil.getAngle(getUser(idParam1).getPosition(), getUser(idParam2).getPosition());
}
/**
* Sets the message text of a certain User.
*
* @param idParam the User's id
* @param message the new message text
* @param send determines wheter the new message should be broadcasted.
*/
public synchronized void setUserMessage(int idParam, String messageParam, boolean send) {
User user;
user = getUser(idParam);
if (user != null) {
user.setMessage(messageParam);
// Repaint in case it's the current User or the User can be seen
if (user == getCurrentUser()) {
if (send && isConnected()) {
chatClient.send(new UserMessageEvent(user.getId(), messageParam));
}
setUserMood(user.getId(), user.getMood(messageParam), send);
repaintView();
}
else if (inVisualRange(getCurrentUser().getId(), user.getId())) {
repaintView();
}
if (user.getRoom() == getCurrentRoomId()) {
if (pnlHistory != null) {
pnlHistory.addMessageEntry(new Date(), messageParam, user.getColor());
generateHistoryEntry();
}
if (chatFrame != null) {
chatFrame.addMessage(user.getName(), messageParam);
}
}
}
}
/**
* Sets the mood of a certain User.
*
* @param idParam the User's id
* @param mood the mood
* @param send determines wheter the new mood should be broadcasted.
*/
public synchronized void setUserMood(int idParam, int moodParam, boolean send) {
User user;
user = getUser(idParam);
if (user != null) {
user.setMood(moodParam);
// Repaint in case it's the current User or the User can be seen
if (user == getCurrentUser()) {
chatFrame.repaintMood();
if (send && isConnected())
chatClient.send(new UserMoodEvent(user.getId(), moodParam));
if (user.getMoodTimeout() > 0) {
if (thrMoodTimeout != null && thrMoodTimeout.isAlive())
thrMoodTimeout.stop();
thrMoodTimeout = new Thread(this);
thrMoodTimeout.start();
}
}
else if (inVisualRange(getCurrentUser().getId(), user.getId()))
repaintView();
if (user.getRoom() == getCurrentRoomId())
generateHistoryEntry();
}
}
/**
* Update's a User's data.
*
* @param user the User to be updated
* @param send determines wheter the new User's data should be broadcasted
*/
public synchronized void updateUser(User user, boolean send) {
if (send) {
chatClient.send(new UserUpdateEvent(user));
}
else {
if (!isConnected()) {
if (!user.getBackAvatarURL().equals(""))
user.setBackAvatar(getImage(user.getBackAvatarURL()));
else
user.setBackAvatar(null);
for (int i = 0; i < user.getNrOfMoods(); i++) {
if (!user.getAvatarURL(i).equals(""))
user.setAvatar(i, getImage(user.getAvatarURL(i)));
else
user.setAvatar(i, null);
}
}
addUser(user);
repaintAll();
if (user.getId() == getCurrentUser().getId() && chatFrame != null) {
chatFrame.repaintCurrentUser();
}
}
}
/**
* Update's a Room's data.
*
* @param room the Room to be updated
* @param send determines wheter the new Room's data should be broadcasted
*/
public synchronized void updateRoom(Room room, boolean send) {
if (send) {
if (room.getId() == NEW_ROOM_ID)
chatClient.send(new RoomCreateEvent(room));
else
chatClient.send(new RoomUpdateEvent(room));
}
else {
addRoom(room);
if (room.isAdministrator(getCurrentUser().getName())) {
if (chatFrame.editableRoomPanel.getRoomId() == room.getId())
chatFrame.editableRoomPanel.showRoom(room);
if (getCurrentRoomId() == NEW_ROOM_ID) {
moveUserToRoom(getCurrentUserId(), room.getId(), isConnected());
removeRoom(NEW_ROOM_ID);
chatFrame.editableRoomPanel.showRoom(room);
}
}
repaintAll();
}
}
/**
* Removes a Room.
*
* @param roomId the Id of the Room to be removed
*/
public synchronized void forceRoomRemoval(int roomId, boolean send) {
Enumeration userEnum;
Room room;
if ((room = getRoom(roomId)) != null) {
userEnum = getRoomUserIdVector(roomId).elements();
while (userEnum.hasMoreElements()) {
moveUserToRoom(((Integer)userEnum.nextElement()).intValue(), 0, send);
}
removeRoom(roomId);
if (send)
chatClient.send(new RoomRemoveEvent(roomId));
}
}
/**
* Enters the history mode. In history mode, the messages and moods of a certain
* period of time will be displayed.
*
* @param histDateParam the start date of the history mode
*/
public synchronized void enterHistoryMode(Date histDateParam) {
historyMode = true;
histDate = histDateParam;
repaintView();
repaintRoom();
}
/**
* Exists the history mode. User messages and moods will be displayed as flooding
* in.
*/
public synchronized void exitHistoryMode() {
historyMode = false;
histDate = null;
repaintView();
repaintRoom();
}
/**
* Returns true if the chat is running in history mode.
*/
public boolean historyMode() {
return historyMode;
}
/**
* Returns the start date of the history mode (in case history mode is activated).
*/
public Date getHistoryDate() {
return histDate;
}
/**
* Returns the start date of the chat, that is when the chat client started up.
*/
public Date getChatStartDate() {
return chatStartDate;
}
/**
* Shows a message in the status bar.
*
* @param statusString the message to be shown
*/
public void setStatus(String statusString) {
setStatus(statusString, false);
}
/**
* Shows a message in the status bar and if - intended - in the login panel as
* well.
*
* @param statusString the message to be shown
* @param login true if the message should be shown in the login panel
*/
public synchronized void setStatus(String statusString, boolean login) {
status = statusString;
if (chatFrame != null)
chatFrame.setStatus(statusString);
if (loginPanel != null && login)
loginPanel.setStatus(statusString);
}
/**
* Returns the message that is currently shown in the status bar.
*/
public String getStatus() {
return status;
}
/**
* Returns a User's avatar.
*
* @param userIdParam the User's id
*/
public Image getUserAvatar(int userIdParam) {
return getUserAvatar(userIdParam, getUser(userIdParam).getMood());
};
/**
* Returns a User's avatar for a certain mood.
*
* @param userIdParam the User's id
* @param moodParam the User's mood
*/
public Image getUserAvatar(int userIdParam, int moodParam) {
Image portrait;
portrait = getUser(userIdParam).getAvatar(moodParam);
if (portrait == null) {
portrait = moodParam < PREDEFINED_NR_OF_MOODS ? defaultAvatar[moodParam] : defaultAvatar[0];
}
return portrait;
}
/**
* Returns a User's back image.
*
* @param userIdParam the User's id
*/
public Image getUserBackAvatar(int userIdParam) {
return getUser(userIdParam).getBackAvatar();
}
/**
* Returns the icon for a certain mood.
*
* @param moodParam the mood
*/
public Image getMoodIcon(int moodParam) {
if (moodParam < PREDEFINED_NR_OF_MOODS) {
return moodIcon[moodParam];
}
else {
return unknownIcon;
}
}
/**
* Returns the icon for user-defined moods.
*/
public Image getUnknownIcon() {
return unknownIcon;
}
/**
* Returns the icon for back-avatar.
*/
public Image getEmptyIcon() {
return emptyIcon;
}
/**
* Returns the host where the ChatApplet descends from.
*/
public String getHost() {
return getCodeBase().getHost();
}
/**
* Returns the default port where the ChatServer is listening.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -