91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 17KB
    文件類型: .zip
    金幣: 2
    下載: 0 次
    發布日期: 2021-05-12
  • 語言: Java
  • 標簽:

資源簡介

使用網絡編程實現五子棋小游戲,適合javaSE階段的學習,熟悉網絡編程,

資源截圖

代碼片段和文件信息

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;//?棋盤左上角的坐標
int?d?=?40;?//?各自的大小
int?N?=?10;?//?棋盤的大小10*10

//?定義一個表示棋盤上每個頂點的(N*N)的狀態矩陣
//?0--空白?1--黑子?2--白子
int[][]?pos?=?new?int[N][N];

//?當前棋子的顏色?1--黑?2--白
int?chesstype?=?0;

Socket?socket;?//?定義一個Socket對象
boolean?isStart?=?false;?//棋局是否開始標識
boolean?isMyTurn=?false;?//?是否到我方落子
public?ClientWindow()?{
settitle(“菜鳥五子棋VO.1“);
setSize(800?600);

//?設置對鼠標事件的監聽
addMouseListener(this);

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

}

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

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

}
}

}

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

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

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

//?2.找到離該點(xy)最近的棋盤的頂點序號(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.設置該頂點的棋子顏色
pos[row][col]?=?chesstype;

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

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

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

/****?定義接收服務端發送過來的消息的線程?*****/
class?ReceiveMsgThread?extends?Thread?{
@Override
public?void?run()?{
try?{
//?獲

?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????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

評論

共有 條評論