package wuziqi;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class pan extends JPanel implements MouseListener{
int i=0,j=0;
int row = 15;
// 数组下标
int col = 15;
// 数组上标
String qipan_name = "qipan1.jpg";
String qizi1_name = "c1.png";
String qizi2_name = "c2.png";
int num[][] = new int[row][col];
// 0:标示该位置为空,1:标示红棋子,2:标示黑棋子
boolean canSetqizi = true;
// 定义boolean值,用来判断该位置是否有子
int qizi_num = 0;
// 定义记录落子数
List<paihangbang> paihanglist = new ArrayList();
// 定义集合,用来存储排行榜
public void paint(Graphics g){
super.paint(g);
Image img= new ImageIcon("img/"+qipan_name).getImage();
// 调入棋盘图片
g.drawImage(img, 0, 0, 567, 567, this);
// 绘制棋盘
Image c1= new ImageIcon("img/"+qizi1_name).getImage();
Image c2= new ImageIcon("img/"+qizi2_name).getImage();
for(int i = 0;i<num.length;i++){
for(int j = 0;j<num[i].length;j++){
if(num[i][j] == 1)
{
g.drawImage(c1, i*35+20, j*35+20, 35, 35, this);
}
else if(num[i][j] == 2)
{
g.drawImage(c2, i*35+20, j*35+20, 35, 35, this);
}
}
// 重绘棋子
}
// 重载整个界面(防止最小化后原内容丢失)
}
public int[] getLoc(int x,int y) {
int count = 1;
// 定义计数器,用于计算棋子数
int[] wz1 = null;
int[] wz2 = null;
// 定义数组,用来存储危险位置
for(int i =x-1;i>=0;i--){
if(num[i][y] == num[x][y])
{
count++;
}
else
{
if(num[i][y] == 0){
wz1 = new int[]{i,y};
// 获取左边的危险位置坐标
}
break;
}
}
// 左
for(int i =x+1;i<row;i++){
if(num[i][y] == num[x][y])
{
count++;
}else{
if(num[i][y] == 0){
wz2 = new int[]{i,y};
// 获取有变位置危险坐标
}
break;
}
}
// 右
if(count>=3)
{
if(wz1 != null){
return wz1;
// 判断返回左边危险位置
}else if(wz2 != null){
return wz2;
// 判断返回右边危险位置
}else{
return null;
}
}
// 左右
count = 1;
wz1 = null;
wz2 = null;
// 初始化所有参数
for(int j =y-1;j>=0;j--){
if(num[x][j] == num[x][y])
{
count++;
}
else
{
if(num[x][j] == 0){
wz1 = new int[]{x,j};
}
break;
}
}
// 上
for(int j =y+1;j<col;j++){
if(num[x][j] == num[x][y])
{
count++;
}else{
if(num[x][j] == 0){
wz2 = new int[]{x,j};
}
break;
}
}
// 下
if(count>=3)
{
if(wz1 != null){
return wz1;
}else if(wz2 != null){
return wz2;
}else{
return null;
}
}
// 上下
count = 1;
wz1 = null;
wz2 = null;
for(int i =x-1,j =y-1;i>=0&&j>=0;i--,j--){
if(num[i][j] == num[x][y])
{
count++;
}
else
{
if(num[i][j] == 0){
wz1 = new int[]{i,j};
}
break;
}
}
// 左上
for(int i =x+1,j =y+1;i<row&&j<col;i++,j++){
if(num[i][j] == num[x][y])
{
count++;
}else{
if(num[i][j] == 0){
wz2 = new int[]{i,j};
}
break;
}
}
// 右下
if(count>=3)
{
if(wz1 != null){
return wz1;
}else if(wz2 != null){
return wz2;
}else{
return null;
}
}
// 左上右下
count = 1;
wz1 = null;
wz2 = null;
for(int i =x-1,j =y+1;i>=0&&j<col;i--,j++){
if(num[i][j] == num[x][y])
{
count++;
}
else
{
if(num[i][j] == 0){
wz1 = new int[]{i,j};
}
break;
}
}
// 左下
for(int i =x+1,j =y-1;i<row&&j>=0;i++,j--){
if(num[i][j] == num[x][y])
{
count++;
}else{
if(num[i][j] == 0){
wz2 = new int[]{i,j};
}
break;
}
}
// 右上
if(count>=3)
{
if(wz1 != null){
return wz1;
}else if(wz2 != null){
return wz2;
}else{
return null;
}
}
// 左下右上
return null;
}
public boolean iswin(int x,int y){
int count = 1;
for(int i =x-1;i>=0;i--){
if(num[i][y] == num[x][y])
{
count++;
}
else
{
break;
}
}
// 左
for(int i =x+1;i<row;i++){
if(num[i][y] == num[x][y])
{
count++;
}else{
break;
}
}
// 右
if(count>=5)
{
return true;
}
// 左右
count = 1;
for(int j =y-1;j>=0;j--){
if(num[x][j] == num[x][y])
{
count++;
}
else
{
break;
}
}
// 上
for(int j =y+1;j<col;j++){
if(num[x][j] == num[x][y])
{
count++;
}else{
break;
}
}
// 下
if(count>=5)
{
return true;
}
// 上下
count = 1;
for(int i =x-1,j =y-1;i>=0&&j>=0;i--,j--){
if(num[i][j] == num[x][y])
{
count++;
}
else
{
break;
}
}
// 左上
for(int i =x+1,j =y+1;i<row&&j<col;i++,j++){
if(num[i][j] == num[x][y])
{
count++;
}else{
break;
}
}
// 右下
if(count>=5)
{
return true;
}
// 左上右下
count = 1;
for(int i =x-1,j =y+1;i>=0&&j<col;i--,j++){
if(num[i][j] == num[x][y])
{
count++;
}
else
{
break;
}
}
// 左下
for(int i =x+1,j =y-1;i<row&&j>=0;i++,j--){
if(num[i][j] == num[x][y])
{
count++;
}else{
break;
}
}
// 右上
if(count>=5)
{
return true;
}
// 左下右上
return false;
}
// 判断输赢
@Override
public void mouseClicked(MouseEvent e) {
if(canSetqizi){
Graphics g = this.getGraphics();
int checkusersuccess = 0;
// 标示是否落子成功
int x = e.getX();
int y = e.getY();
// 获取鼠标点击的位置
Image c1= new ImageIcon("img/"+qizi1_name).getImage();
int i = (x-25)/35;
int j = (y-75)/35;
if(num[i][j] != 0){
JOptionPane.showMessageDialog(null, "该位置有旗子,请重新落子");
return;
// 判断有子,终止本次事件,进行下次事件触发
}else{
g.drawImage(c1, i*35+20, j*35+20, 35, 35, this);
// 画出玩家落子
num[i][j] = 1;
// 给数组付一个只值,表示该位置有旗子
checkusersuccess = 1;
// 标示量标示
qizi_num++;
// 记录玩家落子步数
}
boolean b=iswin(i,j);
if(b){
JOptionPane.showMessageDialog(null, "你赢了!");
canSetqizi = false;
paihangbang ph = new paihangbang();
ph.setJushu(paihanglist.size()+1);
ph.setBushu(qizi_num);
ph.setJieguo("win");
paihanglist.add(ph);
return;
}
// 调用boolean判断方法
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
// 时间间隔:玩家落子后的等待
Random r = new Random();
Image c2 = new ImageIcon("img/"+qizi2_name).getImage();
// 调入黑棋子图片
do{
int[] wz =getLoc(i, j);
if(wz == null){
i = r.nextInt(15);
j = r.nextInt(15);
}else{
i=wz[0];
j=wz[1];
}
// 设置随机的坐标
}while(num[i][j] != 0);
g.drawImage(c2, i*35+20, j*35+20, 35, 35, this);
num[i][j] = 2;
boolean d=iswin(i,j);
if(d){
JOptionPane.showMessageDialog(null, "你输了!");
canSetqizi = false;
paihangbang ph = new paihangbang();
ph.setJushu(paihanglist.size()+1);
ph.setBushu(qizi_num);
ph.setJieguo("fail");
paihanglist.add(ph);
return;
}
// 随机电脑落子位置;
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
Java Swing五子棋游戏项目是一款基于Java编程语言的桌面应用程序,使用Swing库来构建用户界面。Swing是Java Standard Edition (Java SE)的一部分,它提供了丰富的组件集,用于创建功能丰富的图形用户界面(GUI)。这个项目的源代码是学习和理解Java GUI编程,特别是Swing应用的一个宝贵资源。 让我们深入了解Swing库。Swing是Java AWT(Abstract Window Toolkit)的扩展,提供了更多的组件和功能。它使用轻量级组件模型,这使得Swing应用在不同平台上表现一致,而无需依赖底层操作系统。在Swing中,我们通常会用JFrame作为主窗口,然后在其中添加各种组件,如JButton、JLabel、JPanel等。 在五子棋游戏中,Swing被用来创建棋盘界面,可能使用了JPanel来绘制棋盘格子,并通过重写paintComponent方法来实现图形绘制。每个棋子可能是由一个自定义的JComponent类表示,根据玩家的点击位置在棋盘上放置。为了实现游戏逻辑,开发者可能使用二维数组来存储棋盘状态,每行每列的元素代表对应位置的棋子颜色。 游戏的核心算法是检查是否有五个同色的棋子连成一线。这通常涉及到水平、垂直以及两个对角线方向的搜索。开发者可能会使用递归或循环来遍历所有可能的连线,一旦找到五个连续的棋子,就判定游戏结束。 此外,项目中的“QQ截图20150708212301.png”可能是一个游戏界面的截图,展示了实际运行时的外观和布局。这可以帮助初学者理解如何将代码与实际视图关联起来。 关于标签中的“JAVA小游戏”,这个五子棋项目是Java编程实践的一个简单示例,适合初学者进行GUI编程和游戏开发的入门。通过分析和修改源代码,学习者可以加深对事件处理(如鼠标点击事件)、多线程(用于交替玩家操作)和游戏逻辑的理解。 "JAVA swing五子棋游戏项目软件源代码"是一个很好的学习资源,它涵盖了Java Swing的基础知识,包括组件使用、图形绘制、事件处理以及简单的游戏逻辑实现。无论是对于希望提升Java GUI编程技能的初学者,还是寻求实践经验的开发者,都有很高的参考价值。通过阅读和调试这个项目,你可以掌握如何利用Swing构建一个交互式的桌面应用程序,并从中体会到编程的乐趣。

































































- 1

- #完美解决问题
- #运行顺畅
- #内容详尽
- #全网独家
- #注释完整
- m0_705090562022-11-06实在是宝藏资源、宝藏分享者!感谢大佬~

- 粉丝: 2987
- 资源: 7733





我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- [AB PLC例程源码][MMS_043640]DeviceNet Diagnostic.zip
- [AB PLC例程源码][MMS_039840]ASCII_Messaging.zip
- [AB PLC例程源码][MMS_040306]Configuration of AMCI 3601 Single Axis Stepper Motor Controller with.zip
- [AB PLC例程源码][MMS_040307]MD60, MD65 Drives control with CompactLogix.zip
- [AB PLC例程源码][MMS_040305]ASCII Read , Write for CompactLogix & Bul90.zip
- [AB PLC例程源码][MMS_040316]DINT Conversion of INT data type and Arithmatic comparison .zip
- [AB PLC例程源码][MMS_040315]Double INC and Double DEC of INT datatype.zip
- [AB PLC例程源码][MMS_040317]DINT Conversion of INT data type and Arithmetic operations like Div, Mul.zip
- [AB PLC例程源码][MMS_040319]Swaps Byte in an array.zip
- [AB PLC例程源码][MMS_040318]DINT data type to String data type conversion.zip
- [AB PLC例程源码][MMS_040322]Decoding of Boolean array.zip
- [AB PLC例程源码][MMS_040323]Encoding of Boolean array.zip
- [AB PLC例程源码][MMS_040324]Value search in an array.zip
- [AB PLC例程源码][MMS_040325]Bubble sorting.zip
- [AB PLC例程源码][MMS_040326]Level control using FBD.zip
- [AB PLC例程源码][MMS_040327]Ramp Control of Electrical parameters.zip


