java中repaint()方法的问题

时间:2017-07-21 14:27:10

标签: java swing tic-tac-toe

我是 Java 的新手,并尝试使用<创建一个简单的 Player vs. Computer&#34; Tic-Tac-Toe&#34; 游戏强>摇摆即可。在代码中,我使用paintComponent()方法绘制某些组件,并在播放器或计算机播放后调用repaint()方法。问题是,在我点击鼠标的第三个(有时是第二个)时间后,游戏会冻结。在GameWindow class中,我创建了Map class。

的实例
public class GameWindow extends JFrame{

    private final int sizeX = 3;
    private final int sizeY = 3;

    public GameWindow(){

        setLocation(400,150);
        setSize(406, 452);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        Map map = new Map(sizeX, sizeY);
        add(map, BorderLayout.CENTER);
        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new GridLayout(1, 2));
        add(bottomPanel, BorderLayout.SOUTH);

        Button newGame = new Button("Новая игра");
        newGame.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Начинаем новую игру");
            }
        });
        Button exit = new Button("Выход");
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        bottomPanel.add(newGame);
        bottomPanel.add(exit);

        setVisible(true);
    }
}

以下是Map类中鼠标单击的监听器:

public class Map extends JPanel {

private final int sizeX;
private final int sizeY;
private int cellWidth;
private int cellHeight;
private int cellX = -1;
private int cellY = -1;
private String lastPlayer = "nobody";

private String[][] table = new String[3][3];

public Map(int sizeX, int sizeY) {

    this.sizeX = sizeX;
    this.sizeY = sizeY;
    setBackground(Color.WHITE);

    for (int i = 0; i < table.length; i++) {

        for (int j = 0; j < table.length; j++) {

            table[i][j] = "empty";
        }
    }

    addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent e) {
            super.mouseReleased(e);

            cellX = e.getX() / cellWidth;
            cellY = e.getY() / cellHeight;

            if (!winOrDraw()) {
                boolean playerProgress = playerProgress();
                if(playerProgress) {
                    botProgress();
                }
            }
        }
    });
}

playerProgress()botProgress()方法中,我调用repaint()方法将新组件添加到屏幕上。我无法真正找出此代码中的问题以及为什么程序在实际第6次repaint()方法调用后会冻结?

这里有winOrDraw(),playerProgress()和botProgress()方法:

 private boolean winOrDraw(){

    for (int i = 0; i < table.length; i++) {

        if(table[i][0] == table[i][1] && table[i][1] == table[i][2] && !table[i][0].equals("empty")) return true;
        else if(table[0][i] == table[1][i] && table[1][i] == table[2][i] && !table[0][i].equals("empty")) return true;
        else if(table[0][0] == table[1][1] && table[1][1] == table[2][2] && !table[0][0].equals("empty")) return true;
        else if(table[2][0] == table[1][1] && table[1][1] == table[0][2] && !table[2][0].equals("empty")) return true;
    }

    int i, j = 0;
    for (i = 0; i < table.length; i++) {

        for (j = 0; j < table.length; j++) {

            if(table[i][j].equals("empty")) break;
        }
    }
    if(i == table.length && j == table.length) { lastPlayer = "nobody"; return true; }
    else return false;
}

private boolean playerProgress(){

    if(!table[cellY][cellX].equals("empty")){
        System.out.println("This cell is not empty");
        return false;
    } else {
        lastPlayer = table[cellY][cellX] = "player";
        repaint();
        return true;
    }
}

private void botProgress(){

    do{
        if(winOrDraw()){

            switch (lastPlayer) {
                case "player":

                    System.out.println("You won!");
                    System.exit(0);
                case "bot":

                    System.out.println("Bot won");
                    System.exit(0);
                default:

                    System.out.println("Draw");
                    System.exit(0);
            }
        }
        Random random = new Random();
        cellX = random.nextInt(2);
        cellY = random.nextInt(2);

        if(table[cellY][cellX].equals("empty")) {

            lastPlayer = table[cellY][cellX] = "bot";
            break;
        }
    }while (!table[cellY][cellX].equals("empty"));

    System.out.println("Bot's turn");
    repaint();
}

1 个答案:

答案 0 :(得分:0)

您正在将Swing组件直接添加到JFrame-AWT容器中。你不应该这样做。相反,你应该这样做:

JPanel panel = new JPanel();
myFrame.setContentPane(panel);

panel.setLayout(...)
panel.add(...);
panel.validate();

这可以解决您的问题。