java swing實(shí)現(xiàn)簡(jiǎn)單的五子棋游戲
更新時(shí)間:2021年03月02日 11:57:09 作者:Jayce27
這篇文章主要為大家詳細(xì)介紹了java swing實(shí)現(xiàn)簡(jiǎn)單的五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
用java swing寫的一個(gè)簡(jiǎn)單的五子棋游戲。

下面是Main.java。
package com.crossing.main;
import com.crossing.view.GameWindow;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
GameWindow gameWindow = new GameWindow();
}
}
下面是GameWindow.java。
package com.crossing.view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
* @Title: GameWindow.java
* @Package com.crossing.view
* @Description: TODO(用一句話描述該文件做什么)
* @author crossing
* @date 2021年2月28日 下午9:23:14
* @version V1.0
*/
@SuppressWarnings("serial")
public class GameWindow extends JFrame implements MouseListener, Runnable {
private int width, height;// 屏幕寬高
private int mouseX = 0, mouseY = 0, mapsX = 0, mapsY = 0;// 鼠標(biāo)坐標(biāo),鼠標(biāo)在地圖中的位置
private int game_width = 600, game_height = 600;// 游戲窗口大小
private BufferedImage bgImage = null;// 背景圖片
private int chessBoardItemWidth = 25;// 棋盤每一小格的大小
private Rectangle chessBoardRect = null;// 棋盤所在矩形
private BufferedImage offsetImg = new BufferedImage(game_width, game_height, BufferedImage.TYPE_4BYTE_ABGR);
private Graphics g = offsetImg.getGraphics();// 雙緩沖解決閃爍問(wèn)題
private int[][] maps = new int[15][15];// 0無(wú)棋子,1黑子,2白子
private boolean isBlack = true;// 是否是黑方的回合
private String message = "黑方先行", whitemessage = "無(wú)限制", blackmessage = "無(wú)限制";// 界面上方信息,下方時(shí)間信息
// 右邊操作界面
private JButton btn_start, btn_exit, btn_settings;
private JPanel operaterPanel;// 操作面板
private int gametime = 0;// 游戲時(shí)間限制(秒)
private int blackTime = 0, whiteTime = 0;// 黑白方剩余時(shí)間
private Thread timeThread = new Thread(this);// 黑白雙方倒計(jì)時(shí)線程
// private boolean isLimitTime = false;
public GameWindow() {
setTitle("五子棋");
setSize(game_width, game_height);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 獲取屏幕寬高
width = Toolkit.getDefaultToolkit().getScreenSize().width;
height = Toolkit.getDefaultToolkit().getScreenSize().height;
// 棋盤位置矩形
chessBoardRect = new Rectangle(50, 120, 370, 370);
setLocation((width - game_width) / 2, (height - game_height) / 2);
addMouseListener(this);
// 初始化右邊的面板
initOeratePane();
repaint();
// 設(shè)置背景
try {
bgImage = ImageIO.read(new File("img/backgroung.png"));
// System.out.println(bgImage);
} catch (IOException e) {
e.printStackTrace();
}
setVisible(true);
}
/**
* 初始化黑白雙方時(shí)間
*/
private void initTime() {
// System.out.println("isLimitTime:" + isLimitTime);
if (gametime > 0) {
timeThread.start();
if (blackTime < 0) {
JOptionPane.showMessageDialog(this, "黑方時(shí)間已到,白方獲勝!");
timeThread.interrupt();
} else if (whiteTime < 0) {
JOptionPane.showMessageDialog(this, "白方時(shí)間已到,黑方獲勝!");
timeThread.interrupt();
}
}
}
/**
* 初始化右邊操作界面
*/
private void initOeratePane() {
btn_start = new JButton("開(kāi)始游戲");
btn_settings = new JButton("游戲設(shè)置");
btn_exit = new JButton("退出游戲");
btn_start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int select = JOptionPane.showConfirmDialog(getContentPane(), "確定要重新開(kāi)始嗎?");
if (select == 0) {
reStartGame();
}
}
});
btn_settings.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String select = "";
select = JOptionPane.showInputDialog("請(qǐng)輸入游戲時(shí)間(分鐘),輸入0不限時(shí)間:");
if (select != null && !select.equals("")) {
try {
gametime = Integer.parseInt(select) * 60;
// System.out.println("gametime:" + gametime);
// isLimitTime = true;
// System.out.println("設(shè)置isLimitTime--" + isLimitTime);
blackTime = gametime;
whiteTime = gametime;
if (gametime > 0) {
blackmessage = blackTime / 3600 + ":" + blackTime / 60 % 60 + ":" + blackTime % 60;
whitemessage = whiteTime / 3600 + ":" + whiteTime / 60 % 60 + ":" + whiteTime % 60;
// timeThread.resume();
} else {
whitemessage = "無(wú)限制";
blackmessage = "無(wú)限制";
}
initTime();
repaint();
} catch (Exception e2) {
e2.printStackTrace();
JOptionPane.showMessageDialog(getContentPane(), "請(qǐng)輸入正確信息!");
}
//
}
}
});
btn_exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
operaterPanel = new JPanel();
GridLayout layout = new GridLayout(0, 1, 100, 100);
operaterPanel.setLayout(layout);
operaterPanel.add(btn_start);
operaterPanel.add(btn_settings);
operaterPanel.add(btn_exit);
getContentPane().add(operaterPanel, BorderLayout.EAST);
}
/**
* 重新開(kāi)始游戲
*/
protected void reStartGame() {
isBlack = true;
blackTime = gametime;
whiteTime = gametime;
// for (int i = 0; i < maps[0].length; i++) {
// for (int j = 0; j < maps.length; j++) {
// maps[i][j] = 0;
// }
// }
maps = new int[15][15];
repaint();
}
@Override
public void paint(Graphics g1) {
super.paint(g);
// 繪制背景
g.drawImage(bgImage, 20, 90, this);
// 繪制上方標(biāo)題
g.setColor(Color.black);
g.setFont(new Font("楷體", Font.BOLD, 30));
g.drawString("游戲信息:" + message, 100, 75);
// 繪制下方
g.setColor(Color.gray);
g.fillRect(50, 530, 200, 50);
g.fillRect(300, 530, 200, 50);
g.setColor(Color.black);
g.setFont(new Font("宋體", Font.BOLD, 20));
g.drawString("黑方時(shí)間:" + blackmessage, 60, 560);
g.drawString("白方時(shí)間:" + whitemessage, 310, 560);
// g.setColor(Color.blue);
// 繪制棋盤線條
for (int i = 0; i < 15; i++) {
g.drawLine(60, 130 + i * chessBoardItemWidth, 410, 130 + i * chessBoardItemWidth);
g.drawLine(60 + i * chessBoardItemWidth, 130, 60 + i * chessBoardItemWidth, 480);
}
// 標(biāo)注點(diǎn)位
g.fillOval(131, 200, 8, 8);
g.fillOval(331, 200, 8, 8);
g.fillOval(131, 400, 8, 8);
g.fillOval(331, 400, 8, 8);
g.fillOval(230, 299, 10, 10);
// 繪制棋子
for (int j = 0; j < maps.length; j++) {
for (int i = 0; i < maps[0].length; i++) {
if (maps[j][i] == 1) {
g.setColor(Color.black);
g.fillOval(50 + i * chessBoardItemWidth, 120 + j * chessBoardItemWidth, 20, 20);
}
if (maps[j][i] == 2) {
g.setColor(Color.white);
g.fillOval(50 + i * chessBoardItemWidth, 120 + j * chessBoardItemWidth, 20, 20);
}
}
}
// 雙緩沖解決屏幕閃爍
g1.drawImage(offsetImg, 0, 0, this);
}
@Override
public void mouseClicked(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
// 鼠標(biāo)落子
if (chessBoardRect.contains(mouseX, mouseY)) {
mapsX = (mouseX - 50) / chessBoardItemWidth;
mapsY = (mouseY - 120) / chessBoardItemWidth;
// System.out.println("mapsXY:" + mapsX + "," + mapsY);
// maps[mapsY][mapsX] = (isBlack == true ? 1 : 2);
if (maps[mapsY][mapsX] == 0) {
if (isBlack) {
maps[mapsY][mapsX] = 1;
isBlack = false;
message = "白色落子";
} else {
maps[mapsY][mapsX] = 2;
isBlack = true;
message = "黑色落子";
}
checkGame();
}
}
repaint();
}
/**
* 判斷游戲是否結(jié)束
*/
private void checkGame() {
int color = maps[mapsY][mapsX];
boolean isWin = false;
// System.out.println("mapsXY:" + mapsX + "," + mapsY);
isWin = checkChess(1, 0, color) || checkChess(0, 1, color) || checkChess(1, 1, color)
|| checkChess(1, -1, color);
if (isWin) {
if (color == 1)
JOptionPane.showMessageDialog(this, "黑方勝利!");
else {
JOptionPane.showMessageDialog(this, "白方勝利!");
}
reStartGame();
// new GameWindow();
}
}
/**
* @param xChange 只能是1,0,-1
* @param yChange 只能是1,0,-1
* @param color
*/
private boolean checkChess(int xChange, int yChange, int color) {
boolean isWin = false;
int count = 1, tempX = xChange, tempY = yChange;
while ((mapsX + tempX) >= 0 && (mapsX + tempX) < 15 && (mapsY + tempY) >= 0 && (mapsY + tempY) < 15
&& maps[mapsY + tempY][mapsX + tempX] == color) {
count++;
if (tempX == 0 && tempY == 0)
break;
if (tempX > 0)
tempX++;
if (tempX < 0)
tempX--;
if (tempY > 0)
tempY++;
if (tempY < 0)
tempY--;
}
tempX = xChange;
tempY = yChange;
while ((mapsX - tempX) >= 0 && (mapsX - tempX) < 15 && (mapsY - tempY) >= 0 && (mapsY - tempY) < 15
&& maps[mapsY - tempY][mapsX - tempX] == color) {
count++;
if (tempX == 0 && tempY == 0)
break;
if (tempX > 0)
tempX++;
if (tempX < 0)
tempX--;
if (tempY > 0)
tempY++;
if (tempY < 0)
tempY--;
}
// System.out.println("count:" + count);
if (count >= 5) {
return true;
}
return isWin;
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
// mouseX = e.getX();
// mouseY = e.getY();
// System.out.println("鼠標(biāo)進(jìn)入游戲窗口");
// System.out.println("鼠標(biāo)坐標(biāo):" + mouseX + "," + mouseY);
// if (chessBoardRect.contains(mouseX, mouseY)) {
// System.out.println("進(jìn)入棋盤");
// if (isBlack) {
// g.setColor(Color.black);
// } else {
// g.setColor(Color.white);
// }
// g.fillOval(mouseX, mouseY, 20, 20);
// repaint();
// }
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void run() {
while (true) {
// System.out.println("isblack:" + isBlack);
if (isBlack) {
blackTime--;
} else {
whiteTime--;
}
blackmessage = blackTime / 3600 + ":" + blackTime / 60 % 60 + ":" + blackTime % 60;
whitemessage = whiteTime / 3600 + ":" + whiteTime / 60 % 60 + ":" + whiteTime % 60;
// System.out.println("blackTime:" + blackTime);
// System.out.println("whiteTime:" + whiteTime);
repaint();
if (blackTime < 0) {
JOptionPane.showMessageDialog(getContentPane(), "黑方時(shí)間已到,白方獲勝!");
timeThread.interrupt();
new GameWindow();
break;
} else if (whiteTime < 0) {
JOptionPane.showMessageDialog(getContentPane(), "白方時(shí)間已到,黑方獲勝!");
timeThread.interrupt();
new GameWindow();
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
背景圖片

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
談?wù)勎覍?duì)Spring Bean 生命周期的理解
Spring Bean 的生命周期在整個(gè) Spring 中占有很重要的位置,掌握這些可以加深對(duì) Spring 的理解。這篇文章主要介紹了Spring Bean 生命周期,需要的朋友可以參考下2018-03-03
Spring運(yùn)行時(shí)手動(dòng)注入bean的方法實(shí)例
spring給我們提供了IOC服務(wù),讓我們可以用注解的方式,方便的使用bean的相互引用,下面這篇文章主要給大家介紹了關(guān)于Spring運(yùn)行時(shí)手動(dòng)注入bean的相關(guān)資料,需要的朋友可以參考下2022-05-05
Java基于正則表達(dá)式實(shí)現(xiàn)查找匹配的文本功能【經(jīng)典實(shí)例】
這篇文章主要介紹了Java基于正則表達(dá)式實(shí)現(xiàn)查找匹配的文本功能,結(jié)合具體實(shí)例形式分析了java正則查找、字符串遍歷、group分組相關(guān)操作技巧,需要的朋友可以參考下2017-04-04
Springboot GET和POST請(qǐng)求參數(shù)獲取方式小結(jié)
Spring Boot GET和POST請(qǐng)求參數(shù)獲取是開(kāi)發(fā)人員經(jīng)常需要解決的問(wèn)題,本文主要介紹了Springboot GET和POST請(qǐng)求參數(shù)獲取方式小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09

