I am doing an cellular automaton exercise in java and I came across a problem.
The logical part of the CA is working fine, if I wanted it to be arranged on the console it would be ready, but I want it to be drawn on the panel, when it is 1 drawing a black square, when it is 0 drawing a white square
Let's go to the problem
What is happening is that the whole logic runs normally, but when drawing java only draws the latest generation of CA, I read that java paintComponent only works when java decides to and not when it is called.
My question is: how the best way to solve this problem and how to do it.
I thought of some things, but as I don't know java so well I decided to ask you
I thought about maybe saving all Arrays into one and then drawing from it (but I don't know how I could do it) I thought maybe there was a method of drawing in each loop, but I didn't find it (I used repaint () but it was no use)
Anyway, I would be grateful if someone gave me a solution, I have this problem for days and I have not found a solution, below is all the code
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class CA extends JFrame {
Rules rules = new Rules();
public CA() {
super("Cellular Automaton");
setLayout(new BorderLayout());
add(new GUI_CA(), BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new CA();
}
});
}
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class GUI_CA extends JPanel {
int cellSize = 10;
Rules rules = new Rules();
private BufferedImage img;
private Graphics2D g2d;
public GUI_CA() {
img = new BufferedImage(cellSize, cellSize, BufferedImage.TYPE_INT_RGB);
g2d = (Graphics2D) img.createGraphics();
evolution();
}
public void evolution() {
rules.cells = new int[987/cellSize];
rules.cells[rules.cells.length/2] = 1;
System.out.println(Arrays.toString(rules.cells));
for (int generationCount = 0; generationCount < 10; generationCount++) {
rules.generate();
System.out.println(Arrays.toString(rules.cells));
repaint();
}
}
public void drawCA(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
for (int i = 1; i < rules.cells.length -1; i++) {
if (rules.cells[i] == 1) {
g2d.setColor(Color.BLACK);
g2d.fillRect(i * cellSize, rules.generation * 5, cellSize, cellSize);
System.out.println(Arrays.toString(rules.cells));
}else {
g2d.setColor(Color.WHITE);
g2d.fillRect(i * cellSize, rules.generation * 5, cellSize, cellSize);
}
}
}
public void drawBackground(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
int width = 987;
int height = 545;
g2d.setPaint(Color.WHITE);
g2d.fillRect(0, 0, width, height);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawBackground(g);
drawCA(g);
g.drawImage(img, 0, 0, null);
}
public Dimension getPreferredSize() {
return new Dimension(987, 545);
}
}
public class Rules {
int[] cells;
int[] ruleSet = new int[] {0, 1, 1, 1, 1, 0, 0, 0};
//int cellSize = 10;
int generation = 0;
void generate() {
int[] nextGeneration = new int[cells.length];
for (int i = 1; i < cells.length - 1; i++) {
int left = cells[i - 1];
int me = cells[i];
int right = cells [i + 1];
nextGeneration[i] = rules(left, me, right);
}
cells = nextGeneration;
generation++;
}
int rules(int left, int me, int right) {
String s = "" + left + me + right;
int index = Integer.parseInt(s, 2);
//System.out.println(index);
return ruleSet[index];
}
}