五子棋

五子棋游戏源代码(Java)

//package com.gobang;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Gobang {
    JFrame frame = new JFrame("五子棋游戏");

    // 声明四个BufferedImage对象,分别记录四张图片
    BufferedImage table;
    BufferedImage black;
    BufferedImage white;

    //BufferedImage selected;

    // 声明棋盘的宽和高
    final int TABLE_WIDTH = 450;
    final int TABLE_HIGHT = 450;

    // 声明棋盘横向和纵向分别可以下多少个子
    final int BOARD_SIZE = 15;

    // 声明每个棋子占用棋盘的比率
    final int RATE = TABLE_WIDTH / BOARD_SIZE;

    // 声明变量,记录棋子的偏移量
    final int X_OFFSET = 3;
    final int Y_OFFSET = 3;

    // 声明一个二维数组,判断棋盘上某个点是否有棋子,0-没有棋子,1-黑子,2-白子
    int[][] chess = new int[BOARD_SIZE][BOARD_SIZE];

    // 声明红色选框的坐标
    int selected_X = -1;
    int selected_Y = -1;


    // 自定义画布类
    private class ChessBoard extends JPanel {
        @Override
        public void paint(Graphics g) {
            // 绘图

            // 绘制棋盘
            g.drawImage(table, 0, 0, null);

            // 绘制黑子
            for (int i = 0; i < BOARD_SIZE; i++) {
                for (int j = 0; j < BOARD_SIZE; j++) {
                    if (chess[i][j] == 1) {
                        // 表明要绘制黑子
                        g.drawImage(black, X_OFFSET + i * RATE, Y_OFFSET + j * RATE, null);
                    }
                }
            }

            // 绘制白子
            for (int i = 0; i < BOARD_SIZE; i++) {
                for (int j = 0; j < BOARD_SIZE; j++) {
                    if (chess[i][j] == 2) {
                        // 表明要绘制白子
                        g.drawImage(white, X_OFFSET + i * RATE, Y_OFFSET + j * RATE, null);
                    }
                }
            }
        }
    }

    ChessBoard chessBoard = new ChessBoard();

    // 定义一个面板
    Panel p = new Panel();

    // 定义两个按钮
    Button blackBtn = new Button("黑子");
    Button whiteBtn = new Button("白子");
    Button deleteBtn = new Button("删除");

    // 声明变量,记录当前要下的棋子
    int board_type = 1;

    // 刷新按钮颜色的方法
    public void refreshBtnColor(Color blackBtnColor,Color whiteBtnColor,Color deleteBtnColor ){
        blackBtn.setBackground(blackBtnColor);
        whiteBtn.setBackground(whiteBtnColor);
        deleteBtn.setBackground(deleteBtnColor);
    }

    // 组装视图
    public void init() {

        blackBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 修改记录棋子的标记值为1
                board_type = 1;
                // 刷新按钮的颜色
                refreshBtnColor(Color.GREEN,Color.GRAY,Color.GRAY);
            }
        });

        whiteBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 修改记录棋子的标记值2
                board_type = 2;
                // 刷新按钮的颜色
                refreshBtnColor(Color.GRAY,Color.GREEN,Color.GRAY);
            }
        });

        deleteBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 修改记录棋子的标记值0
                board_type = 0;
                // 刷新按钮的颜色
                refreshBtnColor(Color.GRAY,Color.GRAY,Color.GREEN);
            }
        });

        // 组装底部的按钮
        blackBtn.setFont(new Font("楷体",Font.BOLD,12));
        whiteBtn.setFont(new Font("楷体",Font.BOLD,12));
        deleteBtn.setFont(new Font("楷体",Font.BOLD,12));

        p.add(blackBtn);
        p.add(whiteBtn);
        p.add(deleteBtn);

        frame.add(p,BorderLayout.SOUTH);

        // 组装棋盘

        // 图片初始化
        try {
        // 参数为图片的绝对路径
            table = ImageIO.read(new File("F:\\java\\IntelliJ idea Project\\AWT\\imag\\board.png"));
            black = ImageIO.read(new File("F:\\java\\IntelliJ idea Project\\AWT\\imag\\black.png"));
            white = ImageIO.read(new File("F:\\java\\IntelliJ idea Project\\AWT\\imag\\white.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }


        // 处理鼠标移动
        chessBoard.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                selected_X = (e.getX() - X_OFFSET) / RATE;
                selected_Y = (e.getX() - Y_OFFSET) / RATE;

                chessBoard.repaint();
            }
        });

        // 处理鼠标点击
        chessBoard.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // 将鼠标焦点的坐标转化为棋盘上棋子的索引
                int xPos = (e.getX() - X_OFFSET) / RATE;
                int yPos = (e.getY() - Y_OFFSET) / RATE;

                // 确定该点要放置什么样的棋子
                chess[xPos][yPos] = board_type;

                // 重绘
                chessBoard.repaint();
            }

            // 当鼠标移动到绘图区域外时,调用该方法
            @Override
            public void mouseExited(MouseEvent e) {
                selected_X = -1;
                selected_Y = -1;

                chessBoard.repaint();
            }
        });


        chessBoard.setPreferredSize(new Dimension(TABLE_WIDTH,TABLE_HIGHT));
        frame.add(chessBoard);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });


        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Gobang().init();
    }
}

棋盘:450px × 450px
棋盘
棋子:25px × 25px
在这里插入图片描述
在这里插入图片描述

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页