xxxx18一60岁hd中国/日韩女同互慰一区二区/西西人体扒开双腿无遮挡/日韩欧美黄色一级片 - 色护士精品影院www

  • 大小: 17KB
    文件類(lèi)型: .zip
    金幣: 2
    下載: 0 次
    發(fā)布日期: 2021-05-12
  • 語(yǔ)言: Java
  • 標(biāo)簽:

資源簡(jiǎn)介

使用網(wǎng)絡(luò)編程實(shí)現(xiàn)五子棋小游戲,適合javaSE階段的學(xué)習(xí),熟悉網(wǎng)絡(luò)編程,

資源截圖

代碼片段和文件信息

package?com.jju.chessfive.ui;

import?java.awt.Color;
import?java.awt.Graphics;
import?java.awt.event.MouseEvent;
import?java.awt.event.MouseListener;
import?java.io.BufferedReader;
import?java.io.InputStream;
import?java.io.InputStreamReader;
import?java.io.OutputStream;
import?java.io.PrintWriter;
import?java.net.Socket;?
import?javax.swing.Jframe;
import?javax.swing.JOptionPane;

public?class?ClientWindow?extends?Jframe?implements?MouseListener?{
int?x0?=?50?y0?=?50;//?棋盤(pán)左上角的坐標(biāo)
int?d?=?40;?//?各自的大小
int?N?=?10;?//?棋盤(pán)的大小10*10

//?定義一個(gè)表示棋盤(pán)上每個(gè)頂點(diǎn)的(N*N)的狀態(tài)矩陣
//?0--空白?1--黑子?2--白子
int[][]?pos?=?new?int[N][N];

//?當(dāng)前棋子的顏色?1--黑?2--白
int?chesstype?=?0;

Socket?socket;?//?定義一個(gè)Socket對(duì)象
boolean?isStart?=?false;?//棋局是否開(kāi)始標(biāo)識(shí)
boolean?isMyTurn=?false;?//?是否到我方落子
public?ClientWindow()?{
settitle(“菜鳥(niǎo)五子棋VO.1“);
setSize(800?600);

//?設(shè)置對(duì)鼠標(biāo)事件的監(jiān)聽(tīng)
addMouseListener(this);

try?{
//?和服務(wù)端建立Socket連接
socket?=?new?Socket(“127.0.0.1“?8888);
//?啟動(dòng)接收消息的線(xiàn)程
new?ReceiveMsgThread().start();
}?catch?(Exception?ex)?{
ex.printStackTrace();
}

}

//?繪制屏幕
@Override
public?void?paint(Graphics?g)?{
//?將屏幕背景設(shè)置為白色?---或者加載一個(gè)背景圖片
g.setColor(Color.white);
g.fillRect(0?0?2000?2000);
g.drawString(“菜鳥(niǎo)五子棋VO.1“?400?50);
g.setColor(Color.black);
//?繪制棋盤(pán)
for?(int?i?=?0;?i? //?畫(huà)橫線(xiàn)
g.drawLine(x0?y0?+?i?*?d?x0?+?(N?-?1)?*?d?y0?+?i?*?d);
//?畫(huà)豎線(xiàn)
g.drawLine(x0?+?i?*?d?y0?x0?+?i?*?d?y0?+?(N?-?1)?*?d);
}

//?繪制棋盤(pán)上的棋子---遍歷棋盤(pán)上所有的頂點(diǎn)
//?如果狀態(tài)為1---畫(huà)黑子?2---白子
for?(int?i?=?0;?i? for?(int?j?=?0;?j? int?status?=?pos[i][j];//?取狀態(tài)
if?(status?>?0)?//?有棋子
{
if?(status?==?1)?//?黑子
g.setColor(Color.black);?//?設(shè)置畫(huà)筆顏色
else
g.setColor(Color.yellow);
//?畫(huà)半徑為5的圓表示棋子
g.fillOval(x0?+?j?*?d?-?5?y0?+?i?*?d?-?5?10?10);

}
}

}

@Override
public?void?mouseClicked(MouseEvent?e)?{
//---棋局是否開(kāi)局的判斷,開(kāi)始后方可點(diǎn)擊
if(isStart?==?false)?return;

//到我方落子方可點(diǎn)擊
if(isMyTurn?==?false)?return;

//?鼠標(biāo)點(diǎn)擊的處理方法
//?1.獲得鼠標(biāo)點(diǎn)擊的位置
int?x?=?e.getX();
int?y?=?e.getY();

//?2.找到離該點(diǎn)(xy)最近的棋盤(pán)的頂點(diǎn)序號(hào)(row)
int?row?=?(int)?((y?-?y0)?*?1.0?/?d?+?0.5);
int?col?=?(int)?((x?-?x0)?*?1.0?/?d?+?0.5);

//?2.判斷該位置是否有了棋子
if?(pos[row][col]?>?0)
return;

//?3.設(shè)置該頂點(diǎn)的棋子顏色
pos[row][col]?=?chesstype;

repaint();?//?4.刷新屏幕,重新畫(huà)
//設(shè)置我方不可在落子
isMyTurn?=?false;

//將我方落子信息發(fā)送給服務(wù)端
String?msg?=?“Push#“?+?chesstype?+?“#“?+?row?+?“#“+?col;
sendMessage(?msg);
}

/************?向服務(wù)器發(fā)送消息?********/
public?void?sendMessage(String?msg)?{
try?{
OutputStream?os?=?socket.getOutputStream();
PrintWriter?pw?=?new?PrintWriter(os?true);
pw.println(msg);?//?發(fā)送數(shù)據(jù)
}?catch?(Exception?ex)?{
ex.printStackTrace();
}
}

/****?定義接收服務(wù)端發(fā)送過(guò)來(lái)的消息的線(xiàn)程?*****/
class?ReceiveMsgThread?extends?Thread?{
@Override
public?void?run()?{
try?{
//?獲

?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????目錄???????????0??2016-09-05?15:23??NetChessFive32\
?????文件?????????301??2016-09-05?14:47??NetChessFive32\.classpath
?????文件?????????390??2016-09-05?14:47??NetChessFive32\.project
?????目錄???????????0??2016-09-05?15:23??NetChessFive32\.settings\
?????文件?????????598??2016-09-05?14:47??NetChessFive32\.settings\org.eclipse.jdt.core.prefs
?????目錄???????????0??2016-10-24?08:29??NetChessFive32\bin\
?????目錄???????????0??2016-10-24?08:29??NetChessFive32\bin\com\
?????目錄???????????0??2016-10-24?08:29??NetChessFive32\bin\com\jju\
?????目錄???????????0??2016-10-24?08:29??NetChessFive32\bin\com\jju\chessfive\
?????目錄???????????0??2016-10-24?08:29??NetChessFive32\bin\com\jju\chessfive\ui\
?????文件????????1303??2016-10-24?08:29??NetChessFive32\bin\com\jju\chessfive\ui\ClientWindow$ReceiveMsgThread.class
?????文件????????4596??2016-10-24?08:29??NetChessFive32\bin\com\jju\chessfive\ui\ClientWindow.class
?????文件????????1681??2016-10-24?08:29??NetChessFive32\bin\com\jju\chessfive\ui\ServerWindow$ReceiveMsgThreadSingleSocket.class
?????文件????????1862??2016-10-24?08:29??NetChessFive32\bin\com\jju\chessfive\ui\ServerWindow$ServerListenerThread.class
?????文件????????4405??2016-10-24?08:29??NetChessFive32\bin\com\jju\chessfive\ui\ServerWindow.class
?????目錄???????????0??2016-09-05?15:23??NetChessFive32\src\
?????目錄???????????0??2016-09-05?15:23??NetChessFive32\src\com\
?????目錄???????????0??2016-09-05?15:23??NetChessFive32\src\com\jju\
?????目錄???????????0??2016-09-05?15:23??NetChessFive32\src\com\jju\chessfive\
?????目錄???????????0??2016-09-05?15:23??NetChessFive32\src\com\jju\chessfive\ui\
?????文件????????5124??2016-09-07?10:54??NetChessFive32\src\com\jju\chessfive\ui\ClientWindow.java
?????文件????????5982??2016-09-07?10:48??NetChessFive32\src\com\jju\chessfive\ui\ServerWindow.java

評(píng)論

共有 條評(píng)論