import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CA extends JFrame
{
static int[] cells = new int[60];
static int generation;
static int[] ruleSet = {0,0,0,0,0,0,0,1};
int width = 600;
int w = 15;
JFrame frame;
JPanel panel;
public CA()
{
generation = 0;
panel = new JPanel();
this.setSize(1000, 1000);
this.setVisible(true);
panel.setLayout(null);
this.add(panel);
repaint();
}
public static void generate()
{
int[] nextGen = new int[cells.length];
for(int i = 1; i < cells.length-1; i++)
{
int left = i-1;
int me = i;
int right = i+1;
nextGen[i] = rules(left,me,right);
}
for(int i = 0; i < nextGen.length; i++)
{
cells[i] = nextGen[i];
}
System.out.println(Arrays.toString(cells));
}
public static int rules(int a, int b, int c)
{
if(a == 1 && b ==1 && c == 1)
return ruleSet[0];
else if(a == 1 && b ==1 && c == 0)
return ruleSet[1];
else if(a == 1 && b ==0 && c == 1)
return ruleSet[2];
else if(a == 1 && b ==0 && c == 0)
return ruleSet[3];
else if(a == 0 && b ==1 && c == 1)
return ruleSet[4];
else if(a == 0 && b ==1 && c == 0)
return ruleSet[5];
else if(a == 0 && b ==0 && c == 1)
return ruleSet[6];
else
return ruleSet[7];
}
public static void main(String[] args)
{
for(int i = 0 ; i < cells.length; i++)
{
cells[i]=0;
}
int num = (int)cells.length / 2;
cells[num] = 1;
new CA();
}
public void paint(Graphics g)
{
super.paintComponents(g);
//g2d.drawRect(10, 10, 100, 100);
//generation = 0;
System.out.println("generation ......." + generation);
while(generation < 3)
{
int counter = 0;
System.out.println("cells...." + Arrays.toString(cells));
for( int i : cells)
{
if(i == 1)
{
System.out.println("i == 1");
g.fillRect((counter*w) + 300, generation + 300, w, w);
//counter++;
}
else {
System.out.println("not filling rect");
}
}
System.out.println("generation ...in while ...." + generation);
generate();
generation++;
}
g.drawString("this works", 100, 100);
}
}
Parts of my paint method work such as the drawString works perfectly fine but all the rest of the paint method does not work the way I want it to. I want to make a Cellular automaton that is similar to the Wolfram cellular automaton. I mostly copied my paint method from other projects that have a working paint method so I don't really know what the method itself is doing.
Your whole approach is broken, and instead I recommend that you follow these guidelines:
repaint()
which would signal the JPanel to redraw itself.Also:
super.paintComponents
within a paint method override. The super call should match the override call in this situation.