Guest User

Main.java

a guest
Jan 2nd, 2015
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. package com.jnumerical.gameoflife;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.image.BufferStrategy;
  7. import javax.swing.JFrame;
  8.  
  9. public class Main extends Canvas implements Runnable {    
  10.     static String title = "Game of Life";
  11.     private boolean running = false;
  12.     private Thread thread;
  13.     private JFrame frame;
  14.     private Board board;
  15.    
  16.     static int width = 1900; // pixels width of board
  17.     static int height = 1000; // pixels height of board
  18.     private int cellPixels = 8; // pixels across a cell plus the gap
  19.     private int gridPixels = 2; // pixels between each cell
  20.     private int tick = 500; // milliseconds between ticks
  21.     private int chance = 13; // percent chance each cell will start alive
  22.     private int boardLife = 50; // number of ticks until board randomization
  23.     private int colorLife = 10; // number of ticks until new color chosen
  24.    
  25.     public Main(){
  26.         setPreferredSize(new Dimension(width, height));
  27.         frame = new JFrame();
  28.         board = new Board(chance, cellPixels, gridPixels, boardLife, colorLife);
  29.     }
  30.     public synchronized void start(){
  31.         running = true;
  32.         thread = new Thread(this, "Thread");
  33.         thread.start();
  34.     }
  35.     public synchronized void stop(){
  36.         running = false;
  37.         try{
  38.             thread.join();
  39.         }catch(InterruptedException e){
  40.             e.printStackTrace();
  41.         }
  42.     }
  43.     public void run(){
  44.         requestFocus();
  45.         long lastTime = 0;
  46.         while(running){
  47.             long time = System.currentTimeMillis();
  48.             if(time - lastTime >= tick){
  49.                 lastTime = time;
  50.                 update();
  51.                 render();
  52.             }
  53.         }
  54.         stop();
  55.     }
  56.     public void update(){
  57.         board.update();
  58.     }
  59.     public void render(){
  60.         BufferStrategy bs = getBufferStrategy();
  61.         if(bs == null){
  62.             createBufferStrategy(3);
  63.             return;
  64.         }
  65.         Graphics g = bs.getDrawGraphics();
  66.         board.render(g);
  67.         g.dispose();
  68.         bs.show();
  69.     }
  70.     public static void main(String[] args){
  71.         Main main = new Main();
  72.         main.frame.setResizable(false);
  73.         main.frame.setTitle(title);
  74.         main.frame.add(main);
  75.         main.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  76.         main.frame.setLocation(7, 0);
  77.         main.frame.setVisible(true);
  78.         main.frame.requestFocus();
  79.         main.frame.pack();
  80.         main.start();
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment