📄 client.java
字号:
//用来处理用户点击菜单和按钮引起的操作。
public void actionPerformed(ActionEvent ae)//
{
//播放音乐
if(ae.getSource()==play)
{
Object selection[] = {"fi.MID","five.mid"};
Object set = JOptionPane.showInputDialog(null,
"选择音乐...","播放...",
JOptionPane.QUESTION_MESSAGE,null,selection,selection[0]);
music=new Sound(set.toString());
music.play();
}
else if(ae.getSource()==helpp)
{
JOptionPane.showMessageDialog(panel, scrollHelp);
}
else if(ae.getSource()==stop)
{
music.stop();
}
else if(ae.getSource()==start)
{
try{
writer.println("[START]");//发送开始信息
infoView.setText("等待对方决定.");
startButton.setEnabled(false);
}catch(Exception e){}
}
else if(ae.getSource()==out)
{
try{
goToWaitRoom();//离开房间,进入到游戏大厅,可重新选择房间号
startButton.setEnabled(false);
stopButton.setEnabled(false);
}catch(Exception e){}
}
else if(ae.getSource()==about)
{
JOptionPane.showMessageDialog(null,
"author:软件04-8 唐建锋\nmail:tangjianfeng05@163.com",
"网络五子棋",JOptionPane.INFORMATION_MESSAGE);
}
else if(ae.getSource()==quit)
{
int flag=JOptionPane.showConfirmDialog(null,
"确定退出程序吗",
"退出",
JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if(flag==0)
{
System.exit(0);
}
}
else if(ae.getSource()==sendBox)
{
String msg=sendBox.getText();
if(msg.length()==0)return;
if(msg.length()>=30)msg=msg.substring(0,30);
try{
writer.println("[MSG]"+msg);
sendBox.setText("");
}catch(Exception ie){}
}
else if(ae.getSource()==enterButton)
{
try{
if(Integer.parseInt(roomBox.getText())<1)
{
infoView.setText("房间号错误. 大于1");
return;
}
writer.println("[ROOM]"+Integer.parseInt(roomBox.getText()));
msgView.setText("");
}catch(Exception ie)
{
infoView.setText("输入的事项发生错误.");
}
}
else if(ae.getSource()==exitButton)
{
try{
goToWaitRoom();//离开房间,进入到游戏大厅,可重新选择房间号
startButton.setEnabled(false);
stopButton.setEnabled(false);
}catch(Exception e){}
}
else if(ae.getSource()==startButton)
{
try{
writer.println("[START]");//发送开始信息
infoView.setText("等待对方决定.");
startButton.setEnabled(false);
}catch(Exception e){}
}
else if(ae.getSource()==stopButton)
{
try{
writer.println("[DROPGAME]");//向服务器发送弃权信息
endGame("已弃权.");
}catch(Exception e){}
}
}
//到游戏大厅 可以选择房间进行对战
void goToWaitRoom()
{
if(userName==null)
{
String name=nameBox.getText().trim();
//姓名在3个和9个字符之间
if(name.length()<=2 || name.length()>9){
infoView.setText("名字错误. 2~10个字");
nameBox.requestFocus();
return;
}
userName=name;
writer.println("[NAME]"+userName);
nameBox.setText(userName);
nameBox.setEditable(false);
}
msgView.setText("");
//向服务器发送[ROOM]0 表示进入大厅
writer.println("[ROOM]0");
infoView.setText("已进入游戏大厅.");
roomBox.setText("0");
enterButton.setEnabled(true);
exitButton.setEnabled(false);
}
//覆盖Runnable接口里的run ()方法,用以对游戏过程中的数据传输,处理,控制。
public void run()
{
String msg;
try
{
while((msg=reader.readLine())!=null)
{
if(msg.startsWith("[STONE]"))//接收对方下棋信息,坐标
{
String temp=msg.substring(7);
int x=Integer.parseInt(temp.substring(0,temp.indexOf(" ")));
int y=Integer.parseInt(temp.substring(temp.indexOf(" ")+1));
board.putOpponent(x, y);
board.setEnable(true);
}
else if(msg.startsWith("[ROOM]"))
{
if(!msg.equals("[ROOM]0"))
{
enterButton.setEnabled(false);
exitButton.setEnabled(true);
/*接收服务器信息,msg.substring(6)为房间号,并显示提示信息
*如[ROOM]2 2号房间
*/
infoView.setText("选择"+msg.substring(6)+"号房间");
}
else infoView.setText("已进入大厅.");
roomNumber=Integer.parseInt(msg.substring(6));
if(board.isRunning())
{
board.stopGame();
}
}
//房间已满
else if(msg.startsWith("[FULL]"))
{
infoView.setText("该房间已满");
}
//显示用户列表
else if(msg.startsWith("[PLAYERS]"))
{
nameList(msg.substring(9));
}
//进入房间
else if(msg.startsWith("[ENTER]"))
{
pList.add(msg.substring(7));
playersInfo();
msgView.append("["+ msg.substring(7)+"]入场.\n");
}
//离开房间
else if(msg.startsWith("[EXIT]"))
{
pList.remove(msg.substring(6));
playersInfo();
msgView.append("["+msg.substring(6)+"]"+"退出\n");
if(roomNumber!=0)
endGame("对方离开.");
}
//对方离开
else if(msg.startsWith("[DISCONNECT]"))
{
pList.remove(msg.substring(12));
playersInfo();
msgView.append("["+msg.substring(12)+"]中断连接.\n");
if(roomNumber!=0)
endGame("对方离开.");
}
//提示用户棋子颜色
else if(msg.startsWith("[COLOR]"))
{
String color=msg.substring(7);
board.startGame(color);
if(color.equals("BLACK"))
{
infoView.setText("得到黑子.");
}
else
infoView.setText("得到白子.");
stopButton.setEnabled(true);
}
else if(msg.startsWith("[DROPGAME]"))
{
JOptionPane.showMessageDialog(null," 由于对方弃权,你赢了!",
"win the game",JOptionPane.INFORMATION_MESSAGE);
endGame("对方弃权.");
}
else if(msg.startsWith("[WIN]"))
{
JOptionPane.showMessageDialog(null," win the game!",
"win the game",JOptionPane.INFORMATION_MESSAGE);
endGame("获胜.");
}
else if(msg.startsWith("[LOSE]"))
{
JOptionPane.showMessageDialog(null,
"Sorry,You've failed the game",
"Try Again",
JOptionPane.INFORMATION_MESSAGE);
endGame("失败.");
}
else msgView.append(msg+"\n");
}
}catch(IOException ie)
{
msgView.append(ie+"\n");
}
msgView.append("连接中断.");
}
//结束游戏
private void endGame(String msg)
{
infoView.setText(msg);
startButton.setEnabled(false);
stopButton.setEnabled(false);
try{ Thread.sleep(1000); }catch(Exception e){}
if(board.isRunning())
board.stopGame();
if(pList.getItemCount()==2)
startButton.setEnabled(true);//人数为二则可进行游戏
}
//显示信息
private void playersInfo()
{
int count=pList.getItemCount();
if(roomNumber==0)
pInfo.setText("游戏大厅: "+count+"名");
else pInfo.setText(roomNumber+" 房间: "+count+"名");
if(count==2 && roomNumber!=0)
startButton.setEnabled(true);
else startButton.setEnabled(false);
}
private void nameList(String msg)
{
pList.removeAll();
StringTokenizer st=new StringTokenizer(msg, "\t");
while(st.hasMoreElements())
pList.add(st.nextToken());
playersInfo();
}
//连接到服务器
private void connect()
{
try{
msgView.setText("");
msgView.append("正在连接服务器.........\n");
socket=new Socket("127.0.0.1", 8888);
msgView.append("---连接成功--.\n");
msgView.append("请输入名字,然后进入游戏.\n");
reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer=new PrintWriter(socket.getOutputStream(), true);
new Thread(this).start();//创建一个绑定这个runnable的新Thread对象并开始一个将调用这个runnable的run()方法的线程
board.setWriter(writer);
}catch(Exception e)
{
msgView.append(e+"\n\n连接失败..\n");
}
}
public static void main(String[] args)
{
Client client=new Client("网络五子棋游戏");
client.setSize(760,580);
client.setVisible(true);//窗口可见
client.setIconImage(Toolkit.getDefaultToolkit().createImage("img/win.jpg"));
client.connect();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -