import java.net.*;
import java.io.*;
import java.awt.*;
import java.util.*;
import java.applet.*;
/*
A basic extension of the java.applet.Applet class
*/
//class 7:ChatApplet,电子白板客户端Applet核心类
public class ChatApplet extends Applet
implements Runnable {
volatile boolean logged=false;
volatile boolean belongRoom=false;
volatile boolean paused=false;
volatile boolean roomDataValid=false;
URL url=null;
int port=0;
Socket s=null;
Color currentColor=null;
int x0=0,y0=0,x1=0,y1=0;
String userName=null;
String roomName=null;
int roomID=0;
Rooms rooms=null;
Thread thread=null;
DataInputStream io_in=null;
DataOutputStream io_out=null;
DataOutputStream piped_out=null;
SendDrawData sender=null;
int logo_x=0,logo_y=0,logo_width=0,logo_height=0;
public void start() {
if (thread==null) {
thread=new Thread(this);
thread.start();
}
}
// method 'start' ended
public void stop() {
if (thread!=null) {
quitButton_Clicked(new Event(this,0,null));
message_TextArea.setText("");
thread.stop();
thread=null;
}
}
//method 'stop' ended
public void destroy() {
try{
commandCenter("ask_quit",new DataBag
("none","none",0,0,0,0,0));
if (sender!=null) {
sender.quit();
sender=null;
}
socketClose();
}catch(Exception e){
System.out.println(e);
}
}
//method 'destroy' close
public void socketClose() {
try{
piped_out.flush();
piped_out.close();
piped_out=null;
}catch(IOException e){
System.out.println(e);
}
try{
io_in.close();
io_in=null;
}catch(Exception e){}
try{
io_out.flush();
io_out.close();
io_out=null;
}catch(Exception e){}
try{
s.close();
s=null;
}catch(Exception e){}
}
//method 'socketClose' ended
public void commandCenter(String
command,DataBag data) throws PleaseCloseSocketException {
if (command.equals("ask_refresh")) client_ask_refresh();
else if (command.equals("refresh")) client_answer_refresh();
else if (command.equals("ask_log")) client_ask_log();
else if (command.equals("ask_join")) client_ask_join();
else if (command.equals("ask_pause")) client_ask_pause();
else if (command.equals("ask_continue")) client_ask_continue();
else if (command.equals("ask_new")) client_ask_newRoom();
else if (command.equals("ask_quit")) client_ask_quit();
else if (command.equals("ask_text")) client_ask_text(data);
else if (command.equals("text")) client_answer_text();
else if (command.equals("draw")) client_answer_draw();
}// method commandCenter ended
private void client_answer_text() throws PleaseCloseSocketException{
String message=null;
String name=null;
DataBag data=null;
try{
name=io_in.readUTF();
message=io_in.readUTF();
data=new DataBag(name,message,0,0,0,0,0);
printChat_Area(data);
}catch(IOException e){
message_TextArea.appendText("抱歉,无法正确接受句子");
throw new PleaseCloseSocketException();
}
}//method 'client_answer_text' ended
private void client_answer_draw() throws PleaseCloseSocketException {
DataBag data=null;
int color=0,x0=0,y0=0,x1=0,y1=0;
try{
color=io_in.readInt();
x0=io_in.readShort();
y0=io_in.readShort();
x1=io_in.readShort();
y1=io_in.readShort();
data=new DataBag("none","none",color,x0,y0,x1,y1);
drawBoard_Canvas(data);
}catch(IOException e){
message_TextArea.appendText("抱歉,无法正确接受图形");
throw new PleaseCloseSocketException();
}
}//mwthod 'client_answer_draw' ended
private void client_ask_text(DataBag data)
throws PleaseCloseSocketException {
String message=null;
try{
synchronized(sender){
io_out.writeUTF("text");
io_out.writeUTF(data.message);
}
}catch(IOException e){
throw new PleaseCloseSocketException();
}
}// method 'client_ask_text' ended
private void client_ask_refresh() throws PleaseCloseSocketException {
try{
if (sender!=null) {
synchronized(sender){
io_out.writeUTF("refresh");
}
}else{
io_out.writeUTF("refresh");
}
}catch(IOException e){
roomDataValid=false;
throw new PleaseCloseSocketException();
}
}// method 'client_ask_refresh' ended
private void client_answer_refresh()
throws PleaseCloseSocketException {
String message=null;
RoomData roomData=null;
int roomID=0;
rooms=new Rooms(10,5);
try{
while(!((message=io_in.readUTF()).equals("ok"))) {
roomData=new RoomData(message);
rooms.addElement(roomData);
while(!(message=io_in.readUTF()).equals("complete")) {
roomData.addUser(message);
}
}
message=io_in.readUTF();
rooms.setDefault(message);
roomDataValid=true;
refreshRoomList();
}catch(IOException e){
roomDataValid=false;
message_TextArea.appendText("\n抱歉,服务器无法请求刷新数据");
throw new PleaseCloseSocketException();
}
}// method 'client_answer_refresh' ended
private void client_ask_log() throws PleaseCloseSocketException {
String message=null;
try{
synchronized(sender){
io_out.writeUTF("log");
io_out.writeUTF(userName);
}
}catch(IOException e){
throw new PleaseCloseSocketException();
}
}// method 'client_ask_log' ended
private void client_ask_join() throws PleaseCloseSocketException {
String message=null;
int ID=0;
String subject=null;
int index=0;
try{
subject=room_Choice.getSelectedItem();
synchronized(sender){
io_out.writeUTF("join");
io_out.writeUTF(subject);
}
roomName=subject;
}catch(IOException e){
throw new PleaseCloseSocketException();
}
}// method 'client_ask_join' ended
private void client_ask_pause() throws PleaseCloseSocketException {
String message=null;
try{
synchronized(sender){
io_out.writeUTF("pause");
}
}catch(IOException e){
throw new PleaseCloseSocketException();
}
}// method 'client_ask_pause' ended
private void client_ask_continue() throws PleaseCloseSocketException {
String message=null;
try{
synchronized(sender){
io_out.writeUTF("continue");
}
}catch(IOException e){
throw new PleaseCloseSocketException();
}
}// method 'client_ask_continue' ended
private void client_ask_newRoom() throws PleaseCloseSocketException {
String message=null;
String subject=null;
int roomID=0;
RoomData roomData=null;
try{
synchronized(sender){
io_out.writeUTF("new");
subject=subject_TextField.getText();
io_out.writeUTF(subject);
}
this.roomName=subject;
}catch(IOException e){
throw new PleaseCloseSocketException();
}
}// method 'client_ask_new' ended
private void client_ask_quit() throws PleaseCloseSocketException {
String message=null;
int index=0;
try{
synchronized(sender){
io_out.writeUTF("quit");
}
if (sender!=null) {
sender.quit();
sender=null;
}
}catch(IOException e){
throw new PleaseCloseSocketException();
}
}// method 'client_ask_quit' ended
public void openSocket() throws IOException {
s=new Socket(url.getHost(),port);
io_in=new DataInputStream(s.getInputStream());
io_out=new DataOutputStream(s.getOutputStream());
}
// method openSocket ended
public void run() {
String message=null;
while(true) {
try{
if (s!=null) {
commandCenter("ask_refresh",
new DataBag("none","none",0,0,0,0,0));
}
while(logged){
message=io_in.readUTF();
commandCenter(message,new DataBag("none","none",0,0,0,0,0));
}
synchronized (this) {
try{
wait();
}catch(Exception w){
System.out.println(w);
}
}
}catch(PleaseCloseSocketException e) {
}catch(IOException e) {}
}
}// method 'run' ended
没有合适的资源?快使用搜索试试~ 我知道了~
Java聊天程序,电子白板源代码二
共23个文件
class:16个
java:4个
htm:1个
4星 · 超过85%的资源 需积分: 12 46 下载量 143 浏览量
2009-11-30
19:21:56
上传
评论
收藏 85KB ZIP 举报
温馨提示
以前发了一个,现在再发一个我同学写的,也很好的
资源推荐
资源详情
资源评论
收起资源包目录
73462689电子白板代码(JAVA).zip (23个子文件)
电子白板代码(JAVA)
使用说明.CHM 49KB
www.cnzz.cn
Room.class 1KB
Logo.class 711B
ChatServer.class 6KB
AboutDialog.java 2KB
NotMemberException.class 205B
User.class 8KB
NoSuchRoomException.class 206B
ChatApplet.class 21KB
ChatApplet.java 29KB
SendDrawData.class 1KB
RoomData.class 1KB
RoomGroup.class 3KB
QuitDialog.class 2KB
Rooms.class 997B
QuitDialog.java 2KB
AboutDialog.class 2KB
index.htm 185B
DataBag.class 552B
TooManyRoomsException.class 208B
ChatServer.java 24KB
PleaseCloseSocketException.class 213B
www.pudn.com.txt 218B
共 23 条
- 1
wangshiliang01
- 粉丝: 4
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页