java程序设计五子棋.docx

上传人:b****7 文档编号:9248808 上传时间:2023-02-03 格式:DOCX 页数:52 大小:29KB
下载 相关 举报
java程序设计五子棋.docx_第1页
第1页 / 共52页
java程序设计五子棋.docx_第2页
第2页 / 共52页
java程序设计五子棋.docx_第3页
第3页 / 共52页
java程序设计五子棋.docx_第4页
第4页 / 共52页
java程序设计五子棋.docx_第5页
第5页 / 共52页
点击查看更多>>
下载资源
资源描述

java程序设计五子棋.docx

《java程序设计五子棋.docx》由会员分享,可在线阅读,更多相关《java程序设计五子棋.docx(52页珍藏版)》请在冰豆网上搜索。

java程序设计五子棋.docx

java程序设计五子棋

packageFiveChess;

importjava.awt.Color;

importjava.awt.Graphics;

publicclassChess{

intid;

intchess_x;

intchess_y;

intstate;

intradius=13;

publicChess(){}

publicChess(intid,intchess_x,intchess_y,intstate){

this.id=id;

this.chess_x=chess_x;

this.chess_y=chess_y;

this.state=state;

}

publicintgetChess_x(){

returnchess_x;

}

publicvoidsetChess_x(intchess_x){

this.chess_x=chess_x;

}

publicintgetChess_y(){

returnchess_y;

}

publicvoidsetChess_y(intchess_y){

this.chess_y=chess_y;

}

publicintgetState(){

returnstate;

}

publicvoidsetState(intstate){

this.state=state;

}

publicintgetId(){

returnid;

}

publicvoidsetId(intid){

this.id=id;

}

publicvoiddrawChess(Graphicsg){

Colorcolor=null;

if(state==1){

color=newColor(255,255,255);

g.setColor(color);

g.fillOval(chess_x*30-radius,chess_y*30-radius,radius*2,radius*2);

}elseif(state==-1){

color=newColor(0,0,0);

g.setColor(color);

g.fillOval(chess_x*30-radius,chess_y*30-radius,radius*2,radius*2);

}

}

}

packageFiveChess;

importjava.awt.Graphics;

publicclassChessBoard{

int[][]board=newint[16][16];

publicChessBoard(){

initialBoard();

}

publicvoidinitialBoard(){

for(inti=0;i

for(intj=0;j

board[i][j]=0;//0无棋1白棋-1黑棋

}

}

}

publicvoiddraw(Graphicsg){

g.drawLine(25,25,455,25);

g.drawLine(455,25,455,455);

g.drawLine(25,455,455,455);

g.drawLine(25,25,25,455);

inti=1,j=1;

while(i

g.drawLine(i*30,30,i*30,15*30);

i++;

}

while(j

g.drawLine(30,j*30,15*30,j*30);

j++;

}

}

publicintgetState(intx,inty){

if(x>0&y>0&x<16&y<16)returnboard[x][y];

elsereturn-2;

}

publicvoidsetState(intx,inty,intstate){

board[x][y]=state;

}

}

packageFiveChess;

importjava.awt.BorderLayout;

importjava.awt.Color;

importjava.awt.Dimension;

importjava.awt.Image;

importjava.awt.TextArea;

importjava.awt.Toolkit;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.awt.event.KeyListener;

importjava.awt.event.WindowAdapter;

importjava.awt.event.WindowEvent;

importjava.io.IOException;

import.ServerSocket;

import.Socket;

importjava.sql.Time;

importjavax.swing.ImageIcon;

importjavax.swing.JButton;

importjavax.swing.JFrame;

importjavax.swing.JLabel;

importjavax.swing.JMenu;

importjavax.swing.JMenuBar;

importjavax.swing.JMenuItem;

importjavax.swing.JOptionPane;

importjavax.swing.JPanel;

importjavax.swing.JSplitPane;

importjavax.swing.JTextField;

importjavax.swing.border.Border;

