So I'm trying to build conways game of life and everything works except when I update the background color on the label when it needs to be changed to black or white. When I click on the labels they change color and when I first make the labels it changes the color. When I run the game loop and it checks to see how many other cells it's touching and it tries to update the color of the label it doesn't work.
The method that updates the color seems to work because I put print statements in it and they printed it only works when I remove the keylistener stuff then it will update the color. Literaly everythings works except for the setBackground method. (I'll add a bunch of comments when the not working setBackground method is).
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Scanner;
import javax.swing.JLabel;
import java.util.concurrent.TimeUnit;
public class Main
{
public static final int arrY = 28;
public static final int arrX = 28;
public static int[][] arr = new int[arrX][arrY];
public static int[][] endArr = new int[arrX][arrY];
public static final int CELL = 1;
public static final int DEADCELL = 0;
public static boolean stop = true;
public static int generations = 0;
static MyFrame frame = new MyFrame();
public static int isTouching(int X, int Y) {
int integer = 0;
if (X != 0 && Y != 0) {
if (arr[X-1][Y-1] == CELL) {
integer++;
}
}
if (X != 0) {
if (arr[X-1][Y] == CELL) {
integer++;
}
}
if (X != 0) {
if (arr[X-1][Y+1] == CELL) {
integer++;
}
}
if (Y != 0) {
if (arr[X][Y-1] == CELL) {
integer++;
}
}
if (arr[X][Y+1] == CELL) {
integer++;
}
if (Y != 0) {
if (arr[X+1][Y-1] == CELL) {
integer++;
}
}
if (arr[X+1][Y] == CELL) {
integer++;
}
if (arr[X+1][Y+1] == CELL) {
integer++;
}
return integer;
}
public static void main(String[] args) throws InterruptedException {
Scanner scan = new Scanner(System.in);
for (int i = 1; i < arrX; i++) {
for (int j = 1; j < arrY; j++) {
arr[i][j] = 0;
endArr[i][j] = 0;
}
}
frame.makeBoard(arr);
}
public static void loop() {
while (!stop) {
if (stop) {
break;
}
generations++;
for (int i = 0; i <= arrX - 2; i++) {
for (int j = 0; j <= arrY - 2; j++) {
int touch = isTouching(i, j);
if (arr[i][j] == CELL && touch < 2) {
frame.labelWhite(i, j);
endArr[i][j] = DEADCELL;
}
if (arr[i][j] == CELL && touch == 3) {
frame.labelBlack(i,j);
endArr[i][j] = CELL;
}
if (arr[i][j] == CELL && touch > 3) {
frame.labelWhite(i,j);
endArr[i][j] = DEADCELL;
}
if (arr[i][j] == DEADCELL && touch == 3) {
frame.labelBlack(i,j);
endArr[i][j] = CELL;
}
if (arr[i][j] == CELL && touch == 2) {
frame.labelBlack(i,j);
endArr[i][j] = CELL;
}
}
}
for (int i = 0; i < arrX; i++) {
for (int j = 0; j < arrY; j++) {
arr[i][j] = endArr[i][j];
endArr[i][j] = 0;
}
}
}
}
public static void stop() {
if (stop == false) {
stop = true;
} else if (stop == true) {
stop = false;
loop();
}
}
}
import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MyFrame extends JFrame implements KeyListener{
public static JLabel[][] labelArr = new JLabel[28][28];
MyFrame(){
this.setTitle("Oliver's Game of Life");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(1000,1000);
this.setLayout(null);
this.addKeyListener(this);
this.setVisible(true);
}
public void makeBoard(int[][] arr) {
int addX = 0;
int addY = 0;
for (int i = 0; i < Main.arrX; i++) {
for (int j = 0; j < Main.arrY; j++) {
labelArr[i][j] = new JLabel();
// first two move second two make size
labelArr[i][j].setBounds(addY, addX, 25, 25);
labelArr[i][j].setOpaque(true);
this.add(labelArr[i][j]);
labelArr[i][j].setBackground(Color.black);
labelArr[i][j].setVisible(true);
int ii = i;
int jj = j;
labelArr[i][j].addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
labelArr[ii][jj].setBackground(Color.BLACK);
Main.arr[ii][jj] = Main.CELL;
Main.endArr[ii][jj] = Main.CELL;
}
});
addX += 25;
}
addX = 0;
addY += 25;
}
}
@Override
public void keyTyped(KeyEvent e) {
switch(e.getKeyChar()) {
case 'a': Main.stop();
break;
}
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
public void labelWhite(int i, int j) {
//this doesn't work
labelArr[i][j].setBackground(Color.white);
//this doesn't work
}
public void labelBlack(int i, int j) {
//this doesn't work
labelArr[i][j].setBackground(Color.black);
//this doesn't work
}
}