MontyTheMack
MontyTheMack

Reputation: 481

Tic Tac Toe game in Java using MouseAdapter

I have a CLickableBox class that creates boxes for me and now I need to make it so that when clicked, either an X or an O will be displayed in place. Here is the ClickableBox class.

 import java.awt.event.MouseAdapter;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.Container;


public class ClickableBox extends MouseAdapter {

  private int x, y, width, height;
  private Color borderColor, backColor, oldColor;
  private boolean drawBorder, clicked;
  private Container parent;

  public ClickableBox(int x, int y, int width, int height, Color borderColor, 
      Color backColor, boolean drawBorder, Container parent) {

    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.borderColor = borderColor;
    this.backColor = backColor;
    this.drawBorder = drawBorder;
    this.parent = parent;



  }

  public void draw(Graphics g) {

    oldColor = g.getColor();
    g.setColor(backColor);
    g.fillRect(x, y, width, height);
    if(drawBorder) {
        g.setColor(borderColor);
        g.drawRect(x, y, width, height);
    }
    g.setColor(oldColor);
  }

  public void mouseReleased(MouseEvent e) {
    if(x < e.getX() && e.getX() < x + width &&
         y < e.getY() && e.getY() < y + height) {
        clicked = true;
        parent.repaint();
    }
  }

  public boolean isClicked() {
    return clicked;
  }


  public int getX() {
    return x;
  }

  public void setX(int x) {
    this.x = x;
  }

  public int getY() {
    return y;
  }

  public void setY(int y) {
    this.y = y;
  }

  public int getWidth() {
    return width;
  }

  public void setWidth(int width) {
    this.width = width;
  }

  public int getHeight() {
    return height;
  }

  public void setHeight(int height) {
    this.height = height;
  }

  public Color getBorderColor() {
    return borderColor;
  }

  public void setBorderColor(Color borderColor) {
    this.borderColor = borderColor;
  }

  public Color getBackColor() {
    return backColor;
  }

  public void setBackColor(Color backColor) {
    this.backColor = backColor;
  }

  public Color getOldColor() {
    return oldColor;
  }

  public void setOldColor(Color oldColor) {
    this.oldColor = oldColor;
  }

  public boolean isDrawBorder() {
    return drawBorder;
  }

  public void setDrawBorder(boolean drawBorder) {
    this.drawBorder = drawBorder;
  }



  public void setClicked(boolean clicked) {
    this.clicked = clicked;
}
}

The TicTacToeBox class should extend ClickableBox, so that each box will be a listener. It needs to be designed so that each Box object will take care of itself- it knows if it's been clicked or not, and if so, whether it's going to be showing an x or an o.

The TicTacToeBox class is what I am having trouble with. This is what I will need for my game board. Any suggestions on how to implement this, simply? Below is my TicTacToeBox so far (not much):

Some direction and/or assistance would be greatly appreciated! Thanks.

import java.awt.Color;
import java.awt.Container;


public class TicTacToeBox extends ClickableBox {

  public TicTacToeBox(int x, int y, int width, int height, Color borderColor,
      Color backColor, boolean drawBorder, boolean mask, Container parent)
  {
    super(x, y, width, height, borderColor, backColor, drawBorder, parent);

  }
}

Upvotes: 0

Views: 518

Answers (1)

Rob I
Rob I

Reputation: 5737

Perhaps you need to override mouseReleased() - something like this:

public void mouseReleased(MouseEvent e) {
  if ( this.value == NONE ) {
    if ( currentTurn == Turn.X ) {
      this.value = X;
    }
    else {
      this.value = O;
    }
  }
  super.mouseReleased();
}

With some global currentTurn variable to keep track of whose turn it is, and a value field to represent what the current value of this box is. You'd also probably want to override draw() to make it render the "X" or "O"...

Upvotes: 1

Related Questions