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