技术标签: java实现五子棋窗口
今天给大家介绍一下如何用Java swing实现五子棋的开发即用Java开发图形界面程序五子棋,代码由于太多,只贴部分,最下面会附上下载地址,废话不多说,下面我们先看一下运行结果:
接下来我们看代码:
首先是创建主frame框架界面:
package org.liky.game.frame;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
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.JFrame;
import javax.swing.JOptionPane;
public class FiveChessFrame extends JFrame implements MouseListener, Runnable {
// 取得屏幕的宽度
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
// 取得屏幕的高度
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
// 背景图片
BufferedImage bgImage = null;
// 保存棋子的坐标
int x = 0;
int y = 0;
// 保存之前下过的全部棋子的坐标
// 其中数据内容 0: 表示这个点并没有棋子, 1: 表示这个点是黑子, 2:表示这个点是白子
int[][] allChess = new int[19][19];
// 标识当前应该黑棋还是白棋下下一步
boolean isBlack = true;
// 标识当前游戏是否可以继续
boolean canPlay = true;
// 保存显示的提示信息
String message = "黑方先行";
// 保存最多拥有多少时间(秒)
int maxTime = 0;
// 做倒计时的线程类
Thread t = new Thread(this);
// 保存黑方与白方的剩余时间
int blackTime = 0;
int whiteTime = 0;
// 保存双方剩余时间的显示信息
String blackMessage = "无限制";
String whiteMessage = "无限制";
public FiveChessFrame() {
// 设置标题
this.setTitle("五子棋");
// 设置窗体大小
this.setSize(500, 500);
// 设置窗体出现位置
this.setLocation((width - 500) / 2, (height - 500) / 2);
// 将窗体设置为大小不可改变
this.setResizable(false);
// 将窗体的关闭方式设置为默认关闭后程序结束
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 为窗体加入监听器
this.addMouseListener(this);
// 将窗体显示出来
this.setVisible(true);
t.start();
t.suspend();
// 刷新屏幕,防止开始游戏时出现无法显示的情况.
this.repaint();
String imagePath = "" ;
try {
imagePath = System.getProperty("user.dir")+"/src/image/background.jpg" ;
bgImage = ImageIO.read(new File(imagePath.replaceAll("\\\\", "/")));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void paint(Graphics g) {
// 双缓冲技术防止屏幕闪烁
BufferedImage bi = new BufferedImage(500, 500,
BufferedImage.TYPE_INT_RGB);
Graphics g2 = bi.createGraphics();
g2.setColor(Color.BLACK);
// 绘制背景
g2.drawImage(bgImage, 1, 20, this);
// 输出标题信息
g2.setFont(new Font("黑体", Font.BOLD, 20));
g2.drawString("游戏信息:" + message, 130, 60);
// 输出时间信息
g2.setFont(new Font("宋体", 0, 14));
g2.drawString("黑方时间:" + blackMessage, 30, 470);
g2.drawString("白方时间:" + whiteMessage, 260, 470);
// 绘制棋盘
for (int i = 0; i < 19; i++) {
g2.drawLine(10, 70 + 20 * i, 370, 70 + 20 * i);
g2.drawLine(10 + 20 * i, 70, 10 + 20 * i, 430);
}
// 标注点位
g2.fillOval(68, 128, 4, 4);
g2.fillOval(308, 128, 4, 4);
g2.fillOval(308, 368, 4, 4);
g2.fillOval(68, 368, 4, 4);
g2.fillOval(308, 248, 4, 4);
g2.fillOval(188, 128, 4, 4);
g2.fillOval(68, 248, 4, 4);
g2.fillOval(188, 368, 4, 4);
g2.fillOval(188, 248, 4, 4);
/*
* //绘制棋子 x = (x - 10) / 20 * 20 + 10 ; y = (y - 70) / 20 * 20 + 70 ;
* //黑子 g.fillOval(x - 7, y - 7, 14, 14); //白子 g.setColor(Color.WHITE) ;
* g.fillOval(x - 7, y - 7, 14, 14); g.setColor(Color.BLACK) ;
* g.drawOval(x - 7, y - 7, 14, 14);
*/
// 绘制全部棋子
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
if (allChess[i][j] == 1) {
// 黑子
int tempX = i * 20 + 10;
int tempY = j * 20 + 70;
g2.fillOval(tempX - 7, tempY - 7, 14, 14);
}
if (allChess[i][j] == 2) {
// 白子
int tempX = i * 20 + 10;
int tempY = j * 20 + 70;
g2.setColor(Color.WHITE);
g2.fillOval(tempX - 7, tempY - 7, 14, 14);
g2.setColor(Color.BLACK);
g2.drawOval(tempX - 7, tempY - 7, 14, 14);
}
}
}
g.drawImage(bi, 0, 0, this);
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
/*
* System.out.println("X:"+e.getX()); System.out.println("Y:"+e.getY());
*/
if (canPlay == true) {
x = e.getX();
y = e.getY();
if (x >= 10 && x <= 370 && y >= 70 && y <= 430) {
x = (x - 10) / 20;
y = (y - 70) / 20;
if (allChess[x][y] == 0) {
// 判断当前要下的是什么颜色的棋子
if (isBlack == true) {
allChess[x][y] = 1;
isBlack = false;
message = "轮到白方";
} else {
allChess[x][y] = 2;
isBlack = true;
message = "轮到黑方";
}
// 判断这个棋子是否和其他的棋子连成5连,即判断游戏是否结束
boolean winFlag = this.checkWin();
if (winFlag == true) {
JOptionPane.showMessageDialog(this, "游戏结束,"
+ (allChess[x][y] == 1 ? "黑方" : "白方") + "获胜!");
canPlay = false;
}
} else {
JOptionPane.showMessageDialog(this, "当前位置已经有棋子,请重新落子!");
}
this.repaint();
}
}
/* System.out.println(e.getX() + " -- " + e.getY()); */
// 点击 开始游戏 按钮
if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 70
&& e.getY() <= 100) {
int result = JOptionPane.showConfirmDialog(this, "是否重新开始游戏?");
if (result == 0) {
// 现在重新开始游戏
// 重新开始所要做的操作: 1)把棋盘清空,allChess这个数组中全部数据归0.
// 2) 将 游戏信息: 的显示改回到开始位置
// 3) 将下一步下棋的改为黑方
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
allChess[i][j] = 0;
}
}
// 另一种方式 allChess = new int[19][19];
message = "黑方先行";
isBlack = true;
blackTime = maxTime;
whiteTime = maxTime;
if (maxTime > 0) {
blackMessage = maxTime / 3600 + ":"
+ (maxTime / 60 - maxTime / 3600 * 60) + ":"
+ (maxTime - maxTime / 60 * 60);
whiteMessage = maxTime / 3600 + ":"
+ (maxTime / 60 - maxTime / 3600 * 60) + ":"
+ (maxTime - maxTime / 60 * 60);
t.resume();
} else {
blackMessage = "无限制";
whiteMessage = "无限制";
}
this.canPlay = true;
this.repaint();
}
}
// 点击 游戏设置 按钮
if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 120
&& e.getY() <= 150) {
String input = JOptionPane
.showInputDialog("请输入游戏的最大时间(单位:分钟),如果输入0,表示没有时间限制:");
try {
maxTime = Integer.parseInt(input) * 60;
if (maxTime < 0) {
JOptionPane.showMessageDialog(this, "请输入正确信息,不允许输入负数!");
}
if (maxTime == 0) {
int result = JOptionPane.showConfirmDialog(this,
"设置完成,是否重新开始游戏?");
if (result == 0) {
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
allChess[i][j] = 0;
}
}
// 另一种方式 allChess = new int[19][19];
message = "黑方先行";
isBlack = true;
blackTime = maxTime;
whiteTime = maxTime;
blackMessage = "无限制";
whiteMessage = "无限制";
this.canPlay = true;
this.repaint();
}
}
if (maxTime > 0) {
int result = JOptionPane.showConfirmDialog(this,
"设置完成,是否重新开始游戏?");
if (result == 0) {
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
allChess[i][j] = 0;
}
}
// 另一种方式 allChess = new int[19][19];
message = "黑方先行";
isBlack = true;
blackTime = maxTime;
whiteTime = maxTime;
blackMessage = maxTime / 3600 + ":"
+ (maxTime / 60 - maxTime / 3600 * 60) + ":"
+ (maxTime - maxTime / 60 * 60);
whiteMessage = maxTime / 3600 + ":"
+ (maxTime / 60 - maxTime / 3600 * 60) + ":"
+ (maxTime - maxTime / 60 * 60);
t.resume();
this.canPlay = true;
this.repaint();
}
}
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(this, "请正确输入信息!");
}
}
// 点击 游戏说明 按钮
if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 170
&& e.getY() <= 200) {
JOptionPane.showMessageDialog(this,
"这个一个五子棋游戏程序,黑白双方轮流下棋,当某一方连到五子时,游戏结束。");
}
// 点击 认输 按钮
if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 270
&& e.getY() <= 300) {
int result = JOptionPane.showConfirmDialog(this, "是否确认认输?");
if (result == 0) {
if (isBlack) {
JOptionPane.showMessageDialog(this, "黑方已经认输,游戏结束!");
} else {
JOptionPane.showMessageDialog(this, "白方已经认输,游戏结束!");
}
canPlay = false;
}
}
// 点击 关于 按钮
if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 320
&& e.getY() <= 350) {
JOptionPane.showMessageDialog(this,
"本游戏由MLDN制作,有相关问题可以访问www.mldn.cn");
}
// 点击 退出 按钮
if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 370
&& e.getY() <= 400) {
JOptionPane.showMessageDialog(this, "游戏结束");
System.exit(0);
}
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
private boolean checkWin() {
boolean flag = false;
// 保存共有相同颜色多少棋子相连
int count = 1;
// 判断横向是否有5个棋子相连,特点 纵坐标 是相同, 即allChess[x][y]中y值是相同
int color = allChess[x][y];
/*
* if (color == allChess[x+1][y]) { count++; if (color ==
* allChess[x+2][y]) { count++; if (color == allChess[x+3][y]) {
* count++; } } }
*/
// 通过循环来做棋子相连的判断
/*
* int i = 1; while (color == allChess[x + i][y + 0]) { count++; i++; }
* i = 1; while (color == allChess[x - i][y - 0]) { count++; i++; } if
* (count >= 5) { flag = true; } // 纵向的判断 int i2 = 1 ; int count2 = 1 ;
* while (color == allChess[x + 0][y + i2]) { count2++; i2++; } i2 = 1;
* while (color == allChess[x - 0][y - i2]) { count2++; i2++; } if
* (count2 >= 5) { flag = true ; } // 斜方向的判断(右上 + 左下) int i3 = 1 ; int
* count3 = 1 ; while (color == allChess[x + i3][y - i3]) { count3++;
* i3++; } i3 = 1; while (color == allChess[x - i3][y + i3]) { count3++;
* i3++; } if (count3 >= 5) { flag = true ; } // 斜方向的判断(右下 + 左上) int i4 =
* 1 ; int count4 = 1 ; while (color == allChess[x + i4][y + i4]) {
* count4++; i4++; } i4 = 1; while (color == allChess[x - i4][y - i4]) {
* count4++; i4++; } if (count4 >= 5) { flag = true ; }
*/
// 判断横向
count = this.checkCount(1, 0, color);
if (count >= 5) {
flag = true;
} else {
// 判断纵向
count = this.checkCount(0, 1, color);
if (count >= 5) {
flag = true;
} else {
// 判断右上、左下
count = this.checkCount(1, -1, color);
if (count >= 5) {
flag = true;
} else {
// 判断右下、左上
count = this.checkCount(1, 1, color);
if (count >= 5) {
flag = true;
}
}
}
}
return flag;
}
// 判断棋子连接的数量
private int checkCount(int xChange, int yChange, int color) {
int count = 1;
int tempX = xChange;
int tempY = yChange;
while (x + xChange >= 0 && x + xChange <= 18 && y + yChange >= 0
&& y + yChange <= 18
&& color == allChess[x + xChange][y + yChange]) {
count++;
if (xChange != 0)
xChange++;
if (yChange != 0) {
if (yChange > 0)
yChange++;
else {
yChange--;
}
}
}
xChange = tempX;
yChange = tempY;
while (x - xChange >= 0 && x - xChange <= 18 && y - yChange >= 0
&& y - yChange <= 18
&& color == allChess[x - xChange][y - yChange]) {
count++;
if (xChange != 0)
xChange++;
if (yChange != 0) {
if (yChange > 0)
yChange++;
else {
yChange--;
}
}
}
return count;
}
public void run() {
// TODO Auto-generated method stub
// 判断是否有时间限制
if (maxTime > 0) {
while (true) {
if (isBlack) {
blackTime--;
if (blackTime == 0) {
JOptionPane.showMessageDialog(this, "黑方超时,游戏结束!");
}
} else {
whiteTime--;
if (whiteTime == 0) {
JOptionPane.showMessageDialog(this, "白方超时,游戏结束!");
}
}
blackMessage = blackTime / 3600 + ":"
+ (blackTime / 60 - blackTime / 3600 * 60) + ":"
+ (blackTime - blackTime / 60 * 60);
whiteMessage = whiteTime / 3600 + ":"
+ (whiteTime / 60 - whiteTime / 3600 * 60) + ":"
+ (whiteTime - whiteTime / 60 * 60);
this.repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(blackTime + " -- " + whiteTime);
}
}
}
}
然后再创建子框架界面:
package org.liky.game.frame;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
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.JFrame;
public class MyChessFrame extends JFrame implements MouseListener {
public MyChessFrame() {
this.setTitle("五子");
this.setSize(1000, 700);
this.setResizable(false) ;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
/*System.out.println("宽度为: "+width);
System.out.println("高度为: "+height);*/
this.setLocation((width - 200)/2, (height-100)/2);
this.addMouseListener(this);
this.setVisible(true);
}
public void paint(Graphics g) {
/*g.drawString("五子棋游戏", 20, 40);*/
/*g.drawOval(20, 40, 40, 40);*/
/*g.fillOval(20, 40, 40, 40);*/
/*g.drawLine(20, 40, 80, 40);*/
/* g.drawRect(20, 40, 40, 20);
g.fillRect(80, 40, 40, 20);*/
BufferedImage image = null;
try {
image = ImageIO.read(new File("E:/image/MLDN文字01.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g.drawImage(image,0,0,this);
g.drawOval(20, 40, 40, 40);
g.setColor(Color.BLUE);
g.fillRect(80, 40, 40, 20);
g.setFont(new Font("宋体",40,40));
g.drawString("五子棋游戏", 20, 100);
}
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
文章浏览阅读1k次,点赞2次,收藏2次。在网页中实现视频课程功能,重点是要能够保存用户的观看进度。比如,用户观看一个视频,这个视频看到2秒,这时用户离开此页面,下次打开这个视频,这个视频就要从2秒开始播放。_video currenttime
文章浏览阅读1.7w次,点赞11次,收藏26次。报错的原因:通俗解释:本地的JDK版本和IDEA默认的JDK不匹配三、解决办法:把这个默认使用的JDK5版本改成我们当前使用的JDK的版本就好了。在maven web项目上面要部署运行tomcat时候,会出现这个问题。(1)官方解释:这个错误消息表明你正在使用的Java版本过低,不能支持程序的运行。具体来说,你正在使用的版本是5,而程序需要更高版本的Java才能运行。建议更新Java到较新版本,以确保程序能够运行_java: 错误: 不支持发行版本 5
文章浏览阅读379次,点赞5次,收藏10次。问题是学习的先导,教师的有效提问能够激发学生的学习兴趣,调动学生积极思考,当提问指向疑惑处、关键处和思考处时,既为学生指明思考的方向,提供思考的空间,又能引发学生深入思考,提升学习的深度和广度。在《摸球游戏》一课的教学中,一位男生连着6次都摸出了黄球(盒内装有形状、大小相同,质量相等的球,每次摸球都摇匀再摸),学生一片欢呼,都认为他再次摸出的一定也是黄球。聚焦学生的思考处,教师通过提问引发学生深度思考,不仅帮助学生充分利用已有的旧知来学习新知,而且使学生学会学习,有利于学生在今后的数学学习中进行运用。
文章浏览阅读1.2k次。解决jupyter一直显示内核准备中....maybe helpful:)_jupyter内核正在启动,请等待
文章浏览阅读1.4k次。一、数据流分析1.1 介绍原理:数据流分析的原理可参考这些文章 Useful Literature。说明:phasar提供了很多复杂机制,以便于用户自定义数据流分析。用户针对自己的需求,选择最合适的接口,用户只需要提供一个新类,即可实现该接口缺少的功能,该功能可作为分析的问题描述。将具体问题描述交给求解器求解,以下将列出多种数据流求解器。(1)选择控制流图首先需选择一种控制流图,以基于此图进行分析,如CFG (intra-procedural control-flow graph) 或 ICFG _phasar框架
文章浏览阅读1.8k次。声明:文中所涉及的技术、思路和工具仅供以安全为目的的学习交流使用,任何人不得将其用于非法用途以及盈利等目的,否则后果自行承担!Metasploit实验:制作免杀payload+对任意“外网”主机的远控这个试验的实现需要拆分成下面三个模块:内网穿透到外网制作免杀木马后门Metasploit控制客户机环境准备操作系统用途IP地址需要软件Kali制作payl..._metasploit内网穿透
文章浏览阅读2.2k次。关系型数据库与图数据库比较关系型数据库是基于实体建模理念设计。 该设计理念并没有提供对这些实体间关系的直接支持。 在需要描述这些实体之间的关系时,我们常常需要创建一个关联表以记录这些数据之间的关联关系,而且这些关联表常常不用来记录除外键之外的其它数据。也就是说,这些关联表也仅仅是通过关系型数据库所已有的功能来模拟实体之间的关系。 这种模拟导致了两个非常糟糕的结果:数据库需要通过关联表间接地维护实..._关系型数据库 图数据库
文章浏览阅读1w次,点赞12次,收藏13次。vue3.0发布后,在双向数据绑定这里,使用proxy代替了object.defineProperty,众所周知,obj.defineProperty是对对象属性监听,循环对象,一个个属性监听,proxy是对一整个对象进行监听。而proxy的一大优势就是可以监听数组。此帖用来记录自己对proxy的调研理解。1,proxy的基本理解proxy是es6中的语法,最为代理的功能,当外接要访问对象的时候,proxy可以做一层拦截,并且可以对这个proxy代理进行操作,进而实现对对象的操作。如:var prox_proxy为什么能监听数组变化
文章浏览阅读3.3w次。下载地址_my app电脑版
文章浏览阅读7.4k次,点赞7次,收藏74次。背景:网络传输层中,一般采用TCP协议,如果要建立连接,客户端会先发送syn包请求,服务器接收,接收后,再传递给客户端ack,syn包,这个时候,客户端再次回应,传回ack包。可是问题就出在,如果我是客户端,只发送一个请求syn包,然后第三次握手的时候,不再往回传ack包,此时服务端是不是就要等待?在等待的时间里,我是不是可以伪造更多的请求,从而不断消耗服务器的资源,..._怎么用python给tcp协议的ip和端口做ddos
文章浏览阅读476次,点赞14次,收藏10次。先给友友们交代一下个人背景:楼主22年毕业于一所985,大三下的时候在字节跳动实习过小半年,毕业后选了阿里巴巴的Offer,担任前端开发工程师的职位,尔来也有1年半的光载。
文章浏览阅读1.2k次,点赞2次,收藏6次。【代码】[Errno 2] No such file or directory: ‘:/usr/local/cuda-11.7:/usr/local/cuda-11.7/bin/nvcc‘_warnings.warn(msg.format('we could not find ninja.'))