我怎样画到我的8x8 reversi(othello)游戏板上?

时间:2014-04-08 03:10:42

标签: java user-interface

我正在制作一款逆转游戏。我决定使用JLabels创建我的主板。我遇到的问题是将四个起始部分放到板上。这只是一个2人游戏,它是1v1。在游戏开始时,每个玩家在棋盘中间的两个棋子开始。在我的计划中,我会为2名玩家创建2个蓝色和2个红色棋子,但无论我做什么,我都无法将这些棋子展示在游戏板上。当我运行我的调试器时,看起来我从未给每个BoardSquare提供正确的x和y值。我不知道如何获得BoardSquares的x和y值。任何和所有帮助/批评都是受欢迎的。我一直试图解决这个问题几个小时。谢谢。

public class ReversiRemake {

    public static void main(String[] args) {
        PlayGame game = new PlayGame();
    }
}

class BoardSquare extends JLabel {

    private int x;
    private int y;
    private int row;
    private int column;
    private MyShape circle;
    private Border b = BorderFactory.createLineBorder(Color.BLACK, 2);

    BoardSquare(int row, int column) {
        addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    if (e.getButton() == 1) {

                    }

                }
            });
        this.row = row;
        this.column = column;
        setBorder(b);


    }
    public void setShape(Player p){

        if(p instanceof HumanPlayer){
            circle = new MyShape(x, y, Color.blue); 
        }
        if(p instanceof aiPlayer){
            circle = new MyShape(x, y, Color.red);
        }

    }
    public void setX(int x){
        this.x = x;
    }
    public void setY(int y){
        this.y = y;
    }
    public MyShape getShape() {
        return circle;
    }

    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        circle.drawShape(g2);
    }

}

class MyShape {

    private Color color;
    private int x;
    private int y;

    MyShape(int x, int y, Color color) {
        this.x = x;
        this.y = y;
        this.color = color;
    }

    MyShape(Color color) {
        this.color = color;
    }

    public void drawShape(Graphics2D g) {

        g.setPaint(color);
        g.drawOval(x + 5, y + 2, 50, 50);
        g.fillOval(x + 5, y + 2, 50, 50);

    }

    public void setColor(Player p) {
        if (p instanceof HumanPlayer) {
            color = Color.BLUE;
        }
        if (p instanceof aiPlayer) {
            color = Color.red;
        }
    }
}

class GameBoard extends JPanel {

    BoardSquare[][] BoardSquares = new BoardSquare[8][8];
    private BoardSquare bs;

    GameBoard() {
        for (int row = 0; row < 8; row++) {
            for (int column = 0; column < 8; column++) {
                BoardSquares[row][column] = new BoardSquare(row, column);
                add(BoardSquares[row][column]);

            }
        }
    }

    public void StartingPieces(HumanPlayer p1, aiPlayer p2) {
        for (int row = 0; row < 8; row++) {
            for (int column = 0; column < 8; column++) {
                BoardSquares[row][column] = bs;
                if (row == 3 && column == 3) {
                    bs.setShape(p1);
                }
                if (row == 3 && column == 4) {
                    bs.setShape(p2);
                }
                if (row == 4 && column == 3) {
                    bs.setShape(p2);
                }
                if (row == 4 && column == 4) {
                    bs.setShape(p1);
                }
            }
        }
    }
}

abstract class Player {

    int playerType;
    Color color;
    int score;

    Player(int playerType, Color color, int score) {
        this.playerType = playerType;
        this.color = color;
        this.score = score;
    }

}

class HumanPlayer extends Player {

    HumanPlayer() {
        super(1, Color.BLUE, 0);

    }
}

class aiPlayer extends Player {

    aiPlayer() {
        super(2, Color.RED, 0);
    }

}

class PlayGame {

    private HumanPlayer p1 = new HumanPlayer();
    private aiPlayer p2 = new aiPlayer();
    private GameBoard gameBoard = new GameBoard();
    private boolean p1Turn;
    private boolean p2Turn;

    PlayGame() {
        setStartingPieces();
    }

    public void setStartingPieces() {
        gameBoard.StartingPieces(p1, p2);
    }
}

1 个答案:

答案 0 :(得分:0)

我无法在发布时运行您的代码,但我相信在线检查

BoardSquares[row][column] = bs;

应该是

bs = BoardSquares[row][column];

如上所述,会发生NullPointerException,并且bs实际上从未设置过。我提供的修订版允许在GameBoard类中设置形状。