?? clientchess.java
字號(hào):
for(i=1;i<=r;i++)
{
int j=1;
for(j=1;j<=c;j++)
{
ppoint[i][j]=new C_ChessPoint(i*unitWidth,j*unitHeight,false);
}
}
try
{
output=new DataOutputStream(clientOne.soc.getOutputStream()); //創(chuàng)建輸入輸出流的實(shí)例
input = new DataInputStream(clientOne.soc.getInputStream());
}
catch(IOException ee)
{
}
}
//發(fā)送數(shù)據(jù)的方法
public void sendData( String messageM)
{
if(messageM.charAt(0)>'o'||messageM.charAt(0)<'a')
{
try
{
output.writeUTF( "\n客戶端:" + messageM);//向服務(wù)器發(fā)送數(shù)據(jù)
output.flush();
displayArea.append( "\n客戶端:" + messageM);
}
catch ( IOException ioException )
{
displayArea.append( "\n傳輸錯(cuò)誤!" );
}
}
}
//關(guān)閉連接的方法
public void closeConnection()
{
try
{
output.close();
input.close();
clientOne.soc.close();
}
catch(IOException ed)
{
}
}
//繪制棋盤
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int j=1; //畫棋盤的橫格線
for(j=1;j<=yy;j++)
{
g.drawLine(ppoint[1][j].x,ppoint[1][j].y,ppoint[xx][j].x,ppoint[xx][j].y);
}
int i=1; //畫棋盤的豎格線
for(i=1;i<=xx;i++)
{
g.drawLine(ppoint[i][1].x,ppoint[i][1].y,ppoint[i][yy].x,ppoint[i][yy].y);
}
//畫棋盤的兩邊提示坐標(biāo)
for(i=1;i<=xx;i++)
{
g.drawString(""+i,i*unitWidth,unitHeight/2);
}
j=1;
for(char c='A';c<='O';c++)
{
g.drawString(" "+c,unitWidth/4,j*unitHeight);
j++;
}
}
//接收并處理服務(wù)器端發(fā)來的信息
public void receive()
{
//用于標(biāo)識(shí)棋子位置的變量
String[]x_t={"","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o"};
String[]y_t={"","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o"};
int new_M=0,new_N=0; //棋子的新位置
C_ChessPiece piece=null;
while(true)
{
try
{
clientPlay=true;
String message=(String)input.readUTF(); //讀取輸入流數(shù)據(jù)
if(message!=null&&message.charAt(0)<='o'&&message.charAt(0)>='a') //排除標(biāo)識(shí)棋子位置的字符
{
displayArea.append("\n服務(wù)器系統(tǒng)消息(對(duì)方下棋位置):");
new_M=message.charAt(0)-96;
new_N=message.charAt(1)-96;
displayArea.append(""+new_M);
displayArea.append(","+(char)(new_N+96));
if(message.charAt(2)=='b') //接收到對(duì)方是黑子的處理方法
{
piece=new C_ChessPiece(Color.orange,Color.black,unitWidth-3,unitHeight-3,this);
ppoint[new_M][new_N].setPiece(piece,this);
blackWon.a[new_N]=blackWon.a[new_N]+(2<<new_M);
widthPlay=true;
blackPlay=false;
if(blackWon.isWon()==true)
{
displayArea.append("\n黑方勝利!請(qǐng)開始..."); //如果一方勝利則提示重新開始
clientPlay=false;
widthPlay=false;
blackPlay=false;
break;
}
}
if(message.charAt(2)=='w') //接收到對(duì)方是白子的處理方法
{
piece=new C_ChessPiece(Color.orange,Color.white,unitWidth-3,unitHeight-3,this);
ppoint[new_M][new_N].setPiece(piece,this);
whiteWon.a[new_N]=whiteWon.a[new_N]+(2<<new_M);
widthPlay=false;
blackPlay=true;
if(whiteWon.isWon()==true)
{
displayArea.append("\n白方勝利!請(qǐng)開始..."); //如果一方勝利則提示重新開始
clientPlay=false;
widthPlay=false;
blackPlay=false;
break;
}
}
}
else if(message!=null&&(message.charAt(0)>'o'||message.charAt(0)<'a')) //顯示漢語聊天內(nèi)容
{
displayArea.append(message);
}
}
catch(IOException cc)
{
}
}
}
//處理鼠標(biāo)點(diǎn)擊事件
public void mouseClicked(MouseEvent e)
{
try
{
C_ChessPiece piece=null;
Rectangle rect=null;
//用于標(biāo)識(shí)棋子位置的字符
String[]x_t={"","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o"};
String[]y_t={"","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o"};
int new_M=0,new_N=0; //記錄傳輸過去和傳輸過來的棋子位置
if(e.getSource()==this)
{
int p=(unitWidth-2)/2;
int q=(unitHeight-2)/2;
rect=new Rectangle((e.getX()-p),e.getY()-q,2*p,2*q);
boolean containChessPoint=false;
int x=0,y=0;
int m=0,n=0;
//檢查棋子當(dāng)前覆蓋的區(qū)域是否有一個(gè)棋點(diǎn)
for(int i=1;i<=xx;i++)
{
for(int j=1;j<=yy;j++)
{
x=ppoint[i][j].getX();
y=ppoint[i][j].getY();
if(rect.contains(x,y))
{
containChessPoint=true;
m=i;
n=j;
}
}
}
if(clientPlay==true&&widthPlay==false&&blackPlay==true)
{
try
{
piece=new C_ChessPiece(Color.orange,Color.black,unitWidth-3,unitHeight-3,this);
ppoint[m][n].setPiece(piece,this); //在棋盤上放置棋子
blackWon.a[n]=blackWon.a[n]+(2<<(m-1)); //置有棋子的位置為1
widthPlay=true;
blackPlay=false;
clientPlay=false;
if(blackWon.isWon()==true)
{
displayArea.append("\n黑方勝利!請(qǐng)開始..."); //如果一方勝利則提示重新開始
clientPlay=false;
widthPlay=false;
blackPlay=false;
}
output.writeUTF(x_t[m]+y_t[n]+"b"); //向客戶端發(fā)送數(shù)據(jù)
output.flush();
}
catch(IOException IOException)
{
System.out.println("Client catch black.");
}
}
else if(clientPlay==true&&widthPlay==true&&blackPlay==false)
{
try
{
piece=new C_ChessPiece(Color.orange,Color.white,unitWidth-3,unitHeight-3,this);
ppoint[m][n].setPiece(piece,this);
whiteWon.a[n]=whiteWon.a[n]+(2<<(m-1)); //置有棋子的位置為1
widthPlay=false;
blackPlay=true;
clientPlay=false;
if(whiteWon.isWon()==true)
{
displayArea.append("\n白方勝利!請(qǐng)開始..."); //如果一方勝利則提示重新開始
clientPlay=false;
widthPlay=false;
blackPlay=false;
}
output.writeUTF(x_t[m]+y_t[n]+"w"); //向客戶端發(fā)送數(shù)據(jù)
output.flush();
}
catch(IOException IOException)
{
System.out.println("Client catch white.");
}
}
}
}
catch(Exception ex)
{
System.out.println("Catch block.");
}
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
}
public class ClientChess extends JFrame implements ActionListener
{
C_ChessBoard board=null;
Container con=null;
public ClientChess()
{
super("五子棋——客戶端");
board=new C_ChessBoard(35,35,15,15); //創(chuàng)建棋盤的實(shí)例
con=getContentPane();
JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,board,board.pan);
split.setDividerSize(5);
split.setDividerLocation(560);
con.add(split,BorderLayout.CENTER);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
setBounds(60,20,850,600);
con.validate();
validate();
}
public void actionPerformed(ActionEvent e)
{
}
//主方法,客戶端程序執(zhí)行的入口
public static void main(String args[])
{
ClientChess C_chess=new ClientChess();
C_chess.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
C_chess.board.receive();
C_chess.board.closeConnection();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -