使用 getHeight() 和 getWidth() 时,矩形未使用包含正确边界的矩形
我一直在兜圈子,试图找出为什么这不起作用。我有一个矩形,它位于 0,0 处。对于高度和宽度,分别是 getHeight 的 1/3 和 getWidth() 的 1/3。我有一个“捕获”x 和 y 坐标的鼠标事件。当我使用“矩形”时。包含(鼠标的 x 和 y)
*编辑:当我检查鼠标单击的位置时,它总是返回为 0,0 *
这是我的一些代码:
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);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
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);
if(w.didWin && w.who.equals("x")){
STATE = WIN;
}
if(w.didWin && w.who.equals("o")){
STATE = LOSS;
}
g2d = (Graphics2D) g;
if(STATE == PLAYING) {
g2d.setColor(Color.black);
g2d.drawLine(getWidth() / 3, 0, getWidth() / 3, getHeight());
g2d.drawLine((getWidth() / 3) * 2, 0, (getWidth() / 3) * 2, getHeight());
g2d.drawLine(0, getHeight() / 3, getWidth(), getHeight() / 3);
g2d.drawLine(0, (getHeight() / 3) * 2, getWidth(),
(getHeight() / 3) * 2);
g2d.setColor(Color.blue);
if(board.board[0].equals("x")) {
g2d.drawLine(0, 0, getWidth()/3, getHeight()/3);
g2d.drawLine(getWidth()/3, 0, 0, getHeight()/3);
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
int x = getX();
int y = getY();
// check which "bounds" contains the mouse click?
for (int i = 0; i < bounds.length; i++) {
if (bounds[i].contains(e.getPoint())) {
System.out.println("Point " + x + " " + y + " contains mouse");
}
}
repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
}
我的一些理论是:
- 在paintComponent期间调用getHeight/Width不起作用,但是当我绘制线条时它完美地工作
- 我在创建矩形时将值放置在错误的位置,但是我仔细检查了 那。
- 我尝试过使用代码,但没有运气
谢谢你,
安德鲁
PS 这里是一个 sscce:
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 java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class mainGUI extends JFrame {
TicTacToePanel tic = new TicTacToePanel();
public mainGUI() {
super("Tic Tac Toe");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(900, 700);
setResizable(false);
add(tic);
setVisible(true);
}
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);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
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);
if(w.didWin && w.who.equals("x")){
STATE = WIN;
}
if(w.didWin && w.who.equals("o")){
STATE = LOSS;
}
g2d = (Graphics2D) g;
if(STATE == PLAYING) {
g2d.setColor(Color.black);
g2d.drawLine(getWidth() / 3, 0, getWidth() / 3, getHeight());
g2d.drawLine((getWidth() / 3) * 2, 0, (getWidth() / 3) * 2, getHeight());
g2d.drawLine(0, getHeight() / 3, getWidth(), getHeight() / 3);
g2d.drawLine(0, (getHeight() / 3) * 2, getWidth(),
(getHeight() / 3) * 2);
g2d.setColor(Color.blue);
if(board.board[0].equals("x")) {
g2d.drawLine(0, 0, getWidth()/3, getHeight()/3);
g2d.drawLine(getWidth()/3, 0, 0, getHeight()/3);
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
int x = getX();
int y = getY();
// check which "bounds" contains the mouse click?
for (int i = 0; i < bounds.length; i++) {
if (bounds[i].contains(e.getPoint())) {
board.changeState(i, "x");
System.out.println("Point " + x + " " + y + " contains mouse");
}
}
repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
}
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;
if(isWin().who.equals("x") && isWin().didWin){
ready=false;
}
while (ready) {
Random r = new Random(System.currentTimeMillis());
int which = r.nextInt(8);
if (board[which].equals("e")) {
board[which] = "o";
ready = false;
}
}
}
public Win isWin() {
// o
if (board[0].equals("x") && board[1].equals("x")
&& board[2].equals("x")) {
return new Win(true, "x");
}
if (board[3].equals("x") && board[4].equals("x")
&& board[5].equals("x")) {
return new Win(true, "x");
}
if (board[6].equals("x") && board[7].equals("x")
&& board[8].equals("x")) {
return new Win(true, "x");
}
// o
if (board[0].equals("o") && board[1].equals("o")
&& board[2].equals("o")) {
return new Win(true, "o");
}
if (board[3].equals("o") && board[4].equals("o")
&& board[5].equals("o")) {
return new Win(true, "o");
}
if (board[6].equals("o") && board[7].equals("o")
&& board[8].equals("o")) {
return new Win(true, "o");
}
// x
if (board[0].equals("x") && board[4].equals("x")
&& board[8].equals("x")) {
return new Win(true, "x");
}
if (board[2].equals("x") && board[4].equals("x")
&& board[6].equals("x")) {
return new Win(true, "x");
}
// o
if (board[0].equals("o") && board[4].equals("o")
&& board[8].equals("o")) {
return new Win(true, "o");
}
if (board[2].equals("o") && board[4].equals("o")
&& board[6].equals("o")) {
return new Win(true, "o");
}
// x
if (board[0].equals("x") && board[3].equals("x")
&& board[6].equals("x")) {
return new Win(true, "x");
}
if (board[1].equals("x") && board[4].equals("x")
&& board[7].equals("x")) {
return new Win(true, "x");
}
if (board[2].equals("x") && board[5].equals("x")
&& board[8].equals("x")) {
return new Win(true, "x");
}
// x
if (board[0].equals("o") && board[3].equals("o")
&& board[6].equals("o")) {
return new Win(true, "o");
}
if (board[1].equals("o") && board[4].equals("o")
&& board[7].equals("o")) {
return new Win(true, "o");
}
if (board[2].equals("o") && board[5].equals("o")
&& board[8].equals("o")) {
return new Win(true, "o");
}
return new Win(false, "e");
}
}
public class Win {
boolean didWin = false;
String who = "";
public Win(boolean i, String s) {
didWin = i;
who = s;
}
}
public static void main(String[] args) {
mainGUI app = new mainGUI();
}
}
I have been going in circles trying to figure out why this is not working. I have a rectanlge, it is positioned at 0,0. And is 1/3 of getHeight, and 1/3 of getWidth(), for height and width respectively. I have a mouse event that "captures" the x and y cordinates. When I use the "rectangle".contains(the x and y of the mouse)
*EDIT: When i check where the mouse clicks it always comes back as 0,0 *
Here is some of my 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);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
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);
if(w.didWin && w.who.equals("x")){
STATE = WIN;
}
if(w.didWin && w.who.equals("o")){
STATE = LOSS;
}
g2d = (Graphics2D) g;
if(STATE == PLAYING) {
g2d.setColor(Color.black);
g2d.drawLine(getWidth() / 3, 0, getWidth() / 3, getHeight());
g2d.drawLine((getWidth() / 3) * 2, 0, (getWidth() / 3) * 2, getHeight());
g2d.drawLine(0, getHeight() / 3, getWidth(), getHeight() / 3);
g2d.drawLine(0, (getHeight() / 3) * 2, getWidth(),
(getHeight() / 3) * 2);
g2d.setColor(Color.blue);
if(board.board[0].equals("x")) {
g2d.drawLine(0, 0, getWidth()/3, getHeight()/3);
g2d.drawLine(getWidth()/3, 0, 0, getHeight()/3);
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
int x = getX();
int y = getY();
// check which "bounds" contains the mouse click?
for (int i = 0; i < bounds.length; i++) {
if (bounds[i].contains(e.getPoint())) {
System.out.println("Point " + x + " " + y + " contains mouse");
}
}
repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
}
Some theories I have had are:
- Calling getHeight/Width during paintComponent doesn't work, but it works perfectly when I draw the lines
- I have places the values in the wrong places when creating the rectanlge, but i doubled checked that.
- I have tried playing around with the code, but no luck
Thank You,
Andrew
PS here is a sscce:
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 java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class mainGUI extends JFrame {
TicTacToePanel tic = new TicTacToePanel();
public mainGUI() {
super("Tic Tac Toe");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(900, 700);
setResizable(false);
add(tic);
setVisible(true);
}
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);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
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);
if(w.didWin && w.who.equals("x")){
STATE = WIN;
}
if(w.didWin && w.who.equals("o")){
STATE = LOSS;
}
g2d = (Graphics2D) g;
if(STATE == PLAYING) {
g2d.setColor(Color.black);
g2d.drawLine(getWidth() / 3, 0, getWidth() / 3, getHeight());
g2d.drawLine((getWidth() / 3) * 2, 0, (getWidth() / 3) * 2, getHeight());
g2d.drawLine(0, getHeight() / 3, getWidth(), getHeight() / 3);
g2d.drawLine(0, (getHeight() / 3) * 2, getWidth(),
(getHeight() / 3) * 2);
g2d.setColor(Color.blue);
if(board.board[0].equals("x")) {
g2d.drawLine(0, 0, getWidth()/3, getHeight()/3);
g2d.drawLine(getWidth()/3, 0, 0, getHeight()/3);
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
int x = getX();
int y = getY();
// check which "bounds" contains the mouse click?
for (int i = 0; i < bounds.length; i++) {
if (bounds[i].contains(e.getPoint())) {
board.changeState(i, "x");
System.out.println("Point " + x + " " + y + " contains mouse");
}
}
repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
}
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;
if(isWin().who.equals("x") && isWin().didWin){
ready=false;
}
while (ready) {
Random r = new Random(System.currentTimeMillis());
int which = r.nextInt(8);
if (board[which].equals("e")) {
board[which] = "o";
ready = false;
}
}
}
public Win isWin() {
// o
if (board[0].equals("x") && board[1].equals("x")
&& board[2].equals("x")) {
return new Win(true, "x");
}
if (board[3].equals("x") && board[4].equals("x")
&& board[5].equals("x")) {
return new Win(true, "x");
}
if (board[6].equals("x") && board[7].equals("x")
&& board[8].equals("x")) {
return new Win(true, "x");
}
// o
if (board[0].equals("o") && board[1].equals("o")
&& board[2].equals("o")) {
return new Win(true, "o");
}
if (board[3].equals("o") && board[4].equals("o")
&& board[5].equals("o")) {
return new Win(true, "o");
}
if (board[6].equals("o") && board[7].equals("o")
&& board[8].equals("o")) {
return new Win(true, "o");
}
// x
if (board[0].equals("x") && board[4].equals("x")
&& board[8].equals("x")) {
return new Win(true, "x");
}
if (board[2].equals("x") && board[4].equals("x")
&& board[6].equals("x")) {
return new Win(true, "x");
}
// o
if (board[0].equals("o") && board[4].equals("o")
&& board[8].equals("o")) {
return new Win(true, "o");
}
if (board[2].equals("o") && board[4].equals("o")
&& board[6].equals("o")) {
return new Win(true, "o");
}
// x
if (board[0].equals("x") && board[3].equals("x")
&& board[6].equals("x")) {
return new Win(true, "x");
}
if (board[1].equals("x") && board[4].equals("x")
&& board[7].equals("x")) {
return new Win(true, "x");
}
if (board[2].equals("x") && board[5].equals("x")
&& board[8].equals("x")) {
return new Win(true, "x");
}
// x
if (board[0].equals("o") && board[3].equals("o")
&& board[6].equals("o")) {
return new Win(true, "o");
}
if (board[1].equals("o") && board[4].equals("o")
&& board[7].equals("o")) {
return new Win(true, "o");
}
if (board[2].equals("o") && board[5].equals("o")
&& board[8].equals("o")) {
return new Win(true, "o");
}
return new Win(false, "e");
}
}
public class Win {
boolean didWin = false;
String who = "";
public Win(boolean i, String s) {
didWin = i;
who = s;
}
}
public static void main(String[] args) {
mainGUI app = new mainGUI();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
例如,只需将其添加到您的鼠标侦听器(并且鼠标侦听器中的大部分代码可以通过更智能地使用数组来压缩),这样您就可以看到当您单击一个正方形时会发生什么:
顺便说一句:我会捕获 mousePressed 事件而不是 mouseClicked 事件,因为后者可能会错过按下事件。
For instance, just add this to your mouse listener (and again most of the code in the mouse listener can be compressed with smarter use of arrays) so you can see what happens when you click in a square:
As an aside: I'd trap the mousePressed event not the mouseClicked event since the latter can miss presses.