好吧,我正在做一个简单的Tic Tac Toe计划。现在我正在使用MouseListener检查鼠标点击的位置,并查看鼠标单击的9个空格中的哪1个。我想检查以确保矩形检查正常工作,因此每次鼠标单击控制台时都会显示9个字母的字符串。字母是e =空x = X o = O.但似乎矩形似乎不包含任何坐标。以下是一些代码片段:
package com.blackattack.tictactoe;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
public class TicTacToePanel extends JPanel implements MouseListener {
Graphics2D g2d;
Rectangle[] bounds = new Rectangle[9];
TicTacToeLogic board = new TicTacToeLogic();
int STATE = 0;
final int PLAYING = 0;
final int LOSS = 1;
final int WIN = 2;
Win w = new Win(false, "e");
public TicTacToePanel() {
super();
addMouseListener(this);
bounds[0] = new Rectangle(0, 0, getWidth()/3, getHeight()/3);
bounds[1] = new Rectangle(getWidth()/3, 0, getWidth()/3, getHeight()/3);
bounds[2] = new Rectangle((getWidth()/3)*2, 0, getWidth()/3, getHeight()/3);
bounds[3] = new Rectangle(0, getHeight()/3, getWidth()/3, getHeight()/3);
bounds[4] = new Rectangle(getWidth()/3, getHeight()/3, getWidth()/3, getHeight()/3);
bounds[5] = new Rectangle((getWidth()/3)*2, getHeight()/3, getWidth()/3, getHeight()/3);
bounds[6] = new Rectangle(0, getHeight()/3, (getWidth()/3)*2, getHeight()/3);
bounds[7] = new Rectangle(getWidth()/3, (getHeight()/3)*2, getWidth()/3, getHeight()/3);
bounds[8] = new Rectangle((getWidth()/3)*2, (getHeight()/3)*2, getWidth()/3, getHeight()/3);
}
//paintComponent would be here
@Override
public void mouseClicked(MouseEvent e) {
int x = getX();
int y = getY();
if(bounds[0].contains(x, y)) {
board.changeState(0, "x");
w = board.isWin();
board.aiPlayerChoose();
w = board.isWin();
}
if(bounds[1].contains(x, y)) {
board.changeState(1, "x");
w = board.isWin();
board.aiPlayerChoose();
w = board.isWin();
}
if(bounds[2].contains(x, y)) {
board.changeState(2, "x");
w = board.isWin();
board.aiPlayerChoose();
w = board.isWin();
}
if(bounds[3].contains(x, y)) {
board.changeState(3, "x");
w = board.isWin();
board.aiPlayerChoose();
w = board.isWin();
}
if(bounds[4].contains(x, y)) {
board.changeState(4, "x");
w = board.isWin();
board.aiPlayerChoose();
w = board.isWin();
}
if(bounds[5].contains(x, y)) {
board.changeState(5, "x");
w = board.isWin();
board.aiPlayerChoose();
w = board.isWin();
}
if(bounds[6].contains(x, y)) {
board.changeState(6, "x");
w = board.isWin();
board.aiPlayerChoose();
w = board.isWin();
}
if(bounds[7].contains(x, y)) {
board.changeState(7, "x");
w = board.isWin();
board.aiPlayerChoose();
w = board.isWin();
}
if(bounds[8].contains(x, y)) {
board.changeState(8, "x");
w = board.isWin();
board.aiPlayerChoose();
w = board.isWin();
}
System.out.println(board.board[0] + " " + board.board[1] + " " + board.board[2] + " " + board.board[3] + " " + board.board[4] + " " + board.board[5] + " " + board.board[6] + " " + board.board[7] + " " + board.board[8]);
}
所以在这里我检查鼠标是否在9个空格中的一个中点击并输出“游戏板”这是TicTacToeLogic的一部分,这样你就可以更好地理解:
public class TicTacToeLogic {
String[] board = { "e", "e", "e", "e", "e", "e", "e", "e", "e" };
public TicTacToeLogic() {
}
public void changeState(int pos, String val) {
board[pos] = val;
}
public void aiPlayerChoose() {
boolean ready = true;
while (ready) {
Random r = new Random();
int which = r.nextInt(8);
if (board[which].equals("e")) {
board[which] = "o";
ready = false;
}
}
}
}
我似乎无法确定问题所在。当我打印出电路板[n]时,总会出现一堆电子邮件。它与我在构造函数中调用getWidth和getHeight有什么关系吗?谢谢你提前,
安德鲁
答案 0 :(得分:2)
你的理论认为这是通过在构造函数中调用getWidth和getHeight引起的,这是一个很好的理论。您可以通过检查调试器来验证这一点,甚至只需添加一些println或logging语句来查看这些方法返回的值。
如果这是问题(我猜它是),那么您可以通过将大小计算移动到调整大小时触发的侦听器/方法来修复它。
答案 1 :(得分:1)
getWidth()
和getHeight()
将返回0。当窗口布局时,这些值由LayoutManager设置。
您需要在mouseClicked
内计算您的界限。