欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java swing五子棋的实现方法

程序员文章站 2023-12-18 16:38:22
今天给大家介绍一下如何用java swing实现五子棋的开发即用java开发图形界面程序五子棋,代码由于太多,只贴部分,最下面会附上下载地址,废话不多说,下面我们先看一下运...

今天给大家介绍一下如何用java swing实现五子棋的开发即用java开发图形界面程序五子棋,代码由于太多,只贴部分,最下面会附上下载地址,废话不多说,下面我们先看一下运行结果: 

Java swing五子棋的实现方法

接下来我们看代码:

首先是创建主frame框架界面:

package org.liky.game.frame;

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 {

 // 取得屏幕的宽度
 int width = toolkit.getdefaulttoolkit().getscreensize().width;
 // 取得屏幕的高度
 int height = toolkit.getdefaulttoolkit().getscreensize().height;
 // 背景图片
 bufferedimage bgimage = null;
 // 保存棋子的坐标
 int x = 0;
 int y = 0;
 // 保存之前下过的全部棋子的坐标
 // 其中数据内容 0: 表示这个点并没有棋子, 1: 表示这个点是黑子, 2:表示这个点是白子
 int[][] allchess = new int[19][19];
 // 标识当前应该黑棋还是白棋下下一步
 boolean isblack = true;
 // 标识当前游戏是否可以继续
 boolean canplay = true;
 // 保存显示的提示信息
 string message = "黑方先行";
 // 保存最多拥有多少时间(秒)
 int maxtime = 0;
 // 做倒计时的线程类
 thread t = new thread(this);
 // 保存黑方与白方的剩余时间
 int blacktime = 0;
 int whitetime = 0;
 // 保存双方剩余时间的显示信息
 string blackmessage = "无限制";
 string whitemessage = "无限制";

 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.addmouselistener(this);
 // 将窗体显示出来
 this.setvisible(true);

 t.start();
 t.suspend();

 // 刷新屏幕,防止开始游戏时出现无法显示的情况.
 this.repaint();
 string imagepath = "" ;
 try {
  imagepath = system.getproperty("user.dir")+"/src/image/background.jpg" ;
  bgimage = imageio.read(new file(imagepath.replaceall("\\\\", "/")));
 } catch (ioexception e) {
  // todo auto-generated catch block
  e.printstacktrace();
 }
 }

 public void paint(graphics g) {
 // 双缓冲技术防止屏幕闪烁
 bufferedimage bi = new bufferedimage(500, 500,
  bufferedimage.type_int_rgb);
 graphics g2 = bi.creategraphics();
 g2.setcolor(color.black);
 // 绘制背景
 g2.drawimage(bgimage, 1, 20, this);
 // 输出标题信息
 g2.setfont(new font("黑体", font.bold, 20));
 g2.drawstring("游戏信息:" + message, 130, 60);
 // 输出时间信息
 g2.setfont(new font("宋体", 0, 14));
 g2.drawstring("黑方时间:" + blackmessage, 30, 470);
 g2.drawstring("白方时间:" + whitemessage, 260, 470);

 // 绘制棋盘
 for (int i = 0; i < 19; i++) {
  g2.drawline(10, 70 + 20 * i, 370, 70 + 20 * i);
  g2.drawline(10 + 20 * i, 70, 10 + 20 * i, 430);
 }

 // 标注点位
 g2.filloval(68, 128, 4, 4);
 g2.filloval(308, 128, 4, 4);
 g2.filloval(308, 368, 4, 4);
 g2.filloval(68, 368, 4, 4);
 g2.filloval(308, 248, 4, 4);
 g2.filloval(188, 128, 4, 4);
 g2.filloval(68, 248, 4, 4);
 g2.filloval(188, 368, 4, 4);
 g2.filloval(188, 248, 4, 4);

 /*
  * //绘制棋子 x = (x - 10) / 20 * 20 + 10 ; y = (y - 70) / 20 * 20 + 70 ;
  * //黑子 g.filloval(x - 7, y - 7, 14, 14); //白子 g.setcolor(color.white) ;
  * g.filloval(x - 7, y - 7, 14, 14); g.setcolor(color.black) ;
  * g.drawoval(x - 7, y - 7, 14, 14);
  */

 // 绘制全部棋子
 for (int i = 0; i < 19; i++) {
  for (int j = 0; j < 19; j++) {
  if (allchess[i][j] == 1) {
   // 黑子
   int tempx = i * 20 + 10;
   int tempy = j * 20 + 70;
   g2.filloval(tempx - 7, tempy - 7, 14, 14);
  }
  if (allchess[i][j] == 2) {
   // 白子
   int tempx = i * 20 + 10;
   int tempy = j * 20 + 70;
   g2.setcolor(color.white);
   g2.filloval(tempx - 7, tempy - 7, 14, 14);
   g2.setcolor(color.black);
   g2.drawoval(tempx - 7, tempy - 7, 14, 14);
  }
  }
 }
 g.drawimage(bi, 0, 0, this);
 }

 public void mouseclicked(mouseevent e) {
 // todo auto-generated method stub

 }

 public void mouseentered(mouseevent e) {
 // todo auto-generated method stub

 }

 public void mouseexited(mouseevent e) {
 // todo auto-generated method stub

 }

 public void mousepressed(mouseevent e) {
 // todo auto-generated method stub
 /*
  * system.out.println("x:"+e.getx()); system.out.println("y:"+e.gety());
  */
 if (canplay == true) {
  x = e.getx();
  y = e.gety();
  if (x >= 10 && x <= 370 && y >= 70 && y <= 430) {
  x = (x - 10) / 20;
  y = (y - 70) / 20;
  if (allchess[x][y] == 0) {
   // 判断当前要下的是什么颜色的棋子
   if (isblack == true) {
   allchess[x][y] = 1;
   isblack = false;
   message = "轮到白方";
   } else {
   allchess[x][y] = 2;
   isblack = true;
   message = "轮到黑方";
   }
   // 判断这个棋子是否和其他的棋子连成5连,即判断游戏是否结束
   boolean winflag = this.checkwin();
   if (winflag == true) {
   joptionpane.showmessagedialog(this, "游戏结束,"
    + (allchess[x][y] == 1 ? "黑方" : "白方") + "获胜!");
   canplay = false;
   }
  } else {
   joptionpane.showmessagedialog(this, "当前位置已经有棋子,请重新落子!");
  }
  this.repaint();
  }
 }
 /* system.out.println(e.getx() + " -- " + e.gety()); */
 // 点击 开始游戏 按钮
 if (e.getx() >= 400 && e.getx() <= 470 && e.gety() >= 70
  && e.gety() <= 100) {
  int result = joptionpane.showconfirmdialog(this, "是否重新开始游戏?");
  if (result == 0) {
  // 现在重新开始游戏
  // 重新开始所要做的操作: 1)把棋盘清空,allchess这个数组中全部数据归0.
  // 2) 将 游戏信息: 的显示改回到开始位置
  // 3) 将下一步下棋的改为黑方
  for (int i = 0; i < 19; i++) {
   for (int j = 0; j < 19; j++) {
   allchess[i][j] = 0;
   }
  }
  // 另一种方式 allchess = new int[19][19];
  message = "黑方先行";
  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 = "无限制";
   whitemessage = "无限制";
  }
  this.canplay = true; 
  this.repaint();

  }
 }
 // 点击 游戏设置 按钮
 if (e.getx() >= 400 && e.getx() <= 470 && e.gety() >= 120
  && e.gety() <= 150) {
  string input = joptionpane
   .showinputdialog("请输入游戏的最大时间(单位:分钟),如果输入0,表示没有时间限制:");
  try {
  maxtime = integer.parseint(input) * 60;
  if (maxtime < 0) {
   joptionpane.showmessagedialog(this, "请输入正确信息,不允许输入负数!");
  }
  if (maxtime == 0) {
   int result = joptionpane.showconfirmdialog(this,
    "设置完成,是否重新开始游戏?");
   if (result == 0) {
   for (int i = 0; i < 19; i++) {
    for (int j = 0; j < 19; j++) {
    allchess[i][j] = 0;
    }
   }
   // 另一种方式 allchess = new int[19][19];
   message = "黑方先行";
   isblack = true;
   blacktime = maxtime;
   whitetime = maxtime;
   blackmessage = "无限制";
   whitemessage = "无限制";
   this.canplay = true; 
   this.repaint();
   }
  }
  if (maxtime > 0) {
   int result = joptionpane.showconfirmdialog(this,
    "设置完成,是否重新开始游戏?");
   if (result == 0) {
   for (int i = 0; i < 19; i++) {
    for (int j = 0; j < 19; j++) {
    allchess[i][j] = 0;
    }
   }
   // 另一种方式 allchess = new int[19][19];
   message = "黑方先行";
   isblack = true;
   blacktime = maxtime;
   whitetime = maxtime;
   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();
   this.canplay = true; 
   this.repaint();
   }
  }
  } catch (numberformatexception e1) {
  // todo auto-generated catch block
  joptionpane.showmessagedialog(this, "请正确输入信息!");
  }
 }
 // 点击 游戏说明 按钮
 if (e.getx() >= 400 && e.getx() <= 470 && e.gety() >= 170
  && e.gety() <= 200) {
  joptionpane.showmessagedialog(this,
   "这个一个五子棋游戏程序,黑白双方轮流下棋,当某一方连到五子时,游戏结束。");
 }
 // 点击 认输 按钮
 if (e.getx() >= 400 && e.getx() <= 470 && e.gety() >= 270
  && e.gety() <= 300) {
  int result = joptionpane.showconfirmdialog(this, "是否确认认输?");
  if (result == 0) {
  if (isblack) {
   joptionpane.showmessagedialog(this, "黑方已经认输,游戏结束!");
  } else {
   joptionpane.showmessagedialog(this, "白方已经认输,游戏结束!");
  }
  canplay = false;
  }
 }
 // 点击 关于 按钮
 if (e.getx() >= 400 && e.getx() <= 470 && e.gety() >= 320
  && e.gety() <= 350) {
  joptionpane.showmessagedialog(this,
   "本游戏由mldn制作,有相关问题可以访问www.mldn.cn");
 }
 // 点击 退出 按钮
 if (e.getx() >= 400 && e.getx() <= 470 && e.gety() >= 370
  && e.gety() <= 400) {
  joptionpane.showmessagedialog(this, "游戏结束");
  system.exit(0);
 }
 }

 public void mousereleased(mouseevent e) {
 // todo auto-generated method stub

 }

 private boolean checkwin() {
 boolean flag = false;
 // 保存共有相同颜色多少棋子相连
 int count = 1;
 // 判断横向是否有5个棋子相连,特点 纵坐标 是相同, 即allchess[x][y]中y值是相同
 int color = allchess[x][y];
 /*
  * if (color == allchess[x+1][y]) { count++; if (color ==
  * allchess[x+2][y]) { count++; if (color == allchess[x+3][y]) {
  * count++; } } }
  */
 // 通过循环来做棋子相连的判断
 /*
  * int i = 1; while (color == allchess[x + i][y + 0]) { count++; i++; }
  * i = 1; while (color == allchess[x - i][y - 0]) { count++; i++; } if
  * (count >= 5) { flag = true; } // 纵向的判断 int i2 = 1 ; int count2 = 1 ;
  * while (color == allchess[x + 0][y + i2]) { count2++; i2++; } i2 = 1;
  * while (color == allchess[x - 0][y - i2]) { count2++; i2++; } if
  * (count2 >= 5) { flag = true ; } // 斜方向的判断(右上 + 左下) int i3 = 1 ; int
  * count3 = 1 ; while (color == allchess[x + i3][y - i3]) { count3++;
  * i3++; } i3 = 1; while (color == allchess[x - i3][y + i3]) { count3++;
  * i3++; } if (count3 >= 5) { flag = true ; } // 斜方向的判断(右下 + 左上) int i4 =
  * 1 ; int count4 = 1 ; while (color == allchess[x + i4][y + i4]) {
  * count4++; i4++; } i4 = 1; while (color == allchess[x - i4][y - i4]) {
  * count4++; i4++; } if (count4 >= 5) { flag = true ; }
  */

 // 判断横向
 count = this.checkcount(1, 0, color);
 if (count >= 5) {
  flag = true;
 } else {
  // 判断纵向
  count = this.checkcount(0, 1, color);
  if (count >= 5) {
  flag = true;
  } else {
  // 判断右上、左下
  count = this.checkcount(1, -1, color);
  if (count >= 5) {
   flag = true;
  } else {
   // 判断右下、左上
   count = this.checkcount(1, 1, color);
   if (count >= 5) {
   flag = true;
   }
  }
  }
 }

 return flag;
 }

 // 判断棋子连接的数量
 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;
 }

 public void run() {
 // todo auto-generated method stub
 // 判断是否有时间限制
 if (maxtime > 0) {
  while (true) {
  if (isblack) {
   blacktime--;
   if (blacktime == 0) {
   joptionpane.showmessagedialog(this, "黑方超时,游戏结束!");
   }
  } else {
   whitetime--;
   if (whitetime == 0) {
   joptionpane.showmessagedialog(this, "白方超时,游戏结束!");
   }
  }
  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) {
   // todo auto-generated catch block
   e.printstacktrace();
  }
  system.out.println(blacktime + " -- " + whitetime);
  }
 }
 }

}

