package com.top.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.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class GobangFrame extends JFrame implements MouseListener, Runnable {
// 取得屏幕的宽度
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
// 取得屏幕的高度
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
// 背景图片
BufferedImage background = null;
// 黑白棋子图片
BufferedImage blackChess = null;
BufferedImage whiteChess = null;
// 保存全部棋子 0表示该位置没有棋子,1表示该位置是黑子,2表示该位置是白子
int[][] allChess = new int[15][15];
// 保存棋子横纵坐标
int x = 0;
int y = 0;
// 保存当前该哪种颜色棋子走下一步
boolean isBlack = true;
// 保存当前步数
int step = 1;
int tempStep = step;
// 保存每步棋子的落点
int[] arrayX = new int[361];
int[] arrayY = new int[361];
int[] arrayTempX = new int[361];
int[] arrayTempY = new int[361];
// 游戏信息
String message = "黑方先行";
// 限制时间
String blackMessage = "无限制";
String whiteMessage = "无限制";
// 保存是否可以继续下棋
boolean canPlay = true;
// 保存当前是否有人胜利
boolean winFlag = false;
// 保存最大限制时间
int maxTime = 0;
// 保存黑白双方的剩余时间
int blackTime = 0;
int whiteTime = 0;
// 限制时间的线程
Thread t = new Thread(this);
public GobangFrame() {
// 设置标题
this.setTitle("五子棋");
// 设置窗口大小
this.setSize(646, 628);
// 设置窗口关闭方式
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置窗口大小是否可变
this.setResizable(false);
// 使窗口在屏幕中央显示
this.setLocation((width - 645)/2, (height - 630)/2);
// 加入监听器
this.addMouseListener(this);
// 显示窗体
this.setVisible(true);
// 倒计时线程操作
t.start();
t.suspend();
//重绘
this.repaint();
try {
URL bgPath = GobangFrame.class.getResource("/Image/background.jpg");
URL blackPath = GobangFrame.class.getResource("/Image/blackChess.png");
URL whitePath = GobangFrame.class.getResource("/Image/whiteChess.png");
URL iconPath = GobangFrame.class.getResource("/Image/Gobang.gif");
background = ImageIO.read(bgPath);
blackChess = ImageIO.read(blackPath);
whiteChess = ImageIO.read(whitePath);
// 设置程序图标
this.setIconImage(ImageIO.read(iconPath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void paint(Graphics g) {
// 双缓冲技术防止屏幕闪烁
BufferedImage bg = new BufferedImage(646, 628, BufferedImage.TYPE_INT_ARGB);
Graphics g1 = bg.createGraphics();
g1.setColor(Color.BLACK);
// 绘制背景
g1.drawImage(background, 3, 25, this);
// 绘制游戏信息
g1.setFont(new Font("黑体", Font.BOLD, 20));
g1.drawString("游戏信息:" + message, 430, 73);
// 绘制时间信息
g1.setFont(new Font("宋体", 0, 14));
g1.drawString("黑方时间:" + blackMessage, 52, 70);
g1.drawString("白方时间:" + whiteMessage, 259, 70);
// 悔棋
g1.drawString("<--", 555, 348);
g1.drawRect(548, 328, 35, 30);
g1.drawString("-->", 598, 348);
g1.drawRect(590, 328, 35, 30);
// 绘制全部棋子
for (int i=0; i<15; i++) {
for (int j=0; j<15; j++) {
int tempX = i * 35 + 5;
int tempY = j * 35 + 81;
if (allChess[i][j] == 1) {
// 黑子
g1.drawImage(blackChess, tempX, tempY, this);
} else if (allChess[i][j] == 2) {
// 白子
g1.drawImage(whiteChess, tempX, tempY, this);
}
}
}
g.drawImage(bg, 0, 0, this);
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
x = e.getX();
y = e.getY();
// 落子
if (canPlay == true) {
if (x>=5 && x<=530 && y>=81 && y<=606) {
x = (e.getX() - 5) / 35;
y = (e.getY() - 81) / 35;
if (allChess[x][y] == 0) {
if (isBlack == true) {
allChess[x][y] = 1;
isBlack = false;
message = "轮到白方";
} else {
allChess[x][y] = 2;
isBlack = true;
message = "轮到黑方";
}
arrayTempX[step] = arrayX[step] = x;
arrayTempY[step] = arrayY[step] = y;
System.out.println(step);
System.out.println("<" + arrayX[step] + "," + arrayY[step] + ">");
tempStep = step = step + 1;
}
this.repaint();
winFlag = this.checkWin();
if (winFlag) {
t.suspend();
message = (allChess[x][y] == 1 ? "黑方胜利!" : "白方胜利!");
JOptionPane.showMessageDialog(this, "游戏结束," + message);
this.repaint();
canPlay = false;
}
}
}
// 重新开始
if (x>=548 && x<=625 && y>=118 && y<=148) {
int result = JOptionPane.showConfirmDialog(this, "重新开始游戏?", "重新开始", 2, 3);
if (result == 0) {
// 初始化所有棋子
allChess = new int[15][15];
// 初始化哪方走棋
isBlack = true;
// 初始化是否可以继续走棋
canPlay = true;
// 初始化游戏信息
message = "黑方先行";
// 初始化限制时间
maxTime = 0;
blackTime = maxTime;
whiteTime = maxTime;
// 初始化步数
step = 1;
tempStep = step;
// 初始化落子位置
arrayX = new int[361];
arrayY = new int[361];
arrayTempX = new int[361];
arrayTempY = new int[361];
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 = "无限制";
}
canPlay = true;
this.repaint();
}
}
// 游戏设置
if (x>=548 && x<=625 && y>=188 && y<=218) {
String input = JOptionPane.showInputDialog(this,"请输入游戏的最大时间(单位:分钟),如果输入0,表示没有时间限制:", "游戏设置", 3);
try {
maxTime = Integer.parseInt(input) * 60;
if (maxTime < 0) {
JOptionPane.showMessageDialog(this, "时间不能为负,请输入正数!", "错误的输入", 0);
} else if (maxTime == 0) {
// 重新开始游戏,初始化各项数据
allChess = new int[15][15];
message = "黑方先行";
isBlack = true;
blackTime = maxTime;
whiteTime = maxTime;
blackMessage = "无限制";
whiteMessage = "无限制";
step = 1;
tempStep = step;
arrayX = new int[361];
arrayY = new int[361];
arrayTempX = new int[361];
arrayTempY = new int[361];
canPlay = true;
this.repaint();
} else if (maxTime > 0) {
// 重新开始游戏,初始化各项数据
allChess = new int[15][15];
message = "黑方先行";
isBlack = true;
blackTime = maxTime;
whiteTime = maxTime;
step = 1;
tempStep = step;
arrayX = new int[361];
arrayY = new int[361];
arrayTempX = new int[361];
arrayTempY = new int[361];
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();
canPlay = true;
this.repaint();
}
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(this, "非法的输入,请输入数�
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Java五子棋.rar (7个子文件)
Java五子棋
src
com
top
game
Gobang
Gobang.java 169B
frame
GobangFrame.java 12KB
Image
Gobang.gif 1010B
background.jpg 123KB
blackChess.png 4KB
whiteChess.png 5KB
Gobang.jar 146KB
共 7 条
- 1
资源评论
topcows
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功