package org.guoguo.game.frame;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
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 java.io.InputStream;
import java.net.URL;
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;
String imagePath ="";
// 保存棋子的坐标
int x = 0;
int y = 0;
// 保存之前下过的全部棋子的坐标
// 其中数据内容 0: 表示这个点并没有棋子, 1: 表示这个点是黑子, 2:表示这个点是白子
int[][] allChess = new int[18][18];
// 标识当前应该黑棋还是白棋下下一步
boolean isBlack = false;
// 标识当前游戏是否可以继续
boolean canPlay = true;
// 保存显示的提示信息
String message = "白方先行";
// 保存最多拥有多少时间(秒)
int maxTime = 0;
// 做倒计时的线程类
Thread t = new Thread(this);
// 保存黑方与白方的剩余时间
int blackTime = 0;
int whiteTime = 0;
// 保存双方剩余时间的显示信息
String blackMessage = "无限制";
String whiteMessage = "无限制";
String blackMessage0 = "无限制";
String whiteMessage0 = "无限制";
public FiveChessFrame() {
// 设置标题
this.setTitle("五子棋");
// 设置窗体大小
this.setSize(1000, 750);
// 设置窗体出现位置
this.setLocation((width - 1000) / 2, (height - 750) / 2);
// 将窗体设置为大小不可改变
this.setResizable(false);
// 将窗体的关闭方式设置为默认关闭后程序结束
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 为窗体加入监听器
this.addMouseListener(this);
// 将窗体显示出来
this.setVisible(true);
t.start();
t.suspend();
// 刷新屏幕,防止开始游戏时出现无法显示的情况.
this.repaint();
try {
//URL imagePath = this.getClass().getResource("");
//System.out.println(imagePath);
imagePath = System.getProperty("user.dir")+"/bin/image/background.jpg" ;
System.out.println(imagePath);
bgImage = ImageIO.read(new File(imagePath.replaceAll("\\\\", "/")));
} catch (IOException e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
// 双缓冲技术防止屏幕闪烁
BufferedImage bi = new BufferedImage(1000, 750,
BufferedImage.TYPE_INT_RGB);
Graphics g2 = bi.createGraphics();
g2.setColor(Color.WHITE);
// 绘制背景
g2.drawImage(bgImage, 1, 20, this);
// 输出标题信息
g2.setFont(new Font("隶书", Font.BOLD, 20));
g2.drawString("游戏信息:" + message, 428, 83);
g2.setColor(Color.BLACK);
// 输出时间信息
g2.setFont(new Font("宋体", Font.BOLD, 14));
//g2.drawString(maxTime, 128, 315);
g2.drawString(whiteMessage, 128, 360);
g2.drawString(blackMessage0, 128, 315);
g2.setColor(Color.WHITE);
g2.drawString(blackMessage, 830, 365);
g2.drawString(whiteMessage0, 830, 315);
g2.setColor(Color.BLACK);
g2.setFont(new Font("隶书", Font.BOLD, 40));
// 绘制棋盘
for (int i = 0; i < 18; i++) {
g2.drawLine(258, 102 + 30 * i, 767, 102 + 30 * i);
g2.drawLine(258 + 30 * i, 102, 258 + 30 * i, 612);
}
// 标注点位
g2.fillOval(375, 219, 8, 8);
g2.fillOval(645,219, 8, 8);
g2.fillOval(645, 489, 8, 8);
g2.fillOval(375, 489, 8, 8);
// 绘制全部棋子
for (int i = 0; i < 18; i++) {
for (int j = 0; j < 18; j++) {
if (allChess[i][j] == 1) {
// 黑子
int tempX = i * 30 + 258;
int tempY = j * 30 + 102;
g2.fillOval(tempX - 12, tempY - 12, 28, 28);
}
if (allChess[i][j] == 2) {
// 白子
int tempX = i * 30 + 258;
int tempY = j * 30 + 102;
g2.setColor(Color.WHITE);
g2.fillOval(tempX - 12, tempY - 12, 28, 28);
g2.setColor(Color.BLACK);
g2.drawOval(tempX - 12, tempY - 12, 28, 28);
}
}
}
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 >=260 && x <= 767 && y >= 101 && y <= 610) {
x = (x - 260) / 30;
y = (y - 101) / 30;
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) {
if(allChess[x][y]== 1){
message="黑方胜利";
t.suspend();
}
else
{
message="白方胜利";
t.suspend();
}
JOptionPane.showMessageDialog(this, "游戏结束,"
+ (allChess[x][y] == 1 ? "黑方" : "白方") + "获胜!");
this.canPlay = false;
}
} else {
JOptionPane.showMessageDialog(this, "当前位置已经有棋子,请重新落子!");
}
this.repaint();
}
}
// 点击 开始游戏 按钮
if (e.getX() >= 482 && e.getX() <= 523 && e.getY() >= 667
&& e.getY() <= 685) {
int result = JOptionPane.showConfirmDialog(this, "是否重新开始游戏?");
if (result == 0) {
// 现在重新开始游戏
// 重新开始所要做的操作: 1)把棋盘清空,allChess这个数组中全部数据归0.
// 2) 将 游戏信息: 的显示改回到开始位置
// 3) 将下一步下棋的改为黑方
for (int i = 0; i < 18; i++) {
for (int j = 0; j < 18; j++) {
allChess[i][j] = 0;
}
}
// 另一种方式 allChess = new int[19][19];
message = "白方先行";
isBlack = false;
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() >= 264 && e.getX() <= 307 && e.getY() >= 645
&& e.getY() <= 665) {
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 < 18; i++) {
for (int j = 0; j < 18; j++) {
allChess[i][j] = 0;
}
}
message = "白方先行";
isBlack = false;
blackTime = maxTime;
whiteTime = maxTime;
blackMessage = "无限制";
whiteMessage = "无限制";
blackMessage0= "无限制";
whiteMessage0 = "无限制";
t.suspend();
this.canPlay = true;
this.repaint();
}
}
if (maxTime > 0) {
int result = JOptionPane.showConfirmDialog(this,
"设置完成,是否重新开始游戏?");
if (result == 0) {
for (int i = 0; i < 18; i++) {
for (int j = 0; j < 18; j++) {
allChess[i][j] = 0;
}
}
m
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
内容摘要: 随着互联网迅速的发展,网络游戏已经成为人们普遍生活中不可或缺的一部分,它不仅能使人娱乐,也能够开发人的智力,就像本文所主要讲的五子棋游戏一样能挖掘人们聪明的才干与脑袋的机灵程度。 本文主要讲的是用Eclipse开发工具与java开发语言开发出一个自定义界面的五子棋小游戏,本游戏程序能实现两个人对阵下棋,并且程序中能自定义游戏的时间并且也能智能的判定游戏的获胜方和游戏的结束与否,实现两方进行博弈。 部分代码如下:setTitle() //窗体标题。 setSize(200, 100) //窗体大小。 setLocation() //窗体位置。 setResizable() //窗体缩放。 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) //窗体关闭,同时结束程序。 setVisible(true) //窗体可见。 2.取得屏幕大小: Toolkit.getDefaultToolkit().getScreenSize().width Toolkit.getDefaultToolkit().getScreenSize().height 。。。。。
资源推荐
资源详情
资源评论
收起资源包目录
Java五子棋源码.rar (13个子文件)
1
.project 377B
bin
org
guoguo
game
test
Test.class 512B
frame
FiveChessFrame.class 10KB
image
background.jpg 50KB
Thumbs.db 5KB
.settings
org.eclipse.jdt.core.prefs 629B
1_fat.jar 46KB
src
org
guoguo
game
test
Test.java 197B
frame
FiveChessFrame.java 13KB
image
background.jpg 50KB
Thumbs.db 5KB
.fatjar 377B
.classpath 301B
共 13 条
- 1
pc_master
- 粉丝: 45
- 资源: 128
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 【实务学习资料】python的Excel自动处理学习资料
- 2000-2020年全球194个国家双边贸易进口额出口额进出口总额
- 2003-2019全国各省地级市工业废水排放量城市废水排放量面板数据-Excel完整版
- 北京大学中国商业银行数字化转型指数 2010-2021年EXCEL
- 各国反补贴措施数量指标2000-2020-反补贴措施-贸易壁垒情况
- MFC 列表控件添加修改删除实例
- 2000-2021年 44指标 美国国际收支平衡表 德国国际收支平衡表 印度 中国国际收支平衡表
- Stata空间面板模型(slm,sem,sdm)包括do文件,样例数据和详细说明
- 重磅更新上市公司高管团队异质性数据处理Stata代码(2008-2022年数据)
- 基于QT+C++实现的文本或窗口炫酷有趣的滚动条-果冻条+源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页