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)源码
共21个文件
class:16个
java:4个
htm:1个
0 下载量 183 浏览量
2023-07-02
16:34:18
上传
评论
收藏 40KB RAR 举报
温馨提示
工 作 原 理 ---- 电 子 白 板 有 两 种 实 现 模 型, 一 种 是 无 白 板 服 务 器, 因 此 仅 支 持 两 个 用 户 直 接 连 结; 另 一 种 是 有 白 板 服 务 器, 原 则 上 不 限 制 同 时 上 线 人 数 和 交 谈 室 个 数, 具 体 实 现 上 可 视 服 务 器 性 能 和 需 要 而 定。 本 文 要 介 绍 属 于 后 者。 ---- 当 用 户 需 用 白 板 与 他 人 交 流 时, 需 要 先 用 浏 览 器 连 到Applet 所 在 主 页,Applet 运 行 后 会 连 接 到 该 白 板 服 务 器, 和 服 务 器 建 立TCP 连 接。 每 个 用 户 可 以 在 自 己 的 白 板 上 ( 嵌 在Applet 画 面 中) 绘 制 图 形 和 输 入 文 字,Applet 会 将 这 些 信 息 通 过 已 建 立 的 网 络 连 接 发 往 白 板 服 务 器, 并 不 断 侦 听、 接 收 来 自 白 板 服 务 器 的 图 形 和 文 字 信 息, 将 其 再 现 在 用 户 的 白 板 中。 白
资源推荐
资源详情
资源评论
收起资源包目录
javabaiban.rar (21个子文件)
javabaiban
PleaseCloseSocketException.class 213B
AboutDialog.java 2KB
NotMemberException.class 205B
ChatApplet.class 21KB
Logo.class 711B
User.class 8KB
SendDrawData.class 1KB
index.htm 185B
ChatApplet.java 29KB
ChatServer.class 6KB
TooManyRoomsException.class 208B
ChatServer.java 24KB
Rooms.class 997B
DataBag.class 552B
Room.class 1KB
NoSuchRoomException.class 206B
RoomData.class 1KB
RoomGroup.class 3KB
QuitDialog.class 2KB
AboutDialog.class 2KB
QuitDialog.java 2KB
共 21 条
- 1
资源评论
2201_75761617
- 粉丝: 18
- 资源: 7339
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功