publicclassChessServerextendsJFrameimplementsActionListener{

publicstaticfinalintframe_width=35;//窗体的宽度

publicstaticfinalintframe_height=27;//窗体的高度

publicstaticfinalintframe_opration=10;//操作区域的宽度

publicstaticfinalintblock_width=20;//单位宽度

publicstaticfinalintblock_height=20;//单位高度

ServerWarjpz=newServerWar(this);//创建服务器端主面板

JMenuBarmenubar=newJMenuBar();//创建菜单条

JMenufileMenu=newJMenu("文件");

JMenuItemone=newJMenuItem("单人游戏");

JMenuItemtwo=newJMenuItem("双人游戏");

JMenuItemexit=newJMenuItem("退出");

JPaneljpy=newJPanel();//创建操作区的面板

JLabeljlPort=newJLabel("端口号");//创建提示用户输入端口号的标签

JTextFieldjtfPort=newJTextField("9999");//创建用于输入端口号的文本框,默认是9999

JLabeljlNickName=newJLabel("昵称");//创建用于提示输入昵称的标签

JTextFieldjtfNickName=newJTextField("Player2");//创建用于输入昵称的文本框

JButtonjbNew=newJButton("建主");//创建"建主","停止","开始"三个动作按钮

JButtonjbStop=newJButton("停止");

JButtonjbStart=newJButton("开始");

JButtonjbSend=newJButton("发送");

TextAreajaChat=newTextArea("",5000,1,TextArea.SCROLLBARS_VERTICAL_ONLY);

inttextIndex=0;

JTextFieldjtfMessage=newJTextField("你好");

JSplitPanejsp=newJSplitPane(JSplitPane.HORIZONTAL_SPLIT,jpz,jpy);//创建JSplitPane

ServerSocketss;//声明ServerSocket引用

ServerThreadst;//声明服务器线程

ServerAgentThreadsat;//声明服务器代理线程引用

publicChessServer(){

this.addComponent();//初始化窗体,将控件添加到窗体中

this.addListener();//为相应控件注册监听器

this.initialFrame();//初始化窗体

jpz.setFocusable(true);

jpz.requestFocus(true);//使左边面板获得焦点

}

publicvoidaddComponent(){//添加控件的方法

jpy.setLayout(null);//设为空布局

jlPort.setBounds(10,20,40,25);

jpy.add(jlPort);//添加"端口号"标签

jtfPort.setBounds(55,20,100,25);

jpy.add(jtfPort);//添加输入端口号的文本框

jlNickName.setBounds(10,50,40,25);

jpy.add(jlNickName);//添加"昵称"标签

jtfNickName.setBounds(55,50,100,25);

jpy.add(jtfNickName);//添加输入昵称的文本框

jbNew.setBounds(20,85,60,20);

jpy.add(jbNew);//添加"建主"按钮

jbStop.setBounds(100,85,60,20);

jpy.add(jbStop);//添加"停止"按钮

jbStart.setBounds(20,120,140,30);

jpy.add(jbStart);//添加"开始"按钮

jaChat.setBounds(10,230,160,205);

jaChat.setForeground(Color.blue);

jaChat.setEditable(false);

jpy.add(jaChat);

jtfMessage.setBounds(10,440,100,25);

jpy.add(jtfMessage);

jbSend.setBounds(120,440,60,20);

jpy.add(jbSend);

jsp.setDividerLocation((frame_width-frame_opration)*block_width);

jsp.setDividerSize(4);//设置JSplitPane分割线的位置及宽度

fileMenu.add(one);

fileMenu.add(two);

fileMenu.addSeparator();

fileMenu.add(exit);

fileMenu.setBounds(0,0,20,20);

menubar.add(fileMenu);

this.add(menubar,BorderLayout.NORTH);

this.add(jsp);//添加jsp

}

publicvoidaddListener(){//为控件注册监听器

jbNew.addActionListener(this);//为"建主"按钮注册监听器

jbStop.addActionListener(this);//为"停止"按钮注册监听器

jbStart.addActionListener(this);//为"开始"按钮注册监听器

one.addActionListener(this);

two.addActionListener(this);

exit.addActionListener(this);

jbSend.addActionListener(this);

}

publicvoidactionPerformed(ActionEvente){

if(e.getSource()==this.jbNew){//当按下"建主"按钮时

intport=0;

try{//获得用户输入的端口号

port=Integer.parseInt(this.jtfPort.getText().trim());

}

catch(Exceptionee){//如果不是数字则给出提示

JOptionPane.showMessageDialog(this,"端口号只能是整数","错误",

JOptionPane.ERROR_MESSAGE);

return;

}

if(port>65535||port<0){//判断端口号是否在有效范围内

JOptionPane.showMessageDialog(this,"端口号只能是0-65535的整数",

"错误",JOptionPane.ERROR_MESSAGE);

return;

}

try{

ss=newServerSocket(port);//创建ServerSocket

st=newServerThread(this,ss);//创建服务器线程

st.start();//启动服务器线程

JOptionPane.showMessageDialog(this,"服务器启动成功","提示",

JOptionPane.INFORMATION_MESSAGE);//给出成功的提示

this.setState(false);//设置窗体状态

this.jbStart.setEnabled(false);

}

catch(Exceptionee){//启动失败

JOptionPane.showMessageDialog(this,"服务器启动失败","提示",

JOptionPane.INFORMATION_MESSAGE);

}

}

elseif(e.getSource()==this.jbStop){//当单击"停止"按钮时

try{

if(sat!

=null){//代理线程不为空,则向客户端发出离开的消息

sat.dout.writeUTF("<#ServerDown#>");

sat.setFlag(false);//终止代理线程

}

st.setFlag(false);//终止服务器线程

ss.close();//关闭连接

}

catch(IOExceptionee){ee.printStackTrace();}

this.setState(true);//设置窗体状态

jpz.setGameState(false);//设置游戏状态

}

elseif(e.getSource()==this.jbStart){//当单击"开始"按钮时

if(jpz.ready==false){

JOptionPane.showMessageDialog(this,"客户端还未准备",

"提示",JOptionPane.INFORMATION_MESSAGE);

}else{

intresult=JOptionPane.showConfirmDialog(this,"是否开始新游戏?

",

"提示",JOptionPane.YES_NO_OPTION);

if(result==0){

try{sat.dout.writeUTF("<#Start#>");}

catch(IOExceptionee){ee.printStackTrace();}

jpz.setGameState(true);//开始游戏

jpz.setChess_id(0);

jpz.chess=newChess[255];

jpz.chess[0]=newChess(0,0,0,0);

jpz.repaint();

}

}

}

elseif(e.getSource()==this.one){

//与计算机游戏

jtfPort.setEnabled(false);

jtfNickName.setEnabled(false);

jbNew.setEnabled(false);

jbStop.setEnabled(false);

jbStart.setEnabled(false);

jpz.setGameState(false);

jpz.setOneGameState(true);

}

elseif(e.getSource()==this.two){

setState(true);

jpz.setOneGameState(false);

}

elseif(e.getSource()==this.exit){

intresult=JOptionPane.showConfirmDialog(this,"是否要退出?

",

"提示",JOptionPane.YES_NO_OPTION);

if(result==0){

System.exit(0);

}

}

elseif(e.getSource()==this.jbSend){

if(!

jtfMessage.getText().trim().equals("")){

Timetime=newTime(System.currentTimeMillis());

Stringmessage=jtfNickName.getText()+""+time.getHours()+":

"+time.getMinutes()+":

"+time.getSeconds()+"\n"+jtfMessage.getText()+"\n";

try{

sat.dout.writeUTF("<#ServerMessage#>"+message);

}catch(IOExceptione1){

e1.printStackTrace();

}

jtfMessage.setText("");

jaChat.insert(message,textIndex);

textIndex+=message.length();

}else{

Strings="不能发送空消息。

\n";

jaChat.insert(s,textIndex);

textIndex+=s.length();

}

}

jpz.requestFocus(true);

}

publicvoidinitialFrame(){//初始化窗体的方法

this.setState(true);

//Imageimage=newImageIcon("ico.gif").getImage();

//this.setIconImage(image);//设置图标

this.setTitle("FiveChess--Server");//设置标题

this.setResizable(false);

DimensionscreenSize=//获得屏幕尺寸

Toolkit.getDefaultToolkit().getScreenSize();

intcenterX=screenSize.width/2;//计算屏幕的中心坐标

intcenterY=screenSize.height/2;

intwidth=frame_width*block_width;//计算窗口的高度和宽度

intheight=frame_height*block_height;

this.setBounds(centerX-width/2,centerY-height/2-30,width,height);//使窗体居中显示

this.addWindowListener(

newWindowAdapter(){

publicvoidwindowClosing(WindowEvente){

if(st==null){//服务器线程为空,则直接退出

System.exit(0);

return;

}

try{

if(sat!

=null){//代理线程非空,向客户端发送离开的消息

sat.dout.writeUTF("<#ServerDown#>");

sat.setFlag(false);//终止代理线程

}

st.setFlag(false);//终止服务器线程

ss.close();//关闭连接

}

catch(IOExceptionee){ee.printStackTrace();}

System.exit(0);

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 英语

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1