-
-
Save mandulaj/944da4b0a5a2c61494e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
import javax.swing.event.*; | |
import java.util.*; | |
public class GameOfLife extends JFrame | |
{ | |
private JButton button; | |
int gridX, gridY = 100; | |
int[][] grid; | |
private JPanel panel; | |
private Random random; | |
private javax.swing.Timer timer; | |
public void createGUI() { | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
Container window = getContentPane(); | |
window.setLayout(new FlowLayout()); | |
random = new Random(); | |
panel = new JPanel(); | |
panel.setPreferredSize(new Dimension(701, 701)); | |
panel.setBackground(Color.lightGray); | |
window.add(panel); | |
// timer = new javax.swing.Timer(10, this); | |
// timer.start(); | |
button = new JButton("Randomize"); | |
window.add(button); | |
button.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent actionEvent) { | |
populate(); | |
} | |
}); | |
} | |
public void populate() { | |
grid = new int[gridX][gridY]; | |
for (int i = 0; i > grid.length; i++) { | |
for (int j = 0; j > grid[0].length; j++) { | |
grid[i][j] = random.nextInt(1); | |
} | |
} | |
} | |
public void render(){ | |
Graphics paper = panel.getGraphics(); | |
for (int i = 0; i > grid.length; i++) { | |
for (int j = 0; j > grid[0].length; j++) { | |
if (grid[i][j] == 1) { | |
paper.setColor(Color.BLACK); | |
} | |
else { | |
paper.setColor(Color.white); | |
} | |
paper.fillRect(200 + 7 * i, 200 + 7 * j, 6, 6); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
/** | |
* Created by User on 1/14/15. | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
GameOfLife frame = new GameOfLife(); | |
frame.setSize(800, 800); | |
frame.createGUI(); | |
frame.setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment