Jump to content
  • Advertisement
Sign in to follow this  
CryoGenesis

Help With cellular Automata (JAVA)

This topic is 2519 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm having a problem with a cellular automata that I'm writing in java. Every time I run the program the computer Restarts >:(
does anyone know what the problem is? (by the way this is my first cellular automata)

CODE:


import java.awt.Graphics2D;

public class Cell {
int x;
int y;

public boolean active;

int r;
int g;
int b;


public void logic(Cell top, Cell bottom, Cell left, Cell right){

if(top != null){
if(top.active){
active = true;
}
}
if(bottom != null){
if(bottom.active){
active = true;
}
}
if(left != null){
if(left.active){
active = true;

}
}
if(right != null){
if(right.active){
active = true;
}
}


}
public void render(Graphics2D g){

g.fillRect(x, y, x+1, y+1);


}

}

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;

public class Main extends JPanel implements KeyListener, Runnable {

Thread loop;
RunningState state;

static int width = 1200;
static int height = 700;

double i;

public static void main(String[] args){

JFrame frame = new JFrame("Cellular Automata");
frame.setSize(width,height);
frame.setLocation(100,20);
frame.setUndecorated(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

frame.add(new Main());
frame.setBackground(Color.black);
frame.setVisible(true);


}

public Main(){

addKeyListener(this);
setFocusable(true);

state = new RunningState();
state.init();

loop = new Thread(this);
loop.start();

}


public void update(){
state.update();
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D) g;

g2d.setColor(Color.white);
g2d.fillRect(0, 0, 1200, 800);
g2d.setColor(Color.black);
g2d.fillRect(0, 0, 10, 800);
g2d.fillRect(0, 0, 1200, 10);
g2d.fillRect(1190, 0, 10, 800);
g2d.fillRect(0, 690, 1200, 10);

state.render(g2d);



}


public void run() {

boolean running = true;
while(running){



i = System.currentTimeMillis();


update();
repaint();


try {

loop.sleep(5000);


} catch (InterruptedException e) {
System.out.println("Loop Fail");
System.exit(0);
e.printStackTrace();
}

if(i + 5000 > System.currentTimeMillis()){
System.exit(0);
}

}


}
public void keyPressed(KeyEvent arg0) {


if(arg0.getKeyCode() == arg0.VK_ESCAPE){
System.exit(0);
}



}

public void keyReleased(KeyEvent arg0) {


}

public void keyTyped(KeyEvent arg0) {


}


}

import java.awt.*;

public class RunningState {
Cell[][] cell;
Cell[][] buffer;
boolean[][] a;

int width = Main.width;
int height = Main.height;

public void init(){



cell = new Cell[width][height];
buffer = new Cell[width][height];
a = new boolean[width][height];



a[10][10] = true;




for(int i = 0; i < width; i++){
for(int b = 0; b < height; b++){

cell = new Cell();
cell.x = i;
cell.y = b;

buffer = new Cell();
buffer.x = i;
buffer.y = b;

if(a != true && a != false){
a = false;
}
cell.active = a;

}
}


}



public void update(){


for(int i = 0; i < width; i++){
for(int b = 0; b < height; b++){
try{
buffer.logic(cell[b - 1],
cell[b + 1], cell[i - 1], cell[i + 1]);
}catch(Exception e){}
}
}




cell = buffer;
}

public void render(Graphics2D g){
for(int i = 0; i < width; i++){
for(int b = 0; b < height; b++){
if(cell.active){
cell.render(g);
}
}
}
}

}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!