chinesechess.java
来自「可联机对战的中国象棋java源代码 包括服务器文件和客户端文件」· Java 代码 · 共 1,165 行 · 第 1/4 页
JAVA
1,165 行
public void mouseEntered(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e)
{ if(sound)select.play();
try{
m_output=new DataOutputStream(s.getOutputStream());
m_output.writeInt(795);m_output.writeInt(800);m_output.writeInt(800);
peaceable.setVisible(false);
peaceunable.setVisible(true);
giveinready.setVisible(false);
givein.setVisible(true);
}
catch(Exception f){}
}
});
giveinready.addMouseListener(new MouseListener()//认输按钮
{
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e)
{ if(sound)select.play();
try{
m_output=new DataOutputStream(s.getOutputStream());
m_output.writeInt(798);m_output.writeInt(800);m_output.writeInt(800);
giveinready.setVisible(false);
givein.setVisible(true);
JOptionPane.showMessageDialog(null,"很遗憾您输了");if(sound)gameover.play();
endgame();
}
catch(Exception f){}
}
});
regretready.addMouseListener(new MouseListener()//悔棋按钮
{
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e)
{ if(sound)select.play();
if((num>=2&&!host)||(num>=1&&host))
{
try{
m_output=new DataOutputStream(s.getOutputStream());
m_output.writeInt(799);m_output.writeInt(800);m_output.writeInt(800);
regretready.setVisible(false);
regret.setVisible(true);
JOptionPane.showMessageDialog(null,"已向对方发出悔棋请求");
}
catch(Exception f){}
}
else JOptionPane.showMessageDialog(null,"您现在还不能悔棋");
}
});
send.addActionListener(new ActionListener()//发送按钮
{
public void actionPerformed(ActionEvent e)
{
if(sound)select.play();
chart.append(name+"说:"+chartinput.getText()+"\n");
try{
m_output=new DataOutputStream(s.getOutputStream());
m_output.writeInt(911);m_output.writeInt(911);m_output.writeInt(911);
m_output.writeUTF(name+"说:"+chartinput.getText());
}
catch(Exception f){}
chartinput.setText("");
}
});
ma[0][0].addActionListener(new ActionListener()//建立游戏
{
public void actionPerformed(ActionEvent p)
{
if(built[0])
{
name=JOptionPane.showInputDialog(null,"请输入您的姓名","输入");
host=true;
player[0]=1;
info.setText("等待玩家进入,你将执红棋\n");
serverbuildThread a=new serverbuildThread();
a.start();
if(first){first=false;
receiveThread c=new receiveThread();
c.start();}
}
else JOptionPane.showMessageDialog(null,"已在游戏中");}}
);
ma[0][1].addActionListener(new ActionListener()//加入游戏
{
public void actionPerformed(ActionEvent e)
{
if(built[0])
{
name=JOptionPane.showInputDialog(null,"请输入您的姓名","输入");
host=false;
player[0]=3;
clientjoinThread b=new clientjoinThread();
b.start();
if(first){first=false;
receiveThread d=new receiveThread();
d.start();
}}
else JOptionPane.showMessageDialog(null,"已在游戏中");}}
);
ma[0][2].addActionListener(new ActionListener()//保存残局
{
public void actionPerformed(ActionEvent e)
{
try
{
FileOutputStream chesssave=new FileOutputStream(JOptionPane.showInputDialog(null,"请输入文件名","data")+".txt");
DataOutputStream dchesssave=new DataOutputStream(chesssave);
for(int i=0;i<=31;i++)
{dchesssave.writeInt(chessman[i].getX());dchesssave.writeInt(chessman[i].getY());}
dchesssave.close();
}
catch(Exception f)
{}
}
});
ma[0][3].addActionListener(new ActionListener()//读取残局
{
public void actionPerformed(ActionEvent e)
{
if(!built[0])JOptionPane.showMessageDialog(null,"已与他人建立连接,不能读取残局");
else
{
try
{
FileInputStream chessload=new FileInputStream(JOptionPane.showInputDialog(null,"请输入文件名","data")+".txt");
DataInputStream dchessload=new DataInputStream(chessload);
for(int i=0;i<=31;i++)
{
chessman[i].setBounds(dchessload.readInt(),dchessload.readInt(),40,40);
if(chessman[i].getX()==0)chessman[i].setVisible(false);
}
}
catch(Exception f){JOptionPane.showMessageDialog(null,"未找到文件");}
}
}
});
ma[0][4].addActionListener(new ActionListener()//断开连接
{
public void actionPerformed(ActionEvent e)
{
if(!built[0])
{
if(JOptionPane.showConfirmDialog(null,"即将断开连接,请确认","确认",JOptionPane.YES_NO_OPTION)==0)
{
try
{
m_input.close();m_output.close();s.close();server.close();}catch(Exception f){}
JOptionPane.showMessageDialog(null,"连接已经断开");
}
}
else{
JOptionPane.showMessageDialog(null,"未建立任何连接");
}
}
});
ma[0][5].addActionListener(new ActionListener()//退出
{
public void actionPerformed(ActionEvent e)
{
if(JOptionPane.showConfirmDialog(null,"即将退出游戏,请确认","确认",JOptionPane.YES_NO_OPTION)==0)
System.exit(0);
}
});
soundon.addActionListener(new ActionListener()//音效开
{
public void actionPerformed(ActionEvent e)
{
sound=true;
}
});
soundoff.addActionListener(new ActionListener()//音效关
{
public void actionPerformed(ActionEvent e)
{
sound=false;
}
});
musicon.addActionListener(new ActionListener()//音乐开
{
public void actionPerformed(ActionEvent e)
{
backgroundmusic.loop();
}
});
musicoff.addActionListener(new ActionListener()//音乐关
{
public void actionPerformed(ActionEvent e)
{
backgroundmusic.stop();
}
});
//背景监听完毕
}//构造方法结束
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public static void putchessman()//放置棋子开始
{
int i,j;
for(i=0;i<=31;i++)
chessman[i].setVisible(true);
//红子
for(i=0,j=5;i<=1;i++,j+=480)//车
{
chessman[i].setBounds(j,5,40,40);
}
for(i=2,j=65;i<=3;i++,j+=360)//马
{
chessman[i].setBounds(j,5,40,40);
}
for(i=4,j=125;i<=5;i++,j+=240)//相
{
chessman[i].setBounds(j,5,40,40);
}
for(i=6,j=185;i<=7;i++,j+=120)//士
{
chessman[i].setBounds(j,5,40,40);
}//帅
chessman[8].setBounds(245,5,40,40);
for(i=9,j=65;i<=10;i++,j+=360)//炮
{
chessman[i].setBounds(j,125,40,40);
}
for(i=11,j=5;i<=15;i++,j+=120)//卒
{
chessman[i].setBounds(j,185,40,40);
}
//黑子
for(i=16,j=5;i<=17;i++,j+=480)//车
{
chessman[i].setBounds(j,545,40,40);
}
for(i=18,j=65;i<=19;i++,j+=360)//马
{
chessman[i].setBounds(j,545,40,40);
}
for(i=20,j=125;i<=21;i++,j+=240)//相
{
chessman[i].setBounds(j,545,40,40);
}
for(i=22,j=185;i<=23;i++,j+=120)//士
{
chessman[i].setBounds(j,545,40,40);
}//帅
chessman[24].setBounds(245,545,40,40);
for(i=25,j=65;i<=26;i++,j+=360)//炮
{
chessman[i].setBounds(j,425,40,40);
}
for(i=27,j=5;i<=31;i++,j+=120)//卒
{
chessman[i].setBounds(j,365,40,40);
}//棋子摆放结束
}
public void send()//网络发送信息
{
try{
int netX,netY,netman,j;
m_output=new DataOutputStream(s.getOutputStream());
m_output.writeInt(man);m_output.writeInt(chessman[man].getX());m_output.writeInt(chessman[man].getY());
}
catch (Exception e)
{System.err.println(e);e.printStackTrace();}
}
public static void endgame()//游戏结束重置棋盘方法
{
putchessman();
recent.setVisible(false);
selected.setVisible(false);
matchbegin.setVisible(false);
matchend.setVisible(true);
readyhere=false;
readythere=false;
redready.setVisible(false);
blackready.setVisible(false);
beginready.setVisible(true);
began.setVisible(false);
peaceable.setVisible(false);
peaceunable.setVisible(true);
giveinready.setVisible(false);
givein.setVisible(true);
regretready.setVisible(false);
regret.setVisible(true);
num=0;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?