服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java swing实现简单的五子棋游戏

java swing实现简单的五子棋游戏

2021-08-18 12:03Jayce27 Java教程

这篇文章主要为大家详细介绍了java swing实现简单的五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

用java swing写的一个简单的五子棋游戏。

java swing实现简单的五子棋游戏

下面是Main.java。

  1. package com.crossing.main;
  2. import com.crossing.view.GameWindow;
  3. public class Main {
  4. /**
  5. * @param args
  6. */
  7. public static void main(String[] args) {
  8. GameWindow gameWindow = new GameWindow();
  9. }
  10. }

下面是GameWindow.java。

  1. package com.crossing.view;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Font;
  6. import java.awt.Graphics;
  7. import java.awt.GridLayout;
  8. import java.awt.Rectangle;
  9. import java.awt.Toolkit;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.MouseEvent;
  13. import java.awt.event.MouseListener;
  14. import java.awt.image.BufferedImage;
  15. import java.io.File;
  16. import java.io.IOException;
  17.  
  18. import javax.imageio.ImageIO;
  19. import javax.swing.JButton;
  20. import javax.swing.JFrame;
  21. import javax.swing.JOptionPane;
  22. import javax.swing.JPanel;
  23.  
  24. /**
  25. * @Title: GameWindow.java
  26. * @Package com.crossing.view
  27. * @Description: TODO(用一句话描述该文件做什么)
  28. * @author crossing
  29. * @date 2021年2月28日 下午9:23:14
  30. * @version V1.0
  31. */
  32. @SuppressWarnings("serial")
  33. public class GameWindow extends JFrame implements MouseListener, Runnable {
  34. private int width, height;// 屏幕宽高
  35. private int mouseX = 0, mouseY = 0, mapsX = 0, mapsY = 0;// 鼠标坐标,鼠标在地图中的位置
  36. private int game_width = 600, game_height = 600;// 游戏窗口大小
  37. private BufferedImage bgImage = null;// 背景图片
  38. private int chessBoardItemWidth = 25;// 棋盘每一小格的大小
  39. private Rectangle chessBoardRect = null;// 棋盘所在矩形
  40. private BufferedImage offsetImg = new BufferedImage(game_width, game_height, BufferedImage.TYPE_4BYTE_ABGR);
  41. private Graphics g = offsetImg.getGraphics();// 双缓冲解决闪烁问题
  42. private int[][] maps = new int[15][15];// 0无棋子,1黑子,2白子
  43. private boolean isBlack = true;// 是否是黑方的回合
  44. private String message = "黑方先行", whitemessage = "无限制", blackmessage = "无限制";// 界面上方信息,下方时间信息
  45. // 右边操作界面
  46. private JButton btn_start, btn_exit, btn_settings;
  47. private JPanel operaterPanel;// 操作面板
  48.  
  49. private int gametime = 0;// 游戏时间限制(秒)
  50. private int blackTime = 0, whiteTime = 0;// 黑白方剩余时间
  51.  
  52. private Thread timeThread = new Thread(this);// 黑白双方倒计时线程
  53. // private boolean isLimitTime = false;
  54.  
  55. public GameWindow() {
  56. setTitle("五子棋");
  57. setSize(game_width, game_height);
  58. setResizable(false);
  59. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  60.  
  61. // 获取屏幕宽高
  62. width = Toolkit.getDefaultToolkit().getScreenSize().width;
  63. height = Toolkit.getDefaultToolkit().getScreenSize().height;
  64.  
  65. // 棋盘位置矩形
  66. chessBoardRect = new Rectangle(50, 120, 370, 370);
  67.  
  68. setLocation((width - game_width) / 2, (height - game_height) / 2);
  69.  
  70. addMouseListener(this);
  71.  
  72. // 初始化右边的面板
  73. initOeratePane();
  74.  
  75. repaint();
  76.  
  77. // 设置背景
  78. try {
  79. bgImage = ImageIO.read(new File("img/backgroung.png"));
  80. // System.out.println(bgImage);
  81. } catch (IOException e) {
  82. e.printStackTrace();
  83. }
  84. setVisible(true);
  85. }
  86.  
  87. /**
  88. * 初始化黑白双方时间
  89. */
  90. private void initTime() {
  91. // System.out.println("isLimitTime:" + isLimitTime);
  92. if (gametime > 0) {
  93.  
  94. timeThread.start();
  95.  
  96. if (blackTime < 0) {
  97. JOptionPane.showMessageDialog(this, "黑方时间已到,白方获胜!");
  98. timeThread.interrupt();
  99. } else if (whiteTime < 0) {
  100. JOptionPane.showMessageDialog(this, "白方时间已到,黑方获胜!");
  101. timeThread.interrupt();
  102. }
  103. }
  104. }
  105.  
  106. /**
  107. * 初始化右边操作界面
  108. */
  109. private void initOeratePane() {
  110. btn_start = new JButton("开始游戏");
  111. btn_settings = new JButton("游戏设置");
  112. btn_exit = new JButton("退出游戏");
  113. btn_start.addActionListener(new ActionListener() {
  114.  
  115. @Override
  116. public void actionPerformed(ActionEvent e) {
  117. int select = JOptionPane.showConfirmDialog(getContentPane(), "确定要重新开始吗?");
  118. if (select == 0) {
  119. reStartGame();
  120. }
  121.  
  122. }
  123. });
  124. btn_settings.addActionListener(new ActionListener() {
  125.  
  126. @Override
  127. public void actionPerformed(ActionEvent e) {
  128. String select = "";
  129. select = JOptionPane.showInputDialog("请输入游戏时间(分钟),输入0不限时间:");
  130. if (select != null && !select.equals("")) {
  131. try {
  132. gametime = Integer.parseInt(select) * 60;
  133. // System.out.println("gametime:" + gametime);
  134. // isLimitTime = true;
  135. // System.out.println("设置isLimitTime--" + isLimitTime);
  136. blackTime = gametime;
  137. whiteTime = gametime;
  138. if (gametime > 0) {
  139. blackmessage = blackTime / 3600 + ":" + blackTime / 60 % 60 + ":" + blackTime % 60;
  140. whitemessage = whiteTime / 3600 + ":" + whiteTime / 60 % 60 + ":" + whiteTime % 60;
  141. // timeThread.resume();
  142. } else {
  143. whitemessage = "无限制";
  144. blackmessage = "无限制";
  145. }
  146. initTime();
  147. repaint();
  148. } catch (Exception e2) {
  149. e2.printStackTrace();
  150. JOptionPane.showMessageDialog(getContentPane(), "请输入正确信息!");
  151. }
  152.  
  153. //
  154. }
  155. }
  156. });
  157. btn_exit.addActionListener(new ActionListener() {
  158.  
  159. @Override
  160. public void actionPerformed(ActionEvent e) {
  161. System.exit(0);
  162. }
  163. });
  164.  
  165. operaterPanel = new JPanel();
  166. GridLayout layout = new GridLayout(0, 1, 100, 100);
  167. operaterPanel.setLayout(layout);
  168. operaterPanel.add(btn_start);
  169. operaterPanel.add(btn_settings);
  170. operaterPanel.add(btn_exit);
  171. getContentPane().add(operaterPanel, BorderLayout.EAST);
  172. }
  173.  
  174. /**
  175. * 重新开始游戏
  176. */
  177. protected void reStartGame() {
  178. isBlack = true;
  179. blackTime = gametime;
  180. whiteTime = gametime;
  181. // for (int i = 0; i < maps[0].length; i++) {
  182. // for (int j = 0; j < maps.length; j++) {
  183. // maps[i][j] = 0;
  184. // }
  185. // }
  186. maps = new int[15][15];
  187. repaint();
  188. }
  189.  
  190. @Override
  191. public void paint(Graphics g1) {
  192. super.paint(g);
  193. // 绘制背景
  194. g.drawImage(bgImage, 20, 90, this);
  195. // 绘制上方标题
  196. g.setColor(Color.black);
  197. g.setFont(new Font("楷体", Font.BOLD, 30));
  198. g.drawString("游戏信息:" + message, 100, 75);
  199. // 绘制下方
  200. g.setColor(Color.gray);
  201. g.fillRect(50, 530, 200, 50);
  202. g.fillRect(300, 530, 200, 50);
  203. g.setColor(Color.black);
  204. g.setFont(new Font("宋体", Font.BOLD, 20));
  205. g.drawString("黑方时间:" + blackmessage, 60, 560);
  206. g.drawString("白方时间:" + whitemessage, 310, 560);
  207. // g.setColor(Color.blue);
  208. // 绘制棋盘线条
  209. for (int i = 0; i < 15; i++) {
  210. g.drawLine(60, 130 + i * chessBoardItemWidth, 410, 130 + i * chessBoardItemWidth);
  211. g.drawLine(60 + i * chessBoardItemWidth, 130, 60 + i * chessBoardItemWidth, 480);
  212. }
  213. // 标注点位
  214. g.fillOval(131, 200, 8, 8);
  215. g.fillOval(331, 200, 8, 8);
  216. g.fillOval(131, 400, 8, 8);
  217. g.fillOval(331, 400, 8, 8);
  218. g.fillOval(230, 299, 10, 10);
  219.  
  220. // 绘制棋子
  221. for (int j = 0; j < maps.length; j++) {
  222. for (int i = 0; i < maps[0].length; i++) {
  223. if (maps[j][i] == 1) {
  224. g.setColor(Color.black);
  225. g.fillOval(50 + i * chessBoardItemWidth, 120 + j * chessBoardItemWidth, 20, 20);
  226. }
  227. if (maps[j][i] == 2) {
  228. g.setColor(Color.white);
  229. g.fillOval(50 + i * chessBoardItemWidth, 120 + j * chessBoardItemWidth, 20, 20);
  230. }
  231. }
  232. }
  233.  
  234. // 双缓冲解决屏幕闪烁
  235. g1.drawImage(offsetImg, 0, 0, this);
  236. }
  237.  
  238. @Override
  239. public void mouseClicked(MouseEvent e) {
  240. mouseX = e.getX();
  241. mouseY = e.getY();
  242. // 鼠标落子
  243. if (chessBoardRect.contains(mouseX, mouseY)) {
  244. mapsX = (mouseX - 50) / chessBoardItemWidth;
  245. mapsY = (mouseY - 120) / chessBoardItemWidth;
  246. // System.out.println("mapsXY:" + mapsX + "," + mapsY);
  247. // maps[mapsY][mapsX] = (isBlack == true ? 1 : 2);
  248.  
  249. if (maps[mapsY][mapsX] == 0) {
  250. if (isBlack) {
  251. maps[mapsY][mapsX] = 1;
  252. isBlack = false;
  253. message = "白色落子";
  254. } else {
  255. maps[mapsY][mapsX] = 2;
  256. isBlack = true;
  257. message = "黑色落子";
  258. }
  259. checkGame();
  260. }
  261.  
  262. }
  263. repaint();
  264. }
  265.  
  266. /**
  267. * 判断游戏是否结束
  268. */
  269. private void checkGame() {
  270. int color = maps[mapsY][mapsX];
  271. boolean isWin = false;
  272.  
  273. // System.out.println("mapsXY:" + mapsX + "," + mapsY);
  274.  
  275. isWin = checkChess(1, 0, color) || checkChess(0, 1, color) || checkChess(1, 1, color)
  276. || checkChess(1, -1, color);
  277. if (isWin) {
  278. if (color == 1)
  279. JOptionPane.showMessageDialog(this, "黑方胜利!");
  280. else {
  281. JOptionPane.showMessageDialog(this, "白方胜利!");
  282. }
  283. reStartGame();
  284. // new GameWindow();
  285. }
  286.  
  287. }
  288.  
  289. /**
  290. * @param xChange 只能是1,0,-1
  291. * @param yChange 只能是1,0,-1
  292. * @param color
  293. */
  294. private boolean checkChess(int xChange, int yChange, int color) {
  295. boolean isWin = false;
  296.  
  297. int count = 1, tempX = xChange, tempY = yChange;
  298. while ((mapsX + tempX) >= 0 && (mapsX + tempX) < 15 && (mapsY + tempY) >= 0 && (mapsY + tempY) < 15
  299. && maps[mapsY + tempY][mapsX + tempX] == color) {
  300. count++;
  301. if (tempX == 0 && tempY == 0)
  302. break;
  303. if (tempX > 0)
  304. tempX++;
  305. if (tempX < 0)
  306. tempX--;
  307. if (tempY > 0)
  308. tempY++;
  309. if (tempY < 0)
  310. tempY--;
  311. }
  312. tempX = xChange;
  313. tempY = yChange;
  314. while ((mapsX - tempX) >= 0 && (mapsX - tempX) < 15 && (mapsY - tempY) >= 0 && (mapsY - tempY) < 15
  315. && maps[mapsY - tempY][mapsX - tempX] == color) {
  316. count++;
  317. if (tempX == 0 && tempY == 0)
  318. break;
  319. if (tempX > 0)
  320. tempX++;
  321. if (tempX < 0)
  322. tempX--;
  323. if (tempY > 0)
  324. tempY++;
  325. if (tempY < 0)
  326. tempY--;
  327. }
  328. // System.out.println("count:" + count);
  329. if (count >= 5) {
  330. return true;
  331. }
  332. return isWin;
  333. }
  334.  
  335. @Override
  336. public void mousePressed(MouseEvent e) {
  337.  
  338. }
  339.  
  340. @Override
  341. public void mouseReleased(MouseEvent e) {
  342.  
  343. }
  344.  
  345. @Override
  346. public void mouseEntered(MouseEvent e) {
  347. // mouseX = e.getX();
  348. // mouseY = e.getY();
  349. // System.out.println("鼠标进入游戏窗口");
  350. // System.out.println("鼠标坐标:" + mouseX + "," + mouseY);
  351. // if (chessBoardRect.contains(mouseX, mouseY)) {
  352. // System.out.println("进入棋盘");
  353. // if (isBlack) {
  354. // g.setColor(Color.black);
  355. // } else {
  356. // g.setColor(Color.white);
  357. // }
  358. // g.fillOval(mouseX, mouseY, 20, 20);
  359. // repaint();
  360. // }
  361. }
  362.  
  363. @Override
  364. public void mouseExited(MouseEvent e) {
  365.  
  366. }
  367.  
  368. @Override
  369. public void run() {
  370. while (true) {
  371. // System.out.println("isblack:" + isBlack);
  372. if (isBlack) {
  373. blackTime--;
  374. } else {
  375. whiteTime--;
  376. }
  377. blackmessage = blackTime / 3600 + ":" + blackTime / 60 % 60 + ":" + blackTime % 60;
  378. whitemessage = whiteTime / 3600 + ":" + whiteTime / 60 % 60 + ":" + whiteTime % 60;
  379. // System.out.println("blackTime:" + blackTime);
  380. // System.out.println("whiteTime:" + whiteTime);
  381. repaint();
  382. if (blackTime < 0) {
  383. JOptionPane.showMessageDialog(getContentPane(), "黑方时间已到,白方获胜!");
  384. timeThread.interrupt();
  385. new GameWindow();
  386. break;
  387. } else if (whiteTime < 0) {
  388. JOptionPane.showMessageDialog(getContentPane(), "白方时间已到,黑方获胜!");
  389. timeThread.interrupt();
  390. new GameWindow();
  391. break;
  392. }
  393. try {
  394. Thread.sleep(1000);
  395. } catch (InterruptedException e) {
  396. e.printStackTrace();
  397. }
  398. }
  399. }
  400.  
  401. }

背景图片

java swing实现简单的五子棋游戏

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/Jayce27/article/details/114266573

延伸 · 阅读

精彩推荐