0

i'm trying to make the Tic Tac Toe game using Java without worring about the artificial intelligence in the game. I'm just trying to draw it and do a few things that I'm going to explain. Here's the code of the tic tac toe that i want to draw:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class JFrameTicTacToeInternet extends JFrame implements MouseListener{

public JFrameTicTacToeInternet(String title){
    super(title);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(500,500);

    addMouseListener(this);
}

public void paint(Graphics g){
    int totalWidth = 200;
    int verticalCenter = (this.getHeight() - totalWidth)/2;
    int horizontalCenter = (this.getWidth() - totalWidth)/2;

    g.setColor(Color.white);
    g.fillRect(0,0,this.getWidth(), this.getHeight());

    g.setColor(Color.black);
    // draw big grid
    this.drawGrid(g, horizontalCenter, verticalCenter, totalWidth);
}

private void drawGrid(Graphics g, int x, int y, int width) {
    // draw box around grid
    g.drawRect(x, y, width, width);

    // draw vertical grid lines
    g.drawLine(width / 3 + x, y, width / 3 + x, y + width);
    g.drawLine(width / 3 * 2 + x, y, width / 3 * 2 + x, y + width);                          

    // draw horizontal grid lines                           
    g.drawLine(x, width / 3 + y, x + width, width / 3 + y);            
    g.drawLine(x, width / 3 * 2 + y, x + width, width / 3 * 2 + y);
}

The result is a tic tac toe. Now i want to add that when you enter with the mouse in a square it colors of blue. For example: i enter with the mouse the square that is bottom right and it has to color of blue. When i exit that square the color must return White.

I want to do it using mouseEntered and mouseExited, but i really don't know how to do it.

Thank you in advance.

HeyMan
  • 1
  • 1
  • Maybe something like [this](http://stackoverflow.com/questions/32468805/tic-tac-toe-java-mouselistener/32468908#32468908)? This example requires to click a cell, but the basic idea is the same – MadProgrammer Apr 03 '16 at 11:13
  • `mouseEnter` and `mouseExit` won't work, because you're using a single component to draw the grid and those events are called only on a component bases. – MadProgrammer Apr 03 '16 at 11:14
  • Take a look at [this](http://stackoverflow.com/questions/13734069/how-can-i-set-in-the-midst/13734319#13734319), [this](http://stackoverflow.com/questions/13313084/graphics-rendering-in-title-bar/13316131#13316131), [this](http://stackoverflow.com/questions/13457237/how-to-get-the-exact-middle-of-a-screen-even-when-re-sized/13460914#13460914) for some reasons why you SHOULDN'T override `paint` of a top level container – MadProgrammer Apr 03 '16 at 11:17
  • You might also like to have a look at [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) and [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) for how painting works in Swing – MadProgrammer Apr 03 '16 at 11:18

0 Answers0