import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.*;
import java.util.*;
class clientThread extends Thread {
chessClient chessclient;
clientThread(chessClient chessclient) {
this.chessclient = chessclient;
}
public void acceptMessage(String recMessage) {
if (recMessage.startsWith("/userlist ")) {
StringTokenizer userToken = new StringTokenizer(recMessage, " ");
int userNumber = 0;
chessclient.veAll();
chessclient.veAll();
chessclient.inputpad.userChoice.addItem("所有人");
while (userToken.hasMoreTokens()) {
String user = (String) Token(" ");
if (userNumber > 0 && !user.startsWith("[inchess]")) {
chessclient.userpad.userList.add(user);
chessclient.inputpad.userChoice.addItem(user);
}
userNumber++;
}
chessclient.inputpad.userChoice.select("所有人");
} else if (recMessage.startsWith("/yourname ")) {
chessclient.chessClientName = recMessage.substring(10);
chessclient.setTitle("Java五子棋客户端 " + "用户名:"
+ chessclient.chessClientName);
} else if (recMessage.equals("/reject")) {
try {
chessclient.chesspad.statusText.setText("不能加入游戏");
lpad.cancelGameButton.setEnabled(false);
lpad.joinGameButton.setEnabled(true);
atGameButton.setEnabled(true);
} catch (Exception ef) {
chessclient.chatpad.chatLineArea
.setText("chessclient.chesspad.chessSocket.close无法关闭");
}
lpad.joinGameButton.setEnabled(true);
} else if (recMessage.startsWith("/peer ")) {
chessclient.chesspad.chessPeerName = recMessage.substring(6);
if (chessclient.isServer) {
chessclient.chesspad.chessColor = 1;
chessclient.chesspad.isMouseEnabled = true;
chessclient.chesspad.statusText.setText("请黑棋下子");
} else if (chessclient.isClient) {
chessclient.chesspad.chessColor = -1;
chessclient.chesspad.statusText.setText("已加入游戏,等待对方下子...");
}
} else if (recMessage.equals("/youwin")) {
chessclient.isOnChess = false;
chessclient.chesspad.chessVictory(chessclient.chesspad.chessColor);
chessclient.chesspad.statusText.setText("对方退出,请点放弃游戏退出连接");
chessclient.chesspad.isMouseEnabled = false;
} else if (recMessage.equals("/OK")) {
chessclient.chesspad.statusText.setText("创建游戏成功,等待别人加入...");
} else if (recMessage.equals("/error")) {
chessclient.chatpad.chatLineArea.append("传输错误:请退出程序,重新加入 \n");
} else {
chessclient.chatpad.chatLineArea.append(recMessage + "\n");
chessclient.chatpad.chatLineArea
.setCaretPosition(chessclient.chatpad.chatLineArea
.getText().length());
}
}
public void run() {
String message = "";
try {
while (true) {
message = adUTF();
acceptMessage(message);
}
} catch (IOException es) {
}
}
}
public class chessClient extends Frame implements ActionListener, KeyListener {
userPad userpad = new userPad();
chatPad chatpad = new chatPad();
controlPad controlpad = new controlPad();
chessPad chesspad = new chessPad();
inputPad inputpad = new inputPad();
Socket chatSocket;
DataInputStream in;
DataOutputStream out;
String chessClientName = null;
String host = null;
int port = 4331;
boolean isOnChat = false; // 在聊天?
boolean isOnChess = false; // 在下棋?
boolean isGameConnected = false; // 下棋的客户端连接?
boolean isServer = false; // 如果是下棋的主机
boolean isClient = false; // 如果是下棋的客户端
Panel southPanel = new Panel();
Panel northPanel = new Panel();
Panel centerPanel = new Panel();
Panel westPanel = new Panel();
简单的java游戏代码 Panel eastPanel = new Panel();
chessClient() {
super("Java五子棋客户端");
setLayout(new BorderLayout());
host = Text();
westPanel.setLayout(new BorderLayout());
westPanel.add(userpad, BorderLayout.NORTH);
westPanel.add(chatpad, BorderLayout.CENTER);
westPanel.setBackground(Color.pink);
inputpad.inputWords.addKeyListener(this);
chesspad.host = Text();
centerPanel.add(chesspad, BorderLayout.CENTER);
centerPanel.add(inputpad, BorderLayout.SOUTH);
centerPanel.setBackground(Color.pink);
tButton.addActionListener(this);
atGameButton.addActionListener(this);
controlpad.joinGameButton.addActionListener(this);
controlpad.cancelGameButton.addActionListener(this);
itGameButton.addActionListener(this);
atGameButton.setEnabled(false);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论