java AWT 五子棋

Frame来绘图更新时会有闪烁,JFrame做为窗口,JPanel实现画图区可以避闪烁。

package javaAWT;

import java.awt.CheckboxMenuItem;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Gobang {
    // 定义代表棋盘,黑子,白子   位图
    BufferedImage table;
    BufferedImage black;
    BufferedImage white;
    //当鼠标移动时的选择框
    BufferedImage selected;
    // 棋盘大小,每行列棋子个数
    private static int BOARD_SIZE = 15;
    private final int TABLE_WIDTH = 535;
    private final int TABLE_HEIGHT = 536;
    // 定义棋盘坐标的像素值和棋盘数组之间的比率
    private final int RATE = TABLE_WIDTH / BOARD_SIZE;
    // 定义棋盘坐标的像素值和棋盘数组之间的偏移距离
    private final int X_OFFSET = 5;
    private final int Y_OFFSET = 6;
    // 定义一个二维数组来充当棋盘
    private String[][] board = new String[BOARD_SIZE][BOARD_SIZE];
    // 游戏窗口
    JFrame frame = new JFrame("Gobang");
    // 棋盘对应的Canvase组件
    ChessBoard chessBoard = new ChessBoard();
    //当前选中点的坐标
    private int selectedX = -1;
    private int selectedY = -1;

    public void init() throws Exception{
        table = ImageIO.read(new File("image/board.jpg"));
        black = ImageIO.read(new File("image/black.gif"));
        white = ImageIO.read(new File("image/white.gif"));

        selected = ImageIO.read(new File("image/selected.gif"));


        // 把每个元素赋值为“+”,代表没有棋子
        for(int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                board[i][j] = "+";
            }
        }

        chessBoard.setPreferredSize(new Dimension(TABLE_WIDTH, TABLE_HEIGHT));
        chessBoard.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                super.mouseClicked(e);
                // 将用户鼠标事件的坐标转换成棋子数组的坐标
                int xPos = (int)((e.getX() - X_OFFSET) / RATE);
                int yPos = (int)((e.getY() - Y_OFFSET) / RATE);
                board[xPos][yPos] = "o";
                /*
                 * 电脑随机生成两个整数,作为电脑下棋的坐标,
                 * 1.如果下棋的点已经有棋子,不能重复下棋
                 * 2.每次下棋后,需要扫描谁赢了
                 * */
                chessBoard.repaint();

            }

            // 当鼠标退出棋盘区后,复位选中点的坐标              
            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub
                super.mouseExited(e);
                selectedX = -1;
                selectedY = -1;
            }

        });


        chessBoard.addMouseMotionListener(new MouseMotionListener() {

            @Override
            public void mouseMoved(MouseEvent e) {
                // TODO Auto-generated method stub
                // 当鼠标移动时,改变选中点的坐标
                selectedX = (e.getX() - X_OFFSET) / RATE;
                selectedY = (e.getY() - Y_OFFSET) / RATE;
                chessBoard.repaint();
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });

        frame.add(chessBoard);
        frame.pack();
        frame.setVisible(true);

    }

    class ChessBoard extends JPanel {
        //重写JPanel的paint方法,实现绘图
        @Override
        public void paint(Graphics g) {
            // TODO Auto-generated method stub
            super.paint(g);
            // 绘制棋盘
            g.drawImage(table, 0, 0, null);
            // 绘制选中点的红框 
            if (selectedX >= 0 && selectedY >= 0) {
                g.drawImage(selected, selectedX * RATE + X_OFFSET,
                        selectedY * RATE + Y_OFFSET, null);
            }
            // 遍历数组,绘制棋子
            for (int i = 0; i < BOARD_SIZE; i++) {
                for(int j = 0; j < BOARD_SIZE; j++) {
                    // 绘制黑棋
                    if (board[i][j].equals("o")) {
                        g.drawImage(black, i * RATE + X_OFFSET, 
                                j * RATE + Y_OFFSET, null);
                    }
                    // 绘制白棋
                    if (board[i][j].equals("O")) {
                        g.drawImage(white, i * RATE + X_OFFSET, 
                                    j * RATE + Y_OFFSET, null);
                    }

                }
            }

        }
    }

    public static void main(String[] args) throws Exception {
        Gobang gobang = new Gobang();
        gobang.init();

    }
}

java AWT 五子棋_第1张图片

你可能感兴趣的:(java,GUI)