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.