用Java实现计算机围板的简单方法

用Java实现计算机围板的简单方法,java,graphics,Java,Graphics,我想做一个简单的围棋板来设计游戏 在围棋游戏中,你将一块“石头”(白色或黑色)放在水平线和垂直线相交的位置上 有哪些简单的方法可以限制用户将石头放在其他位置? 也许我只是没有看到一个简单的解决方案 编辑 我想我应该把我的问题改写得更好: 我想知道如何做围棋板的背景图像,这样我就可以把我的石头放在水平线和垂直线的交点上。我想得到一个普通的棋盘图像,当我实际渲染石头时,我会找到正确的像素位置来放置石头。然而,这个解决方案似乎不是最好的解决方案,因为我需要担心石头图像的大小,并在扩展或缩小板窗口时考虑

我想做一个简单的围棋板来设计游戏

在围棋游戏中,你将一块“石头”(白色或黑色)放在水平线和垂直线相交的位置上

有哪些简单的方法可以限制用户将石头放在其他位置?
也许我只是没有看到一个简单的解决方案

编辑
我想我应该把我的问题改写得更好:
我想知道如何做围棋板的背景图像,这样我就可以把我的石头放在水平线和垂直线的交点上。我想得到一个普通的棋盘图像,当我实际渲染石头时,我会找到正确的像素位置来放置石头。然而,这个解决方案似乎不是最好的解决方案,因为我需要担心石头图像的大小,并在扩展或缩小板窗口时考虑比例问题

别想台词。想想十字路口。交点可以表示为网格,就像棋盘上的正方形形成网格一样

在Java中,最简单的表示形式是正方形数组。比如:

Location[MAX_Y][MAX_X];
其中,
Location
是一个表示交叉点的对象,并包含放置在该交叉点上的工件(如果有)的参考

这是你的网格。此数组中的每个位置表示一个直线交点。

我在这里使用枚举:

enum Stone {BLAC, WHITE, NONE}

class Board {
    static final int dimension = 19;
    private Stone[][] board = new Stone[dimension][dimension];
    // ...
}
编辑

这里有一个小演示(没有调整电路板的大小,也没有图像,只有很好的旧图形!):

import java.awt.*;
导入java.awt.event.*;
导入java.util.Random;
导入javax.swing.*;
公共类GoBoardDemo{
公共静态void main(字符串[]args){
JFrame=新JFrame();
frame.setLayout(新的BorderLayout());
框架。添加(新GoPanel(19),边框布局。中心);
框架。设置尺寸(600625);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setresizeable(false);
frame.setVisible(true);
}
}
@抑制警告(“串行”)
类GoPanel扩展了JPanel{
方形[]板;
布尔白动;
GoPanel(内部尺寸){
板=新方形[尺寸][尺寸];
whiteToMove=真;
初始板(尺寸);
}
专用电路板(内部尺寸){
super.setLayout(新网格布局(维度、维度));
对于(int row=0;row

其中的随机内容是CPerkins所说的有机感觉的实现。不管怎样,我还是试着去做。

如何实施立场已经得到了回答。但你问的是如何限制用户的位置,这是另一回事

假设您计划创建一个绘图区域,您将检测到鼠标单击。如何将石头放置限制在交叉点,就像绘图程序实现一个称为“捕捉”的功能(如“捕捉到栅格”)

基本上,这只意味着减少在每个维度的一个像素范围内的点击,使之成为一条线。在两个维度中执行此操作会将移动的单击放置在一个点上

<
enum Stone {BLAC, WHITE, NONE}

class Board {
    static final int dimension = 19;
    private Stone[][] board = new Stone[dimension][dimension];
    // ...
}
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

public class GoBoardDemo {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.add(new GoPanel(19), BorderLayout.CENTER);
        frame.setSize(600, 625);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setVisible(true);
    }
}


@SuppressWarnings("serial")
class GoPanel extends JPanel {

    Square[][] board;
    boolean whiteToMove;

    GoPanel(int dimension) {
        board = new Square[dimension][dimension];
        whiteToMove = true;
        initBoard(dimension);
    }

    private void initBoard(int dimension) {
        super.setLayout(new GridLayout(dimension, dimension));
        for(int row = 0; row < dimension; row++) {
            for(int col = 0; col < dimension; col++) {
                board[row][col] = new Square(row, col);
                super.add(board[row][col]);
            }
        }
        repaint();
    }

    private class Square extends JPanel {

        Stone stone;
        final int row;
        final int col;

        Square(int r, int c) {
            stone = Stone.NONE;
            row = r;
            col = c;
            super.addMouseListener(new MouseAdapter(){
                @Override
                public void mouseClicked(MouseEvent me) {
                    if(stone != Stone.NONE) return;
                    stone = whiteToMove ? Stone.WHITE : Stone.BLACK;
                    whiteToMove = !whiteToMove;
                    repaint();
                }
            });
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int w = super.getWidth();
            int h = super.getHeight();
            g.setColor(new Color(0xB78600));
            g.fillRect(0, 0, w, h);
            g.setColor(Color.BLACK);   
            if(row == 0 || row == board.length-1 || col == 0 || col == board.length-1) {
                if(col == 0) {
                    g.drawLine(w/2, h/2, w, h/2);
                    if(row == 0) g.drawLine(w/2, h/2, w/2, h);
                    else if(row == 18) g.drawLine(w/2, h/2, w/2, 0);
                    else g.drawLine(w/2, 0, w/2, h);
                }
                else if(col == 18) {
                    g.drawLine(0, h/2, w/2, h/2);
                    if(row == 0) g.drawLine(w/2, h/2, w/2, h);
                    else if(row == 18) g.drawLine(w/2, h/2, w/2, 0);
                    else g.drawLine(w/2, 0, w/2, h);
                }
                else if(row == 0) {
                    g.drawLine(0, h/2, w, h/2);
                    g.drawLine(w/2, h/2, w/2, h);
                }
                else {
                    g.drawLine(0, h/2, w, h/2);
                    g.drawLine(w/2, h/2, w/2, 0);
                }
            } else {
                g.drawLine(0, h/2, w, h/2);
                g.drawLine(w/2, 0, w/2, h);
            }
            stone.paint(g, w);
        }
    }
}

enum Stone { 

    BLACK(Color.BLACK), WHITE(Color.WHITE), NONE(null);

    final Color color;
    private final static Random rand = new Random();

    private Stone(Color c) {
        color = c;
    }

    public void paint(Graphics g, int dimension) {
        if(this == NONE) return;
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(color);
        int x = 5;
        g2d.fillOval(rand.nextInt(x), rand.nextInt(x), dimension-x, dimension-x);
    }
}

    // Since traditionally, boards are not square, you'd call this twice: once with the
    //   width in X for the X click, and once again for Y.
    // Naturally, you'll want to accomodate e.g., 9x9 boards for short games.
    int snapClick (int gridWidth, int clickPos, int numCells) {
        int cellWidth = (int) (gridWidth / numCells);
        int snappedClick = Math.round ((clickPos + (cellWidth/2)) / cellWidth);
        return snappedClick;
    }