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 {
private static final long serialVersionUID = 1L;
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int hight = Toolkit.getDefaultToolkit().getScreenSize().height;
// Background picture
BufferedImage bjImage = null;
// Save piece coordinates
int x = 0;
int y = 0;
// Save the coordinates of the pieces that were played before
// Data锛?:There is no chess on this point 1锛欱lack chess 2锛歐hite chess
int[][] allChess = new int[19][19];
// Indentify the next step whether black or whie
boolean isBlack = true;
// Identify whether the current game can continue
boolean canPlay = true;
// Save the displayed message
String message = "Black first ";
//Save the max time(s)
int maxTime = 0;
// Do countdown thread classe
Thread t = new Thread(this);
// Save the remaining time between black and white.
int blackTime = 0;
int whiteTime = 0;
// Save the display information for the remaining time of both sides.
String blackMessage = "no limited";
String whiteMessage = "no limited";
@SuppressWarnings("deprecation")
public FiveChessFrame() {
this.setTitle("FiveChess");
this.setSize(500, 500);
this.setLocation((width - 500) / 2, (hight - 500) / 2);
this.addMouseListener(this);
// this.setResizable(false);
this.setVisible(true);
t.start();
t.suspend();// Suspends the thread
// Refresh the screen when it is opened to prevent problems that cannot be displayed when the game starts.
this.repaint();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
bjImage = ImageIO.read(new File("src/background.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void paint(Graphics g) {
// BufferedImage bi=new
// BufferedImage(500,500,BufferedImage.TYPE_INT_ARGB);
// Graphics g2=bi.createGraphics();
g.drawImage(bjImage, 0, 20, this);
g.setFont(new Font("Bold", Font.BOLD, 20));
g.drawString("Game Information锛? + message, 120, 60);
g.setFont(new Font("Song typeface", 0, 14));
g.drawString("Black's time锛? + blackMessage, 30, 470);
g.drawString("White's time锛? + whiteMessage, 260, 470);
// Draw a chessboard
for (int i = 0; i < 19; i++) {
g.drawLine(10, 70 + 20 * i, 370, 70 + 20 * i);
g.drawLine(10 + 20 * i, 70, 10 + 20 * i, 430);
}
// Annotate the position of small dots
g.fillOval(68, 128, 4, 4);
g.fillOval(308, 128, 4, 4);
g.fillOval(308, 368, 4, 4);
g.fillOval(68, 368, 4, 4);
g.fillOval(188, 128, 4, 4);
g.fillOval(68, 248, 4, 4);
g.fillOval(188, 368, 4, 4);
g.fillOval(188, 248, 4, 4);
g.fillOval(308, 248, 4, 4);
// //print chess
// x=(x-10)/20*20+10; //in order to get the coordinates of the intersection
// y=(y-70)/20*20+70;
// //black chess
// g.fillOval(x-7, y-7, 14, 14);
// //white chess
// g.setColor(Color.BLACK);
// g.fillOval(x-7, y-7, 14, 14);
// g.setColor(Color.BLACK);
// g.drawOval(x-7, y-7, 14, 14);
// Output all values in an array
// Draw all the pieces
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
if (allChess[i][j] == 1) {
// black chess
int tempX = i * 20 + 10;
int tempY = j * 20 + 70;
g.fillOval(tempX - 7, tempY - 7, 14, 14);
}
if (allChess[i][j] == 2) {
// white chess
int tempX = i * 20 + 10;
int tempY = j * 20 + 70;
g.setColor(Color.WHITE);
g.fillOval(tempX - 7, tempY - 7, 14, 14);
g.setColor(Color.BLACK);
g.drawOval(tempX - 7, tempY - 7, 14, 14);
}
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
private boolean checkWin() {
boolean flag = false;
// Save the total number of same color pieces connected
int count = 1;
// Determine the horizontal Features锛欼n allChess[x][y] y values are equal
int color = allChess[x][y];
// Determine the vertical
count = this.checkCount(1, 0, color);
if (count >= 5) {
flag = true;
} else {
// Determint the vertical
count = this.checkCount(0, 1, color);
if (count >= 5) {
flag = true;
} else {
// Determine the upper left and lower right
count = this.checkCount(1, -1, color);
if (count >= 5) {
flag = true;
} else {
// Determine the lower left and upper right
count = this.checkCount(1, 1, color);
if (count >= 5) {
flag = true;
}
}
}
}
return flag;
}
// determine the total number of same color pieces connected
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;
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@SuppressWarnings("deprecation")
@Override
public void mousePressed(MouseEvent e) {
if (canPlay == true) {
x = e.getX();
y = e.getY();
if (x >= 10 && x <= 370 && y >= 70 && y <= 430) {
// System.out.println("Within the chessborad锛?+x+"--"+y);
x = (x - 10) / 20; // In order to get the coordinates of the intersection
y = (y - 70) / 20;
if (allChess[x][y] == 0) {
// Determine which pices are going to play next
if (isBlack == true) {
allChess[x][y] = 1;
isBlack = false;
message = "Turn to white";
} else {
allChess[x][y] = 2;
isBlack = true;
message = "Turn to black";
}
// Determine whether the piece is connected to other pieces in 5.
boolean winFlag = this.checkWin();
if (winFlag == true) {
JOptionPane.showMessageDialog(this, "Game Over,"
+ (allChess[x][y] == 1 ? "Black" : "White") + "Win!");
canPlay = false;
}
} else {
JOptionPane.showMessageDialog(this, "There is a chess in this position. Please try again锛侊紒锛?);
}
this.repaint();
}
}
// Click to start game button
if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 70
&& e.getY() <= 100) {
int result = JOptionPane.showConfirmDialog(this, "Whether to restart the game锛?);
if (result == 0) {
// Now restart the game
// Restart the operation锛?)Empty the chessboard,All the data in allChess array to 0锛? // 2)Game information display initialization
// 3)Change the next step to black
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
allChess[i][j] = 0;
}
}
// Another way allChess=new int[19][19]
message = "Black first";
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 = "no limited";
whiteMessage = "mo limited";
}
this.repaint();// If you do not transfer, the interface will not refresh
}
}// Click on the game settings button
if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 370
&& e.getY() <= 400) {
JOptionPane.showMessageDialog(this, "quit game");
System.exit(0);
}
}
@Override
public void mouseReleased(MouseEvent arg0) {
}
@Override
public void run() {
// Determine if there is a time limit
if (maxTime > 0) {
while (true) {
if (isBlack) {
blackTime--;
if (blackTime == 0) {
JOptionPane.showMessageDialog(this, "black overtime锛実ame over!");
}
} else {
whiteTime--;
if (whiteTime == 0) {
JOptionPane.showMessageDialog(this, "white overtime锛実ame over!");
}
}
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) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
@SuppressWarnings("unused")
FiveChessFrame mf = new FiveChessFrame();
}
}
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。