使用getHeight()和getWidth()时,矩形不使用包含正确的边界

时间:2011-10-19 01:33:05

标签: java height width jpanel

我一直在试图弄清楚为什么这不起作用。我有一个rectanlge,它位于0,0。高度和宽度分别是getHeight的1/3和getWidth()的1/3。我有一个鼠标事件“捕获”x和y坐标。当我使用“矩形”.contains(鼠标的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不起作用,但在绘制线条时效果很好
  • 我在创建rectanlge时将值放在错误的位置,但我加倍检查了。
  • 我试过玩代码,但没有运气

谢谢,

安德鲁

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();
    }

}

1 个答案:

答案 0 :(得分:1)

例如,只需将其添加到鼠标侦听器中(并且可以通过更智能地使用数组来压缩鼠标侦听器中的大多数代码),这样您就可以看到单击正方形时会发生什么:

@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 " + i + " contains mouse");
     }
  }

顺便说一下:我会捕获mousePressed事件而不是mouseClicked事件,因为后者可能会错过按下。