Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.73 KB | None | 0 0
  1. import java.awt.Canvas;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Point;
  5. import java.util.ArrayList;
  6.  
  7. public class Simulation extends Canvas {
  8.  
  9.     private static boolean[][] world;
  10.     private static ArrayList<Point> toDie;
  11.     private static ArrayList<Point> toBeBorn;
  12.     private static boolean needToUpdate = false;
  13.     private Color gridColor, cellColor, backgroundColor;
  14.     private int cellSize;
  15.  
  16.     public Simulation(int size, Color backgroundColor, Color gridColor, Color cellColor,int cellSize) {
  17.         world = new boolean[size][size];
  18.         toDie = new ArrayList<Point>();
  19.         toBeBorn = new ArrayList<Point>();
  20.         setBackground(backgroundColor);
  21.         this.backgroundColor = backgroundColor;
  22.         this.gridColor = gridColor;
  23.         this.cellSize = cellSize;
  24.         this.cellColor = cellColor;
  25.     }
  26.  
  27.     public boolean[][] getWorld(){
  28.         return world;
  29.     }
  30.    
  31.     public void randomSeed(){
  32.         for(int x = 0; x < world.length; x++)
  33.             for(int y = 0; y < world[x].length; y++)
  34.                 world[x][y] = Math.random() >= .5;
  35.     }
  36.    
  37.     public void clear(){
  38.         for(int x = 0; x < world.length; x++)
  39.             for(int y = 0; y < world[x].length; y++)
  40.                 world[x][y] = false;
  41.     }
  42.    
  43.     public void step() {
  44.         for (int x = 0; x < world.length; x++) {
  45.             for (int y = 0; y < world[x].length; y++) {
  46.                 int neighborCount = 0;
  47.                 for (int ax = -1; ax < 2; ax++) {
  48.                     for (int ay = -1; ay < 2; ay++) {
  49.                         if (ax == 0 && ay == 0)
  50.                             continue;
  51.                         int adaX = x + ax, adaY = y + ay;
  52.                         if(adaX >= world.length)
  53.                             adaX = 0;
  54.                         if(adaX < 0)
  55.                             adaX = world.length - 1;
  56.                         if(adaY >= world[adaX].length)
  57.                             adaY = 0;
  58.                         if(adaY < 0)
  59.                             adaY = world[adaX].length - 1;
  60.                         if (world[adaX][adaY])
  61.                             neighborCount++;
  62.                     }
  63.                 }
  64.                 if (neighborCount == 3) {
  65.                     toBeBorn.add(new Point(x, y));
  66.                 } else if (neighborCount <= 1 || neighborCount >= 4) {
  67.                     toDie.add(new Point(x, y));
  68.                 }
  69.             }
  70.         }
  71.         for (Point born : toBeBorn){
  72.             try{
  73.                 world[born.x][born.y] = true;
  74.             }catch(IndexOutOfBoundsException e){}//occasionally world is resized during previous calculation
  75.         }
  76.         for (Point die : toDie){
  77.             try{
  78.                 world[die.x][die.y] = false;
  79.             }catch(IndexOutOfBoundsException e){}//occasionally world is resized during previous calculation
  80.         }
  81.         toBeBorn.clear();
  82.         toDie.clear();
  83.     }
  84.  
  85.     public void setCell(int x, int y) {
  86.         world[x][y] = !world[x][y];
  87.     }
  88.  
  89.     private void grid(Graphics g) {
  90.         g.setColor(gridColor);
  91.         for (int x = 1; x < world.length; x++)
  92.             g.drawLine(x * cellSize, 0, x * cellSize, world[x].length * cellSize);
  93.         for (int y = 1; y < world.length; y++)
  94.             g.drawLine(0, y * cellSize, world.length * cellSize, y * cellSize);
  95.     }
  96.    
  97.     private void cells(Graphics g) {
  98.         g.setColor(cellColor);
  99.         for (int x = 0; x < world.length; x++)
  100.             for (int y = 0; y < world[x].length; y++)
  101.                 if (world[x][y])
  102.                     g.fillRect(x * cellSize + 2, y * cellSize + 2, cellSize - 3, cellSize - 3);
  103.     }
  104.    
  105.     private void clearCells(Graphics g){
  106.         g.setColor(backgroundColor);
  107.         for (int x = 0; x < world.length; x++)
  108.             for (int y = 0; y < world[x].length; y++)
  109.                 g.fillRect(x * cellSize + 2, y * cellSize + 2, cellSize - 3, cellSize - 3);
  110.     }
  111.  
  112.     public void resize(int size){
  113.         boolean[][] temp = world;
  114.         world = new boolean[size][size];
  115.         for(int x = 0; x < temp.length; x++){
  116.             for(int y = 0; y < temp[x].length; y++){
  117.                 try{
  118.                     world[x][y] = temp[x][y];
  119.                 }catch(IndexOutOfBoundsException e){}
  120.             }
  121.         }
  122.         //System.arraycopy(temp, 0, world, 0, temp.length < world.length ? temp.length : world.length);
  123.         needToUpdate = true;
  124.     }
  125.  
  126.     @Override
  127.     public void paint(Graphics g) {
  128.         grid(g);
  129.         clearCells(g);
  130.         cells(g);
  131.     }
  132.    
  133.     @Override
  134.     public void update(Graphics g){
  135.         if(needToUpdate){
  136.             grid(g);
  137.             needToUpdate = false;
  138.         }
  139.         clearCells(g);
  140.         cells(g);
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement