java五子棋游戏源代码+流程图+棋盘算法 第4页
public static void main(String[] args) {
FiveChessFrame ff = new FiveChessFrame();
}
}
4.2程序窗体类FiveChessFrame类:
4.2.1主类FiveChessFrame
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 = 0;int y = 0;
int[][] allChess = new int[18][18];
boolean isBlack = false;boolean canPlay = true;
String message = "白方先行";int maxTime = 0;
Thread t = new Thread(this);
int blackTime = 0;int whiteTime = 0;
String blackMessage = "无限制";String whiteMessage = "无限制";
String blackMessage0 = "无限制";String whiteMessage0 = "无限制";
public FiveChessFrame() {this.setTitle("五子棋");
this.setSize(1000, 750);this.setLocation((width - 1000) / 2, (height - 750) /2);this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseListener(this);this.setVisible(true);t.start();
t.suspend();this.repaint(); String imagePath = "" ;
try { imagePath=System.getProperty("user.dir")+"/bin/image/background.jpg" ;
bgImage = ImageIO.read(new File(imagePath.replaceAll("
\\\\", "/")));
} catch (IOException e) {
e.printStackTrace();
}
}
4.2.2画棋盘的paint方法:
public void paint(Graphics g) {
BufferedImage bi = new BufferedImage(1000, 750,
BufferedImage.TYPE_INT_RGB);
Graphics g2 = bi.createGraphics();
g2.setColor(Color.WHITE);g2.drawImage(bgImage, 1, 20, this);
g2.setFont(new Font("隶书", Font.BOLD, 20));
g2.drawString("游戏信息:" + message, 428, 83);
g2.setColor(Color.BLACK);
g2.setFont(new Font("宋体", Font.BOLD, 14));
g2.drawString(whiteMessage, 128, 360);
g2.drawString(blackMessage0, 128, 315);g2.setColor(Color.WHITE);
g2.drawString(blackMessage, 830, 365);
g2.drawString(whiteMessage0,830, 315); g2.setColor(Color.BLACK);
g2.setFont(new Font("隶书", Font.BOLD, 40));
for (int i = 0; i < 18; i++) {
g2.drawLine(258, 102 + 30 * i, 767, 102 + 30 * i);
g2.drawLine(258 + 30 * i, 102, 258 + 30 * i, 612);
}
g2.fillOval(375, 219, 8, 8);g2.fillOval(645,219, 8, 8);
g2.fillOval(645, 489, 8, 8);g2.fillOval(375, 489, 8, 8);
for (int i = 0; i < 18; i++) {
for (int j = 0; j < 18; j++) {
if (allChess[i][j] == 1) {
int tempX = i * 30 + 258;int tempY = j * 30 + 102;
g2.fillOval(tempX - 12, tempY - 12, 28, 28);
}
if (allChess[i][j] == 2) {
int tempX = i * 30 + 258;int tempY = j * 30 + 102;
g2.setColor(Color辣~文-论'文:网
http://www.751com.cn );
g2.fillOval(tempX - 12, tempY - 12, 28, 28);
g2.setColor(Color.BLACK);
g2.drawOval(tempX - 12, tempY - 12, 28, 28);
}
}
}
g.drawImage(bi, 0, 0, this);
}
4.2.3鼠标点击的mousePressed()方法,其中包括点击各个按钮的相应事件
public void mousePressed(MouseEvent e) {
System.out.println("X:"+e.getX()); System.out.println("Y:"+e.getY());
if (canPlay == true) {
x = e.getX();
y = e.getY();
if (x >=260 && x <= 767 && y >= 101 && y <= 610) {
x = (x - 260) / 30;y = (y - 101) / 30;
if (allChess[x][y] == 0)
{if (isBlack == true) {
allChess[x][y] = 1;isBlack = false;
message = "轮到白方";
上一页 [1] [2] [3] [4] [5] 下一页
java五子棋游戏源代码+流程图+棋盘算法 第4页下载如图片无法显示或论文不完整,请联系qq752018766