Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Canvas;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Point;
- import java.util.ArrayList;
- public class Simulation extends Canvas {
- private static boolean[][] world;
- private static ArrayList<Point> toDie;
- private static ArrayList<Point> toBeBorn;
- private static boolean needToUpdate = false;
- private Color gridColor, cellColor, backgroundColor;
- private int cellSize;
- public Simulation(int size, Color backgroundColor, Color gridColor, Color cellColor,int cellSize) {
- world = new boolean[size][size];
- toDie = new ArrayList<Point>();
- toBeBorn = new ArrayList<Point>();
- setBackground(backgroundColor);
- this.backgroundColor = backgroundColor;
- this.gridColor = gridColor;
- this.cellSize = cellSize;
- this.cellColor = cellColor;
- }
- public boolean[][] getWorld(){
- return world;
- }
- public void randomSeed(){
- for(int x = 0; x < world.length; x++)
- for(int y = 0; y < world[x].length; y++)
- world[x][y] = Math.random() >= .5;
- }
- public void clear(){
- for(int x = 0; x < world.length; x++)
- for(int y = 0; y < world[x].length; y++)
- world[x][y] = false;
- }
- public void step() {
- for (int x = 0; x < world.length; x++) {
- for (int y = 0; y < world[x].length; y++) {
- int neighborCount = 0;
- for (int ax = -1; ax < 2; ax++) {
- for (int ay = -1; ay < 2; ay++) {
- if (ax == 0 && ay == 0)
- continue;
- int adaX = x + ax, adaY = y + ay;
- if(adaX >= world.length)
- adaX = 0;
- if(adaX < 0)
- adaX = world.length - 1;
- if(adaY >= world[adaX].length)
- adaY = 0;
- if(adaY < 0)
- adaY = world[adaX].length - 1;
- if (world[adaX][adaY])
- neighborCount++;
- }
- }
- if (neighborCount == 3) {
- toBeBorn.add(new Point(x, y));
- } else if (neighborCount <= 1 || neighborCount >= 4) {
- toDie.add(new Point(x, y));
- }
- }
- }
- for (Point born : toBeBorn){
- try{
- world[born.x][born.y] = true;
- }catch(IndexOutOfBoundsException e){}//occasionally world is resized during previous calculation
- }
- for (Point die : toDie){
- try{
- world[die.x][die.y] = false;
- }catch(IndexOutOfBoundsException e){}//occasionally world is resized during previous calculation
- }
- toBeBorn.clear();
- toDie.clear();
- }
- public void setCell(int x, int y) {
- world[x][y] = !world[x][y];
- }
- private void grid(Graphics g) {
- g.setColor(gridColor);
- for (int x = 1; x < world.length; x++)
- g.drawLine(x * cellSize, 0, x * cellSize, world[x].length * cellSize);
- for (int y = 1; y < world.length; y++)
- g.drawLine(0, y * cellSize, world.length * cellSize, y * cellSize);
- }
- private void cells(Graphics g) {
- g.setColor(cellColor);
- for (int x = 0; x < world.length; x++)
- for (int y = 0; y < world[x].length; y++)
- if (world[x][y])
- g.fillRect(x * cellSize + 2, y * cellSize + 2, cellSize - 3, cellSize - 3);
- }
- private void clearCells(Graphics g){
- g.setColor(backgroundColor);
- for (int x = 0; x < world.length; x++)
- for (int y = 0; y < world[x].length; y++)
- g.fillRect(x * cellSize + 2, y * cellSize + 2, cellSize - 3, cellSize - 3);
- }
- public void resize(int size){
- boolean[][] temp = world;
- world = new boolean[size][size];
- for(int x = 0; x < temp.length; x++){
- for(int y = 0; y < temp[x].length; y++){
- try{
- world[x][y] = temp[x][y];
- }catch(IndexOutOfBoundsException e){}
- }
- }
- //System.arraycopy(temp, 0, world, 0, temp.length < world.length ? temp.length : world.length);
- needToUpdate = true;
- }
- @Override
- public void paint(Graphics g) {
- grid(g);
- clearCells(g);
- cells(g);
- }
- @Override
- public void update(Graphics g){
- if(needToUpdate){
- grid(g);
- needToUpdate = false;
- }
- clearCells(g);
- cells(g);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement