Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6688883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:27:18+00:00 2026-05-26T05:27:18+00:00

Alright so I am making a simple Tic Tac Toe program. Right now I

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:27:18+00:00Added an answer on May 26, 2026 at 5:27 am

    getWidth() and getHeight() 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, here's my dillema. I am making a simple application with Qt Creator that
Alright I am making an Android app, and using the CAPTURE_PIC_REQUEST to take a
Alright so in the project I am working on I am making a hard
Alright, here goes. I'm making a very basic app, and I want the user
Alright, quick question. A server is running in Eastern Time. PHP program needs to
Alright, I'm taking an array, and making another array from it with the only
Alright, I'm in the process of making a C# application that uses an API
Possible Duplicate: Difference between void main and int main? Alright, so I'm using bloodshed
alright, so im making an irc bot, and im wondering a few ways that
Alright, I just got finished making a function for some code I'm making, which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.