检查点是否包含在矩形内时出错
好吧,我正在制作一个简单的 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 个空格中的 1 个,然后输出“游戏板” 这是 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;
}
}
}
}
我似乎无法查明问题所在。当我打印出 board[n] 时,它总是显示为一堆 e。这和我在构造函数中调用 getWidth 和 getHeight 有关系吗?预先感谢,
安德鲁
Alright so I am making a simple Tic Tac Toe program. Right now I am working on checking where the mouse clicked, using MouseListener, and seeing in which 1 of 9 spaces the mouse clicked. I wanted to check to make sure that the rectangle checking was working so everytime the mouse clicked the console would display a String of 9 letters. The letters are e = empty x = X o = O. But it doesnt seem that the rectangle doesn't seem to contain any coordinates . Here is some snippets of code:
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]);
}
So here I check to see if the mouse clicked in 1 of the 9 spaces and I output the "game board" Here is part of TicTacToeLogic so you can have a better understanding:
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;
}
}
}
}
I can't seem to pinpoint what the problem is. When ever i print out board[n] it always comes up as a bunch of e's. Does it have something to do with me calling getWidth and getHeight in the constructor? Thanks in advanced,
Andrew
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您认为这是由在构造函数中调用 getWidth 和 getHeight 引起的理论是一个很好的理论。您可以通过检查调试器来验证这一点,甚至只是添加一些 println 或日志记录语句来查看这些方法返回的值。
如果这就是问题所在(我猜是这样),那么您可以通过将大小计算移动到在调整大小时触发的侦听器/方法来修复它。
Your theory that this is caused by calling getWidth and getHeight in the constructor is a good one. You could verify this by checking with a debugger, or even just adding some println or logging statements to see what values those methods are returning.
If that is the problem (and I'm guessing it is) then you could fix it by moving your size calculations to a listener/method that fires on resize.
当您在构造函数中时,
getWidth()
和getHeight()
将返回 0。当窗口布局时,这些值由 LayoutManager 设置。您需要从
mouseClicked
内计算您的边界。getWidth()
andgetHeight()
will return 0 when you're in the constructor. Those values get set by the LayoutManager when the window gets laid out.You need to calculate your bounds from within
mouseClicked
.