Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.Color;
- class GameOfLife {
- public static void main(String[] args) throws FileNotFoundException {
- String fileToLoad = "";
- int width = 0, height = 0, squareSize = 10;
- // 8, 3, Test.txt
- Scanner keyboard = new Scanner(System.in);
- System.out.println("What file would you like to load into the program?");
- fileToLoad = keyboard.next();
- System.out.println("What is the width of the grid in this file?");
- width = keyboard.nextInt();
- System.out.println("What is the height of the grid in this file?");
- height = keyboard.nextInt();
- Grid life = new Grid(width, height,fileToLoad);
- LifeFrame frame = new LifeFrame();
- LifeGlob glob = new LifeGlob(life, squareSize);
- JButton button = new JButton("Next Generation");
- button.addActionListener(new GenerationListener(life, frame, width, height));
- frame.add(glob, BorderLayout.CENTER);
- frame.add(button, BorderLayout.SOUTH);
- frame.pack();
- }
- }
- class GenerationListener implements ActionListener {
- Grid c;
- LifeFrame f;
- int width = 0, height = 0;
- GenerationListener(Grid c, LifeFrame f, int width, int height) {
- this.c = c;
- this.f = f;
- this.width = width;
- this.height = height;
- }
- public void actionPerformed(ActionEvent e) {
- int[][] newGen = new int[width][height];
- int input = 0;
- for (int i = 0; i < height; i ++) {
- for (int j = 0; j < width; j ++) {
- if (c.neighbours(j, i) == 2) {
- newGen[j][i] = 1;
- } else if (c.neighbours(j, i) <= 1) {
- newGen[j][i] = 0;
- } else if (c.neighbours(j, i) >= 3) {
- newGen[j][i] = 0;
- }
- }
- }
- for (int k = 0; k < height; k ++) {
- for (int l = 0; l < width; l ++) {
- input = newGen[l][k];
- c.setCell(l, k, input);
- }
- }
- f.repaint();
- }
- }
- class LifeGlob extends JComponent {
- private Grid c;
- private int squareSize;
- private Color map[] = {Color.BLUE, Color.GREEN};
- LifeGlob(Grid c, int squareSize) {
- this.c = c; this.squareSize = squareSize;
- setPreferredSize(new Dimension(c.getX() * squareSize, c.getY() * squareSize));
- }
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- for (int y = 0; y < c.getY(); y++) {
- for (int x = 0; x < c.getX(); x++) {
- if (c.getCell(x, y) == 0) {
- g.setColor(map[0]);
- g.fillRect(x * squareSize,y * squareSize, squareSize, squareSize);
- } else {
- g.setColor(map[1]);
- g.fillRect(x * squareSize,y * squareSize, squareSize, squareSize);
- }
- }
- }
- }
- }
- class LifeFrame extends JFrame {
- LifeFrame() {
- setTitle("Game of Life");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- }
- }
- class Grid {
- int[][] grid;
- Grid(int x, int y, String inputFileName) throws FileNotFoundException {
- grid = new int[x][y];
- File f = new File(inputFileName);
- Scanner sc = new Scanner(f);
- for (int j = 0; j < y; j++) {
- if (sc.hasNextLine()) {
- String line = sc.nextLine();
- for (int i = 0; i < x; i++) {
- if (line.charAt(i) == '*') {
- grid[i][j] = 1;
- } else {
- grid[i][j] = 0;
- }
- }
- }
- }
- }
- public int getX() {return grid[0].length; }
- public int getY() {return grid.length; }
- public int getCell(int x, int y) {return grid[x][y];}
- public void setCell(int x, int y, int input) {grid[x][y] = input; }
- public int neighbours(int x, int y) {
- int populated = 0;
- try { if (grid[x - 1][y + 1] == 1) { populated ++; } } catch(ArrayIndexOutOfBoundsException e) {}
- try { if (grid[x][y + 1] == 1) { populated ++; } }catch(ArrayIndexOutOfBoundsException e) {}
- try { if (grid[x + 1][y + 1] == 1) { populated ++; } } catch(ArrayIndexOutOfBoundsException e) {}
- try { if (grid[x - 1][y] == 1) { populated ++; } } catch(ArrayIndexOutOfBoundsException e) {}
- try { if (grid[x + 1][y] == 1) { populated ++; } } catch(ArrayIndexOutOfBoundsException e) {}
- try { if (grid[x - 1][y - 1] == 1) { populated ++; } } catch(ArrayIndexOutOfBoundsException e) {}
- try { if (grid[x][y - 1] == 1) { populated ++; } } catch(ArrayIndexOutOfBoundsException e) {}
- try { if (grid[x + 1][y - 1] == 1) { populated ++; } } catch(ArrayIndexOutOfBoundsException e) {}
- return populated;
- }
- }
Add Comment
Please, Sign In to add comment