First of all - I've started doing
Java 4 days ago, so the code is most likely riddled with "bugs" and inefficient approaches. I'm not as much looking for a way to optimize it, as to understand why is it inefficient, but I welcome any advice.
I decided to jump straight into making Conway's Game of Life. I made it to work. Kinda. The issue is that at higher speeds/lesser intervals between steps cells can't keep up with changing colors.
The basic design explanation:
100x100 GridLayout filled with two-dimensional Array of JButtons (5x5 pixels each), created through double if loop, each button is colored black, receives a MouseListener, name with its position in the Array (to help with locating the cell later on), then it's added to the GridLayout and the actual Array.
The algorythm for checking whether cell/button is alive or dead is also fairly simple - block of ifs checking and counting all the neighbours, cell's own color and returning either true or false, depending on the conditions.
Then there's a button to start the whole program. It does two things: sets a boolean "isPlaying" to true and initiates a
thread creation.
Next is "STOP" button, which kills the thread and sets "isPlaying" to false. It works, but I'm pretty sure it's a really bad design.
When the thread is running, it runs a while loop, checks all the cells with the algorythm, repaints the grid based on that and waits a specified amount of time.
Both above paragraphs I've tried to do through "resume"/"sleep"/"suspend" threads and create/"wait"/"stop", neither method had any advantages or noticeable differences (at least noticeable from my point of view).
As for the execution of redrawing the grid... I have 2 approaches:
- make the first Array of buttons and display it, make a second Array as a placeholder for next step, run each button of the first Array through the algorythm, based on true/false change background of the button on specified position in the second Array, after every button is checked - compare colors of corresponding buttons in both Arrays and recolor the first Array based on data from the second one.
- make the first Array of buttons and display it, make 2 more Arrays of booleans to hold whether current button in first Array is alive or dead and whether it should be alive or dead after the next step, compare the second boolean Array to the Array filled with buttons and their color, and change the background colors as necessary.
I've put "CurrentTimeMillis()" in every place of the redrawing process and it's going smooth every time (9 milliseconds delay at most between changing background colors). For some reason when the whole grid gets filled (or "near to filled", what matters is that the dying/living process spread across the whole grid) and the delay is at 1000/500 ms, I can clearly see the grid being recolored in chunks (in horizontal chunks, even though both the algorythm and redrawing process runs through columns first). If the delay is lowered to 200/100/50 ms, the grid can't keep up with displaying information and I get really delayed, flashing cells (at least that's what it looks like) and the whole program becomes clogged, not responding until the grid catches up to where it should be (which takes only a couple of seconds at 50 ms delay, but it's still annoying).
Does anyone have any ideas as to what might be a cause of this? I decided to explain everything to the best of my ability, cause the code is an actual mess right now due to me trying to find a fix. I will add it if my explanation turns out to be not enough and when I manage to clean it up.