To code pretty much makes a grid of a check board. However I want to modify it so if I click on any cell of the grid of the checkerboard it will display a red circle.
If I click on a cell that already has a circle, it will remove it
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class CheckerBoard extends JFrame implements MouseListener {
JLabel[][] label;
CheckerBoard() {
super("CheckerBoard");
JPanel panel = new JPanel(new GridLayout(8,8));
label = new JLabel[8][8];
for(int j = 0; j < 8; j++) {
int k = j;
for(int i = 0; i < 8; i++) {
label[i][j] = new JLabel();
label[i][j].setOpaque(true);
k++;
if(k % 2 == 0)
label[i][j].setBackground(Color.BLACK);
else
label[i][j].setBackground(Color.WHITE);
label[i][j].addMouseListener(this);
panel.add(label[i][j]);
}
}
add(panel, BorderLayout.CENTER);
}
public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
public static void main(String[] arg) {
CheckerBoard cb = new CheckerBoard();
cb.setSize(475, 475);
cb.setVisible(true);
}
}