然后再创建子框架界面:

package org.liky.game.frame;

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;

public class mychessframe extends jframe implements mouselistener {
 public mychessframe() {
 this.settitle("五子");
 this.setsize(1000, 700);
 this.setresizable(false) ;
 this.setdefaultcloseoperation(jframe.exit_on_close);
 int width = toolkit.getdefaulttoolkit().getscreensize().width;
 int height = toolkit.getdefaulttoolkit().getscreensize().height;
 /*system.out.println("宽度为: "+width);
 system.out.println("高度为: "+height);*/
 this.setlocation((width - 200)/2, (height-100)/2);
 
 this.addmouselistener(this);
 
 this.setvisible(true);
 }
 
 public void paint(graphics g) {
 /*g.drawstring("五子棋游戏", 20, 40);*/
 /*g.drawoval(20, 40, 40, 40);*/
 /*g.filloval(20, 40, 40, 40);*/
 /*g.drawline(20, 40, 80, 40);*/
 /* g.drawrect(20, 40, 40, 20);
 g.fillrect(80, 40, 40, 20);*/
 bufferedimage image = null;
 try {
  image = imageio.read(new file("e:/image/mldn文字01.jpg"));
 } catch (ioexception e) {
  // todo auto-generated catch block
  e.printstacktrace();
 }
 g.drawimage(image,0,0,this);
 g.drawoval(20, 40, 40, 40);
 g.setcolor(color.blue);
 g.fillrect(80, 40, 40, 20);
 g.setfont(new font("宋体",40,40)); 
 g.drawstring("五子棋游戏", 20, 100);
 }

 public void mouseclicked(mouseevent arg0) {
 // todo auto-generated method stub
 
 }

 public void mouseentered(mouseevent arg0) {
 // todo auto-generated method stub
 
 }

 public void mouseexited(mouseevent arg0) {
 // todo auto-generated method stub
 
 }

 public void mousepressed(mouseevent arg0) {
 // todo auto-generated method stub
 
 }

 public void mousereleased(mouseevent arg0) {
 // todo auto-generated method stub
 
 }
 

}


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: