Я ходил кругами, пытаясь понять, почему это не работает. У меня есть прямоугольник, он расположен на 0,0. И составляет 1/3 от getHeight и 1/3 от getWidth () для высоты и ширины соответственно. У меня есть событие мыши, которое «захватывает» X и Y кординаты. Когда я использую «прямоугольник» .contains (х и у мыши)
* РЕДАКТИРОВАТЬ: Когда я проверяю, где щелкает мышь, он всегда возвращается как 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) {
}
}
Некоторые теории, которые у меня были:
- Вызов getHeight / Width во время paintComponent не работает, но он отлично работает, когда я рисую линии
- У меня есть значения в неправильных местах при создании прямоугольника, но я дважды проверил это.
- Я пытался поиграться с кодом, но не повезло
Спасибо,
Андрей
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();
}
}