0

I need to write a tic tac toe game that displays who's turn it is. It doesnt have to restart or have AI. Im not sure why everytime i click it repaints a different colors. I want every cell to be one solid color then depending on if its X's or O's turn change to a different color containing an X or O.

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.util.Random;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.*;

public class TicTacToe extends JPanel implements MouseListener
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Tic Tac Toe");
        frame.setSize(616, 643);
        TicTacToe panel = new TicTacToe();
        frame.setBackground(Color.black);
        frame.add(panel);
        frame.setVisible(true);
    }

    private ColorCell cell1 = new ColorCell(0, 0);
    private ColorCell cell2 = new ColorCell(203, 0);
    private ColorCell cell3 = new ColorCell(406, 0);
    private ColorCell cell4 = new ColorCell(0, 203);
    private ColorCell cell5 = new ColorCell(0, 406);
    private ColorCell cell6 = new ColorCell(203, 203);
    private ColorCell cell7 = new ColorCell(203, 406);
    private ColorCell cell8 = new ColorCell(406, 203);
    private ColorCell cell9 = new ColorCell(406, 406);
    private int x = 0;
    private int y = 0;
    private int turn = 0;
    private int width = 0;
    private int height = 0;
    private int clickednum = 0;


  public TicTacToe()
    {
        addMouseListener(cell1);
        addMouseListener(cell2);
        addMouseListener(cell3);
        addMouseListener(cell4);
        addMouseListener(cell5);
        addMouseListener(cell6);
        addMouseListener(cell7);
        addMouseListener(cell8);
        addMouseListener(cell9);
        addMouseListener(this);
    }

  public TicTacToe(int xValue, int yValue)
  {
    x = xValue;
    y = yValue;
  }

  public void paintCell(Graphics g)
  {
      


  }

  public void paintComponent(Graphics g)
    {
        cell1.paintCell(g);
        cell2.paintCell(g);
        cell3.paintCell(g);
        cell4.paintCell(g);
        cell5.paintCell(g);
        cell6.paintCell(g);
        cell7.paintCell(g);
        cell8.paintCell(g);
        cell9.paintCell(g);
    }

    private void placePiece()
        {

        }

        public void mousePressed(MouseEvent e)
        {
            if(e.getX() >= x && e.getX() <= x+width && e.getY() >= y && e.getY() <= y+height)
            {
                placePiece();
            }
        }


    public void mouseReleased(MouseEvent e)
    {
        repaint();
    }
    public void mouseClicked(MouseEvent e)
    {
    }
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
  }
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by NormR1
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.