package FiveChessGame;
/**
* 五子棋游戏.FiveChessGame
* 思路:
* 1。用按钮做棋子,用图片填充每个按钮。
* 2。棋盘为24*24的
* @see java.awt.event.ActionEvent;
* @see java.awt.event.ActionListener;
* @see java.awt.*;
* @see javax.swing.*;
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
/**
* 五子棋FiveChessGame类,继承了JFrame,并且添加了时间监听
*
* @see javax.swing.JFrame
* @see java.awt.event.ActionListener
*/
public class FiveChessGameMain extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
/**
* 五子棋棋盘的长宽:chessLineCount
*
* @serial
*/
static int chessLineCount = 20;
/**
* 五子棋的棋子chess,用JButton表示
*
* @serial
*/
private JButton chess[][] = new JButton[chessLineCount][chessLineCount];
/**
* 和五子棋棋子chess对应的棋子标识,用于标记棋子的状态。 1表示黑棋,-1表示白棋,默认0表示无棋子
*
* @serial
* @see #chess
*/
int chessInt[][] = new int[chessLineCount][chessLineCount];
/**
* 用户标识 1表示黑棋,-1表示白棋,10表示电脑
*/
int userFlag = 1;
/**
* true表示人,false表示电脑
*/
boolean userType = true;// ture Man false PC
/**
* 棋子的图片保存的位置direction
*
* @serial
*/
// home/tarena/You.me/HomeWork/Others/bin/FiveChessGame/FiveChessIcons
String direction = "./FiveChess/FiveChessGame/FiveChessIcons/";
/**
* 初始状态下棋子的图片:beginIcon
*
* @serial
* @see javax.swing.ImageIcon
*/
ImageIcon beginIcon = new ImageIcon(direction + "qz.jpg");
/**
* 白棋的图片:whiteIcon
*
* @serial
* @see javax.swing.ImageIcon
*/
ImageIcon whiteIcon = new ImageIcon(direction + "bz.jpg");
/**
* 黑棋的图片:blackIcon
*
* @see javax.swing.ImageIcon
*/
ImageIcon blackIcon = new ImageIcon(direction + "hz.jpg");
/**
* 用于标识是白棋还是黑棋玩家。1:代表黑棋,-1:代表白棋
*
* @serial
*/
ImageIcon rightIcon = new ImageIcon(direction + "right.jpg");
ImageIcon rightWhiteIcon = new ImageIcon(direction + "rightWhite.jpg");
ImageIcon rightBlackIcon = new ImageIcon(direction + "rightBlack.jpg");
ImageIcon leftIcon = new ImageIcon(direction + "left.jpg");
ImageIcon leftBlackIcon = new ImageIcon(direction + "leftBlack.jpg");
ImageIcon leftWhiteIcon = new ImageIcon(direction + "leftWhite.jpg");
ImageIcon upIcon = new ImageIcon(direction + "up.jpg");
ImageIcon upBlackIcon = new ImageIcon(direction + "upBlack.jpg");
ImageIcon upWhiteIcon = new ImageIcon(direction + "upWhite.jpg");
ImageIcon downIcon = new ImageIcon(direction + "down.jpg");
ImageIcon downBlackIcon = new ImageIcon(direction + "downBlack.jpg");
ImageIcon downWhiteIcon = new ImageIcon(direction + "downWhite.jpg");
ImageIcon upRightIcon = new ImageIcon(direction + "upRight.jpg");
ImageIcon upRightBlackIcon = new ImageIcon(direction + "upRightBlack.jpg");
ImageIcon upRightWhiteIcon = new ImageIcon(direction + "upRightWhite.jpg");
ImageIcon upLeftIcon = new ImageIcon(direction + "upLeft.jpg");
ImageIcon upLeftBlackIcon = new ImageIcon(direction + "upLeftBlack.jpg");
ImageIcon upLeftWhiteIcon = new ImageIcon(direction + "upLeftWhite.jpg");
ImageIcon downRightIcon = new ImageIcon(direction + "downRight.jpg");
ImageIcon downRightBlackIcon = new ImageIcon(direction
+ "downRightBlack.jpg");
ImageIcon downRightWhiteIcon = new ImageIcon(direction
+ "downRightWhite.jpg");
ImageIcon downLeftIcon = new ImageIcon(direction + "downLeft.jpg");
ImageIcon downLeftBlackIcon = new ImageIcon(direction + "downLeftBlack.jpg");
ImageIcon downLeftWhiteIcon = new ImageIcon(direction + "downLeftWhite.jpg");
/**
* 用于存放棋子的JPanel:chessPanel
*
* @see javax.swing.JPanel
*/
JPanel chessPanel = new JPanel();
/**
* FiveChessGame类的构造方法。主要设置JFrame的相关属性。
*
* @see javax.swing.JFrame
* @see #initChess
* @see #initChessInt
* @see #addChessIntoPanel
*/
public FiveChessGameMain() {
setMenu();
/* 调用初始化棋子方法:initChess(JButton[][]) */
initChess(chess);
/* 调用初始化棋子图标的方法setChessIcon(JButton[][]) */
setChessIcon(chess);
/* 调用初始化棋子标签数组的方法:initChessInt(int[][]) */
chessInt = initChessInt(chessInt);
/* 调用向panel中添加棋子的方法:addChessIntoPanel(chess,chessPanel) */
addChessIntoPanel(chess, chessPanel);
/* 向JFrame中添加存放棋子的JPanel:chessPanel */
this.add(chessPanel);
this.setTitle("单机版五子棋.0623");
this.setResizable(false);
this.setLocation(300, 130);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
/**
* 设置JFrame的菜单栏:setMenu()
*
* @see java.awt.MenuBar
* @see java.awt.Menu
* @see java.awt.MenuItem
*/
public void setMenu() {
/* 主菜单mainMenuBar */
JMenuBar mainMenuBar = new JMenuBar();
String[][] menuStrs = { { "游戏", "开始", "PlayWithPC", "", "退出" },
{ "帮助", "关于" }, };
String[][] menuCommands = {
{ "游戏", "Begin", "PlayWithPC", "", "Exit" }, { "帮助", "About" } };
for (int i = 0; i < menuStrs.length; i++) {
JMenu menu = new JMenu(menuStrs[i][0]);
mainMenuBar.add(menu);
for (int j = 1; j < menuStrs[i].length; j++) {
if ("".equals(menuStrs[i][j]))
menu.addSeparator();
else {
JMenuItem menuItem = new JMenuItem(menuStrs[i][j]);
menuItem.addActionListener(this);
menuItem.setActionCommand(menuCommands[i][j]);
menu.add(menuItem);
}
}
}
this.setJMenuBar(mainMenuBar);
}
/**
* 棋盘初始化方法initChess(JButton[][] chess),设置棋子的相关属性,图标、大小、事件等。
*
* @see javax.swing.JButton
* @param chess
* 棋子数组JButton[][],用于保存棋子
*/
public void initChess(JButton[][] chess) {
for (int i = 0; i < chess.length; i++) {
for (int j = 0; j < chess[i].length; j++) {
chess[i][j] = new JButton();
chess[i][j].setBounds(0, 0, 0, 0);
chess[i][j].setBorderPainted(false);
chess[i][j].setPreferredSize(new java.awt.Dimension(32, 32));
chess[i][j].addActionListener(this);
}
}
}
/**
* 设置棋盘的初始棋子的图标setChessIcon
*/
public void setChessIcon(JButton[][] chess) {
for (int i = 0; i < chess.length; i++) {
for (int j = 0; j < chess[i].length; j++) {
if (i == 4 && j == 4)
chess[i][j].setIcon(upLeftIcon);
else if (i == 4 && j == chess[i].length - 4 - 1)
chess[i][j].setIcon(upRightIcon);
else if (i == chess.length - 4 - 1 && j == 4)
chess[i][j].setIcon(downLeftIcon);
else if (i == chess.length - 4 - 1
&& j == chess[i].length - 4 - 1)
chess[i][j].setIcon(downRightIcon);
else if (i == 4)
chess[i][j].setIcon(upIcon);
else if (j == 4)
chess[i][j].setIcon(leftIcon);
else if (j == chess[i].length - 4 - 1)
chess[i][j].setIcon(rightIcon);
else if (i == chess.length - 4 - 1)
chess[i][j].setIcon(downIcon);
else
chess[i][j].setIcon(beginIcon);
}
}
}
/**
* 点击棋子时设置棋子的图标setOnChessIcon
* 根据判断该位置的棋子是否有棋子,和棋子颜色设置棋子图标
*/
public void setOnChessIcon(int i, int j, int userFlag) {
if (i == 4 && j == 4)
chess[i][j].setIcon(userFlag == 1 ? upLeftBlackIcon
: upLeftWhiteIcon);
else if (i == 4 && j == chess[i].length - 4 - 1)
chess[i][j].setIcon(userFlag == 1 ? upRightBlackIcon
: upRightWhiteIcon);
else if (i == chess.length - 4 - 1 && j == 4)
chess[i][j].setIcon(userFlag == 1 ? downLeftBlackIcon
: downLeftWhiteIcon);
else if (i == chess.length - 4 - 1 && j == chess[i].lengt
- 1
- 2
前往页