电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

java swing實現(xiàn)簡單的五子棋游戲

瀏覽:11日期:2022-08-15 18:53:09

用java swing寫的一個簡單的五子棋游戲。

java swing實現(xiàn)簡單的五子棋游戲

下面是Main.java。

package com.crossing.main;import com.crossing.view.GameWindow;public class Main { /** * @param args */ public static void main(String[] args) { GameWindow gameWindow = new GameWindow(); }}

下面是GameWindow.java。

package com.crossing.view;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.GridLayout;import java.awt.Rectangle;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;/** * @Title: GameWindow.java * @Package com.crossing.view * @Description: TODO(用一句話描述該文件做什么) * @author crossing * @date 2021年2月28日 下午9:23:14 * @version V1.0 */@SuppressWarnings('serial')public class GameWindow extends JFrame implements MouseListener, Runnable { private int width, height;// 屏幕寬高 private int mouseX = 0, mouseY = 0, mapsX = 0, mapsY = 0;// 鼠標(biāo)坐標(biāo),鼠標(biāo)在地圖中的位置 private int game_width = 600, game_height = 600;// 游戲窗口大小 private BufferedImage bgImage = null;// 背景圖片 private int chessBoardItemWidth = 25;// 棋盤每一小格的大小 private Rectangle chessBoardRect = null;// 棋盤所在矩形 private BufferedImage offsetImg = new BufferedImage(game_width, game_height, BufferedImage.TYPE_4BYTE_ABGR); private Graphics g = offsetImg.getGraphics();// 雙緩沖解決閃爍問題 private int[][] maps = new int[15][15];// 0無棋子,1黑子,2白子 private boolean isBlack = true;// 是否是黑方的回合 private String message = '黑方先行', whitemessage = '無限制', blackmessage = '無限制';// 界面上方信息,下方時間信息 // 右邊操作界面 private JButton btn_start, btn_exit, btn_settings; private JPanel operaterPanel;// 操作面板 private int gametime = 0;// 游戲時間限制(秒) private int blackTime = 0, whiteTime = 0;// 黑白方剩余時間 private Thread timeThread = new Thread(this);// 黑白雙方倒計時線程// private boolean isLimitTime = false; public GameWindow() { setTitle('五子棋'); setSize(game_width, game_height); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 獲取屏幕寬高 width = Toolkit.getDefaultToolkit().getScreenSize().width; height = Toolkit.getDefaultToolkit().getScreenSize().height; // 棋盤位置矩形 chessBoardRect = new Rectangle(50, 120, 370, 370); setLocation((width - game_width) / 2, (height - game_height) / 2); addMouseListener(this); // 初始化右邊的面板 initOeratePane(); repaint(); // 設(shè)置背景 try { bgImage = ImageIO.read(new File('img/backgroung.png'));// System.out.println(bgImage); } catch (IOException e) { e.printStackTrace(); } setVisible(true); } /** * 初始化黑白雙方時間 */ private void initTime() {// System.out.println('isLimitTime:' + isLimitTime); if (gametime > 0) { timeThread.start(); if (blackTime < 0) { JOptionPane.showMessageDialog(this, '黑方時間已到,白方獲勝!'); timeThread.interrupt(); } else if (whiteTime < 0) { JOptionPane.showMessageDialog(this, '白方時間已到,黑方獲勝!'); timeThread.interrupt(); } } } /** * 初始化右邊操作界面 */ private void initOeratePane() { btn_start = new JButton('開始游戲'); btn_settings = new JButton('游戲設(shè)置'); btn_exit = new JButton('退出游戲'); btn_start.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int select = JOptionPane.showConfirmDialog(getContentPane(), '確定要重新開始嗎?'); if (select == 0) { reStartGame(); } } }); btn_settings.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String select = ''; select = JOptionPane.showInputDialog('請輸入游戲時間(分鐘),輸入0不限時間:'); if (select != null && !select.equals('')) { try { gametime = Integer.parseInt(select) * 60;// System.out.println('gametime:' + gametime);// isLimitTime = true;// System.out.println('設(shè)置isLimitTime--' + isLimitTime); blackTime = gametime; whiteTime = gametime; if (gametime > 0) { blackmessage = blackTime / 3600 + ':' + blackTime / 60 % 60 + ':' + blackTime % 60; whitemessage = whiteTime / 3600 + ':' + whiteTime / 60 % 60 + ':' + whiteTime % 60;// timeThread.resume(); } else { whitemessage = '無限制'; blackmessage = '無限制'; } initTime(); repaint(); } catch (Exception e2) { e2.printStackTrace(); JOptionPane.showMessageDialog(getContentPane(), '請輸入正確信息!'); }// } } }); btn_exit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); operaterPanel = new JPanel(); GridLayout layout = new GridLayout(0, 1, 100, 100); operaterPanel.setLayout(layout); operaterPanel.add(btn_start); operaterPanel.add(btn_settings); operaterPanel.add(btn_exit); getContentPane().add(operaterPanel, BorderLayout.EAST); } /** * 重新開始游戲 */ protected void reStartGame() { isBlack = true; blackTime = gametime; whiteTime = gametime;// for (int i = 0; i < maps[0].length; i++) {// for (int j = 0; j < maps.length; j++) {// maps[i][j] = 0;// }// } maps = new int[15][15]; repaint(); } @Override public void paint(Graphics g1) { super.paint(g); // 繪制背景 g.drawImage(bgImage, 20, 90, this); // 繪制上方標(biāo)題 g.setColor(Color.black); g.setFont(new Font('楷體', Font.BOLD, 30)); g.drawString('游戲信息:' + message, 100, 75); // 繪制下方 g.setColor(Color.gray); g.fillRect(50, 530, 200, 50); g.fillRect(300, 530, 200, 50); g.setColor(Color.black); g.setFont(new Font('宋體', Font.BOLD, 20)); g.drawString('黑方時間:' + blackmessage, 60, 560); g.drawString('白方時間:' + whitemessage, 310, 560);// g.setColor(Color.blue); // 繪制棋盤線條 for (int i = 0; i < 15; i++) { g.drawLine(60, 130 + i * chessBoardItemWidth, 410, 130 + i * chessBoardItemWidth); g.drawLine(60 + i * chessBoardItemWidth, 130, 60 + i * chessBoardItemWidth, 480); } // 標(biāo)注點位 g.fillOval(131, 200, 8, 8); g.fillOval(331, 200, 8, 8); g.fillOval(131, 400, 8, 8); g.fillOval(331, 400, 8, 8); g.fillOval(230, 299, 10, 10); // 繪制棋子 for (int j = 0; j < maps.length; j++) { for (int i = 0; i < maps[0].length; i++) { if (maps[j][i] == 1) { g.setColor(Color.black); g.fillOval(50 + i * chessBoardItemWidth, 120 + j * chessBoardItemWidth, 20, 20); } if (maps[j][i] == 2) { g.setColor(Color.white); g.fillOval(50 + i * chessBoardItemWidth, 120 + j * chessBoardItemWidth, 20, 20); } } } // 雙緩沖解決屏幕閃爍 g1.drawImage(offsetImg, 0, 0, this); } @Override public void mouseClicked(MouseEvent e) { mouseX = e.getX(); mouseY = e.getY(); // 鼠標(biāo)落子 if (chessBoardRect.contains(mouseX, mouseY)) { mapsX = (mouseX - 50) / chessBoardItemWidth; mapsY = (mouseY - 120) / chessBoardItemWidth;// System.out.println('mapsXY:' + mapsX + ',' + mapsY);// maps[mapsY][mapsX] = (isBlack == true ? 1 : 2); if (maps[mapsY][mapsX] == 0) { if (isBlack) { maps[mapsY][mapsX] = 1; isBlack = false; message = '白色落子'; } else { maps[mapsY][mapsX] = 2; isBlack = true; message = '黑色落子'; } checkGame(); } } repaint(); } /** * 判斷游戲是否結(jié)束 */ private void checkGame() { int color = maps[mapsY][mapsX]; boolean isWin = false;// System.out.println('mapsXY:' + mapsX + ',' + mapsY); isWin = checkChess(1, 0, color) || checkChess(0, 1, color) || checkChess(1, 1, color) || checkChess(1, -1, color); if (isWin) { if (color == 1) JOptionPane.showMessageDialog(this, '黑方勝利!'); else { JOptionPane.showMessageDialog(this, '白方勝利!'); } reStartGame();// new GameWindow(); } } /** * @param xChange 只能是1,0,-1 * @param yChange 只能是1,0,-1 * @param color */ private boolean checkChess(int xChange, int yChange, int color) { boolean isWin = false; int count = 1, tempX = xChange, tempY = yChange; while ((mapsX + tempX) >= 0 && (mapsX + tempX) < 15 && (mapsY + tempY) >= 0 && (mapsY + tempY) < 15 && maps[mapsY + tempY][mapsX + tempX] == color) { count++; if (tempX == 0 && tempY == 0) break; if (tempX > 0) tempX++; if (tempX < 0) tempX--; if (tempY > 0) tempY++; if (tempY < 0) tempY--; } tempX = xChange; tempY = yChange; while ((mapsX - tempX) >= 0 && (mapsX - tempX) < 15 && (mapsY - tempY) >= 0 && (mapsY - tempY) < 15 && maps[mapsY - tempY][mapsX - tempX] == color) { count++; if (tempX == 0 && tempY == 0) break; if (tempX > 0) tempX++; if (tempX < 0) tempX--; if (tempY > 0) tempY++; if (tempY < 0) tempY--; }// System.out.println('count:' + count); if (count >= 5) { return true; } return isWin; } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) {// mouseX = e.getX();// mouseY = e.getY();// System.out.println('鼠標(biāo)進(jìn)入游戲窗口');// System.out.println('鼠標(biāo)坐標(biāo):' + mouseX + ',' + mouseY);// if (chessBoardRect.contains(mouseX, mouseY)) {// System.out.println('進(jìn)入棋盤');// if (isBlack) {// g.setColor(Color.black);// } else {// g.setColor(Color.white);// }// g.fillOval(mouseX, mouseY, 20, 20);// repaint();// } } @Override public void mouseExited(MouseEvent e) { } @Override public void run() { while (true) {// System.out.println('isblack:' + isBlack); if (isBlack) { blackTime--; } else { whiteTime--; } blackmessage = blackTime / 3600 + ':' + blackTime / 60 % 60 + ':' + blackTime % 60; whitemessage = whiteTime / 3600 + ':' + whiteTime / 60 % 60 + ':' + whiteTime % 60;// System.out.println('blackTime:' + blackTime);// System.out.println('whiteTime:' + whiteTime); repaint(); if (blackTime < 0) { JOptionPane.showMessageDialog(getContentPane(), '黑方時間已到,白方獲勝!'); timeThread.interrupt(); new GameWindow(); break; } else if (whiteTime < 0) { JOptionPane.showMessageDialog(getContentPane(), '白方時間已到,黑方獲勝!'); timeThread.interrupt(); new GameWindow(); break; } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }}

背景圖片

java swing實現(xiàn)簡單的五子棋游戲

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 99文库_实习生实用的范文资料文库站| TPE塑胶原料-PPA|杜邦pom工程塑料、PPSU|PCTG材料、PC/PBT价格-悦诚塑胶 | 智成电子深圳tdk一级代理-提供TDK电容电感贴片蜂鸣器磁芯lambda电源代理经销,TDK代理商有哪些TDK一级代理商排名查询。-深圳tdk一级代理 | 丹佛斯变频器-Danfoss战略代理经销商-上海津信变频器有限公司 | 气动球阀_衬氟蝶阀_调节阀_电动截止阀_上海沃托阀门有限公司 | 塑胶跑道_学校塑胶跑道_塑胶球场_运动场材料厂家_中国塑胶跑道十大生产厂家_混合型塑胶跑道_透气型塑胶跑道-广东绿晨体育设施有限公司 | 防爆型气象站_农业气象站_校园气象站_农业四情监测系统「山东万象环境科技有限公司」 | 米顿罗计量泵(科普)——韬铭机械 | 武汉创亿电气设备有限公司_电力检测设备生产厂家 | AGV叉车|无人叉车|AGV智能叉车|AGV搬运车-江西丹巴赫机器人股份有限公司 | 粉末包装机-给袋式包装机-全自动包装机-颗粒-液体-食品-酱腌菜包装机生产线【润立机械】 | LINK FASHION 童装·青少年装展| Safety light curtain|Belt Sway Switches|Pull Rope Switch|ultrasonic flaw detector-Shandong Zhuoxin Machinery Co., Ltd | 山楂片_雪花_迷你山楂片_山楂条饼厂家-青州市丰源食品厂 | 多功能干燥机,过滤洗涤干燥三合一设备-无锡市张华医药设备有限公司 | 火锅加盟_四川成都火锅店加盟_中国火锅连锁品牌十强_朝天门火锅【官网】 | 桂林腻子粉_内墙外墙抗裂砂浆腻子粉推荐广西鑫达涂料厂家供应 | 聚丙烯酰胺PAM-聚合氯化铝PAC-絮凝剂-河南博旭环保科技有限公司 巨野电机维修-水泵维修-巨野县飞宇机电维修有限公司 | 金属抛光机-磁悬浮抛光机-磁力研磨机-磁力清洗机 - 苏州冠古科技 | 上海质量认证办理中心| 上海办公室装修,办公楼装修设计,办公空间设计,企业展厅设计_写艺装饰公司 | 桑茶-七彩贝壳桑叶茶 长寿茶 | 深圳激光打标机_激光打标机_激光焊接机_激光切割机_同体激光打标机-深圳市创想激光科技有限公司 深圳快餐店设计-餐饮设计公司-餐饮空间品牌全案设计-深圳市勤蜂装饰工程 | 耐磨陶瓷,耐磨陶瓷管道_厂家-淄博拓创陶瓷科技 | 知网论文检测系统入口_论文查重免费查重_中国知网论文查询_学术不端检测系统 | 多功能真空滤油机_润滑油全自动滤油机_高效真空滤油机价格-重庆润华通驰 | 礼至家居-全屋定制家具_一站式全屋整装_免费量房设计报价 | 精密钢管,冷拔精密无缝钢管,精密钢管厂,精密钢管制造厂家,精密钢管生产厂家,山东精密钢管厂家 | 橡胶接头|可曲挠橡胶接头|橡胶软接头安装使用教程-上海松夏官方网站 | 山东柳店新能源科技有限公司| IIS7站长之家-站长工具-爱网站请使用IIS7站长综合查询工具,中国站长【WWW.IIS7.COM】 | 食品质构分析仪-氧化诱导分析仪-瞬态法导热系数仪|热冰百科 | 反渗透阻垢剂-缓蚀阻垢剂厂家-循环水处理药剂-山东鲁东环保科技有限公司 | 渣土车电机,太阳能跟踪器电机,蜗轮蜗杆减速电机厂家-淄博传强电机 | 上海橡胶接头_弹簧减震器_金属软接头厂家-上海淞江集团 | 气力输送_输送机械_自动化配料系统_负压吸送_制造主力军江苏高达智能装备有限公司! | 贵州成人高考网_贵州成考网 | 品牌设计_VI设计_电影海报设计_包装设计_LOGO设计-Bacross新越品牌顾问 | 蜂窝块状沸石分子筛-吸附脱硫分子筛-萍乡市捷龙环保科技有限公司 | 包装盒厂家_纸盒印刷_礼品盒定制-济南恒印包装有限公司 | UV固化机_UVLED光固化机_UV干燥机生产厂家-上海冠顶公司专业生产UV固化机设备 |