/**
* Create by ZhangJin
* 2015.8
*/
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
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, y;
// 保存棋盘,0表示棋盘该点无棋子,1表示黑子,2表示白子
int[][] allChess = new int[15][15];
// 保存当前下子是黑子还是白字,true是黑子,false是白子
Boolean isBlack = true;
// 标识当前游戏是否结束
Boolean canPlay = true;
// 保存游戏信息
String message = "黑方先行";
// 保存棋谱
int[] chessX = new int[255];
int[] chessY = new int[255];
int countX, countY;
// 保存最大时间
int maxTime = 0;
// 游戏时间设置的信息
String blackMessage = "无限制";
String whiteMessage = "无限制";
// 保存黑白方所剩余的时间
int blackTime = 0;
int whiteTime = 0;
// 游戏倒计时线程
Thread timer = new Thread(this);
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.setVisible(true);
this.repaint();
this.addMouseListener(this);
timer.start();
timer.suspend();
}
public void paint(Graphics g) {
/*try {
bgImage = ImageIO.read(new File("src/wzq/五子棋棋盘.jpg"));
} catch (IOException e) {
e.printStackTrace();
}*/
// 双缓冲
BufferedImage bi = new BufferedImage(500, 500,
BufferedImage.TYPE_INT_RGB);
Graphics g2 = bi.createGraphics();
//g2.drawImage(bgImage, 43, 60, 375, 375, this);
g2.setColor(new Color(0, 169, 158));
g2.fill3DRect(43, 60, 375, 375, true);
for(int i = 0; i <= 15; i++){
g2.setColor(Color.WHITE);
g2.drawLine(43, i * 25 + 60, 375 + 43, 60 + i * 25);
g2.drawLine(i * 25 + 43, 60, 43 + i * 25, 375 + 60);
}
g2.setFont(new Font("黑体", Font.BOLD, 20));
g2.drawString("游戏信息:" + message, 50, 50);
g2.drawRect(30, 440, 180, 40);
g2.drawRect(250, 440, 180, 40);
g2.setFont(new Font("宋体", 0, 12));
g2.drawString("黑方时间:" + blackMessage, 40, 465);
g2.drawString("白方时间:" + whiteMessage, 260, 465);
// 重新开始按钮
g2.drawRect(428, 66, 54, 20);
g2.drawString("重新开始", 432, 80);
// 游戏设置按钮
g2.drawRect(428, 106, 54, 20);
g2.drawString("游戏设置", 432, 120);
// 游戏说明按钮
g2.drawRect(428, 146, 54, 20);
g2.drawString("游戏说明", 432, 160);
// 退出游戏按钮
g2.drawRect(428, 186, 54, 20);
g2.drawString("退出游戏", 432, 200);
// 悔棋
g2.drawRect(428, 246, 54, 20);
g2.drawString("悔棋", 442, 260);
// 认输
g2.drawRect(428, 286, 54, 20);
g2.drawString("认输", 442, 300);
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
// 黑子
if (allChess[i][j] == 1) {
int tempX = i * 25 + 55;
int tempY = j * 25 + 72;
g2.setColor(Color.BLACK);
g2.fillOval(tempX - 8, tempY - 8, 16, 16);
}
// 白子
if (allChess[i][j] == 2) {
int tempX = i * 25 + 55;
int tempY = j * 25 + 72;
g2.setColor(Color.WHITE);
g2.fillOval(tempX - 8, tempY - 8, 16, 16);
g2.setColor(Color.BLACK);
g2.drawOval(tempX - 8, tempY - 8, 16, 16);
}
}
}
g.drawImage(bi, 0, 0, this);
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
Boolean checkWin = false;
if (canPlay) {
x = e.getX();
y = e.getY();
/*
* System.out.println(x); System.out.println(y);
*/
if (x >= 55 && x <= 405 && y >= 72 && y <= 420) {
if ((x - 55) % 25 > 12)
x = (x - 55) / 25 + 1;
else
x = (x - 55) / 25;
if ((y - 72) % 25 > 12)
y = (y - 72) / 25 + 1;
else
y = (y - 72) / 25;
if (allChess[x][y] == 0) {
chessX[countX++] = x;
chessY[countY++] = y;
// System.out.println(countX + " " + countY);
if (isBlack) {
allChess[x][y] = 1;
isBlack = false;
message = "白方下子";
} else {
allChess[x][y] = 2;
isBlack = true;
message = "黑方下子";
}
this.repaint();
checkWin = isWin();
if (checkWin) {
if (allChess[x][y] == 1)
JOptionPane.showMessageDialog(this, "游戏结束,黑方胜利");
else
JOptionPane.showMessageDialog(this, "游戏结束,白方胜利");
canPlay = false;
}
}
}
}
// 重新开始游戏
if (e.getX() >= 428 && e.getX() <= 482 && e.getY() >= 66 && e.getY() <= 86) {
int result = JOptionPane.showConfirmDialog(this, "是否重新开始游戏?");
if (result == 0) {
for (int i = 0; i < 15; i++)
for (int j = 0; j < 15; j++)
allChess[i][j] = 0;
for (int i = 0; i < 15; i++) {
chessX[i] = 0;
chessY[i] = 0;
}
countX = 0;
countY = 0;
message = "黑方先行";
blackMessage = "无限制";
whiteMessage = "无限制";
blackTime = maxTime;
whiteTime = maxTime;
isBlack = true;
canPlay = true;
this.repaint();
}
}
// 游戏倒计时设置
if (e.getX() >= 428 && e.getX() <= 482 && e.getY() >= 106 && e.getY() <= 126) {
String input = JOptionPane
.showInputDialog("请输入游戏的最大时间(单位:分钟),如果输入0,表示没有时间限制:");
maxTime = Integer.parseInt(input) * 60;
System.out.println(maxTime);
if (maxTime < 0) {
JOptionPane.showMessageDialog(this, "输入的游戏时间有误,请重新设置!");
} else if (maxTime == 0) {
int result = JOptionPane.showConfir
辣椒种子
- 粉丝: 4098
- 资源: 5735
最新资源
- 基于javaweb的网上拍卖系统,采用Spring + SpringMvc+Mysql + Hibernate+ JSP技术
- polygon-mumbai
- Chrome代理 switchyOmega
- GVC-全球价值链参与地位指数,基于ICIO表,(Wang等 2017a)计算方法
- 易语言ADS指纹浏览器管理工具
- 易语言奇易模块5.3.6
- cad定制家具平面图工具-(FG)门板覆盖柜体
- asp.net 原生js代码及HTML实现多文件分片上传功能(自定义上传文件大小、文件上传类型)
- whl@pip install pyaudio ERROR: Failed building wheel for pyaudio
- Constantsfd密钥和权限集合.kt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