当前位置:首页 » 编程博文
开发技术指南» 文章正文
    引言: 下面的源代码分为4个文件;chessClient.java:客户端主程序。
 

 

 ·深入研究 stl deque 容器    »显示摘要«
    摘要:深入研究 stl deque 容器 an in-depth study of the stl deque container (by nitron) 翻译 masterlee 本文档深入分析了std::deque,并提供了一个指导思想:当考虑到内存分配和执行性能的时候,使用std::deque要比std::vector好。 介绍 本文深入地研究了std::deque 容器。本......
 ·测试工作总体流程    »显示摘要«
    摘要:转载自www.testage.net ......


java网络五子棋的源代码

下面的源代码分为4个文件;

chessclient.java:客户端主程序. 【程序编程相关:IBM承诺今年年底发布代号为Atlant

【推荐阅读:窗口控制JS代码集锦

chessinterface.java:客户端的界面. 【扩展信息:正在写一个模拟操作系统中存储管理的程序

chesspad.java:棋盘的绘制.

chessserver.java:服务器端.

可同时容纳50个人同时在线下棋,聊天.

没有加上详细注释,不过绝对可以运行,j2sdk1.4下通过.

/*********************************************************************************************

1.chessclient.java

**********************************************************************************************/

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.net.*;

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.userpad.userlist.removeall();

   chessclient.inputpad.userchoice.removeall();

   chessclient.inputpad.userchoice.additem("所有人");

   while(usertoken.hasmoretokens())

   {

    string user=(string)usertoken.nexttoken(" ");

    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("不能加入游戏");

    chessclient.controlpad.cancelgamebutton.setenabled(false);

    chessclient.controlpad.joingamebutton.setenabled(true);

    chessclient.controlpad.creatgamebutton.setenabled(true);

   }

   catch(exception ef)

   {

    chessclient.chatpad.chatlinearea.settext("chessclient.chesspad.chesssocket.close无法关闭");

   }

   chessclient.controlpad.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=chessclient.in.readutf();

    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();

 panel eastpanel=new panel();

 chessclient()

 {

  super("java五子棋客户端");

  setlayout(new borderlayout());

  host=controlpad.inputip.gettext();

  westpanel.setlayout(new borderlayout());

  westpanel.add(userpad,borderlayout.north);

  westpanel.add(chatpad,borderlayout.center);

  westpanel.setbackground(color.pink);

  inputpad.inputwords.addkeylistener(this);

  chesspad.host=controlpad.inputip.gettext();

  centerpanel.add(chesspad,borderlayout.center);

  centerpanel.add(inputpad,borderlayout.south);

  centerpanel.setbackground(color.pink);

  controlpad.connectbutton.addactionlistener(this);

  controlpad.creatgamebutton.addactionlistener(this);

  controlpad.joingamebutton.addactionlistener(this);

  controlpad.cancelgamebutton.addactionlistener(this);

  controlpad.exitgamebutton.addactionlistener(this);

  controlpad.creatgamebutton.setenabled(false);

  controlpad.joingamebutton.setenabled(false);

  controlpad.cancelgamebutton.setenabled(false);

  southpanel.add(controlpad,borderlayout.center);

  southpanel.setbackground(color.pink);

  addwindowlistener(new windowadapter()

  {

   public void windowclosing(windowevent e)

   {

     if(isonchat)

     {

      try

      {

       chatsocket.close();

      }

      catch(exception ed)

      {

      }

     }

     if(isonchess || isgameconnected)

     {

      try

      {

       chesspad.chesssocket.close();

      }

      catch(exception ee)

      {

      }

     }

    system.exit(0);

   }

   public void windowactivated(windowevent ea)

   {

   }

  });

       add(westpanel,borderlayout.west);

      add(centerpanel,borderlayout.center);

      add(southpanel,borderlayout.south);

  pack();

  setsize(670,548);

  setvisible(true);

  setresizable(false);

  validate();

 }

 

 public boolean connectserver(string serverip,int serverport) throws exception

 {

  try

  {

   chatsocket=new socket(serverip,serverport);

   in=new datainputstream(chatsocket.getinputstream());

   out=new dataoutputstream(chatsocket.getoutputstream());

   clientthread clientthread=new clientthread(this);

   clientthread.start();

   isonchat=true;

   return true;

  }

  catch(ioexception ex)

  {

   chatpad.chatlinearea.settext("chessclient:connectserver:无法连接,建议重新启动程序 \n");

  }

  return false;

 }

  public void actionperformed(actionevent e)

  {

   if(e.getsource()==controlpad.connectbutton)

   {

    host=chesspad.host=controlpad.inputip.gettext();

    try

    {

     if(connectserver(host,port))

     {

      chatpad.chatlinearea.settext("");

      controlpad.connectbutton.setenabled(false);

      controlpad.creatgamebutton.setenabled(true);

      controlpad.joingamebutton.setenabled(true);

      chesspad.statustext.settext("连接成功,请创建游戏或加入游戏");


...   下一页
 ·pl/sql单行函数和组函数详解    »显示摘要«
    摘要:  函数是一种有零个或多个参数并且有一个返回值的程序。在sql中oracle内建了一系列函数,这些函数都可被称为sql或pl/sql语句,函数主要分为两大类:   单行函数   组函数   本文将讨论如何利用单行函数以及使用规则。  sql中的单行函数  sql和pl/sql中自带很多类型的函数,有字符、数字、日期、转换、和混合型等多种函数用于处理单行数据,因此这些都可被统称为单行函数。这些函数均......
» 本期热门文章:

©2000-2007 All Rights Reserved. 最佳浏览:1024X768 MSIE