I need some help please :) Our assignment involves writing the Game of Life, whi
ID: 3919167 • Letter: I
Question
I need some help please :)
Our assignment involves writing the Game of Life, which I did. I am stuck with an infinite while loop and it has to do with threads and potentially inner classes which we didn't spend much time on in class. Please look at my code and help me implement my actionListener for startAnimation so that when I click the startAnimation button, the setText changes to "Stop Animation" and my game of life cycles at a specified generation per minute, defaul is 120, until Stop Animation is cliked and the game stops running. I hope that made sense. I also need help implementing a mouse listener so that I can add live cells to the board and run the game given specified live cells.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JComponent;
import javax.swing.JTextField;
/**
*
* This is the Game Of Life with a Graphical Interface
*/
public class GameOfLifeBoard extends JComponent implements ActionListener {
// field variables for color, buttons, life obj, and counter
private java.awt.Color lc;
private JButton startAnimation;
private JButton startingPoint;
private JButton clearBoard;
private JButton UP;
private JButton DOWN;
private JLabel Label;
private JTextField counterText;
private String counterStr;
private GameOfLife life;
private boolean runGame = false;
// constructor
public GameOfLifeBoard() {
// preferred window dimensions
setPreferredSize(new Dimension(500, 500));
lc = java.awt.Color.BLACK;
// create text field 3 columns wide
counterText = new JTextField(3);
// create instance of MyGameOfLife
life = new MyGameOfLife();
}
public JPanel createPanel1() {
JPanel buttons = new JPanel();
// create button with action listener
startAnimation = new JButton("Start Animation");
startAnimation.addActionListener(this);
// add button to Panel
buttons.add(startAnimation);
// create font and add to label
Font bBold = new Font("Helvetica", Font.PLAIN, 18);
Label = new JLabel("Generations per minute:");
Label.setFont(bBold);
// set font and default counter to 120
counterText.setEditable(true);
counterText.setFont(bBold);
counterText.setText("120");
// add Label and counter to Panel
buttons.add(Label);
buttons.add(counterText);
// create the up button with action listener
UP = new JButton("+");
UP.addActionListener(this);
// add button to Panel
buttons.add(UP);
// create down button with action listener
DOWN = new JButton("-");
DOWN.addActionListener(this);
// add button to Panel
buttons.add(DOWN);
return buttons;
}
public JPanel createPanel2() {
JPanel buttons2 = new JPanel();
startingPoint = new JButton("Starting Point");
startingPoint.addActionListener(this);
buttons2.add(startingPoint);
clearBoard = new JButton("Clear Board");
clearBoard.addActionListener(this);
buttons2.add(clearBoard);
return buttons2;
}
public void gol() {
while (runGame)
{
life.nextGeneration();
super.repaint();
try{
Thread.sleep(100);
} catch (InterruptedException a) {
// nothing to do if the sleep was interrupted
}
break;
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startingPoint) {
TestGameOfLife.setBoard(life);
super.repaint();
} else if (e.getSource() == UP) {
counterText.setText(String.valueOf(Integer.parseInt(counterText.getText()) + 20));
} else if (e.getSource() == DOWN) {
counterText.setText(String.valueOf(Integer.parseInt(counterText.getText()) - 20));
} else if (e.getSource() == clearBoard) {
for(int a = 1; a < 19; a++)
{
for(int b = 1; b < 19; b++)
{
life.setCellState(a, b, 0);
super.repaint();
}
}
} else if (e.getSource() == startAnimation) {
if(runGame) {
runGame = false;
startAnimation.setText("Start Animation");
gol();
} else if (!runGame) {
runGame = true;
startAnimation.setText("Stop Animation");
gol();
}
}
}
/**
* The necessary method. This method renders the component.
*
* @param g The Graphics object use to render
*/
@Override
public void paintComponent(Graphics g)
{
// paint the underlying component
super.paintComponent(g);
g.setColor(lc);
// draw the board
for(int i = 0; i < 475; i += 25)
{
for(int j = 10; j < 475; j += 25)
{
g.drawLine(0 + j, 0 + i, 25 + j, 0 + i);
g.drawLine(25 + j, 0 + i, 25 + j, 25 + i);
g.drawLine(0 + j, 0 + i, 0 + j, 25 + i);
g.drawLine(0 + j, 25 + i, 25 + j, 25 + i);
}
}
// draw the filled circle
for(int a = 1; a < 19; a++)
{
for(int b = 1; b < 19; b++)
{
int heightBuffer = 5;
int widthBuffer = 15;
// check state of cell
if(life.getCellState(a, b) == 1)
{
int ovalYLocation = a * 25 + heightBuffer;
int ovalXLocation = b * 25 + widthBuffer;
g.setColor(lc);
g.drawOval(ovalXLocation, ovalYLocation, 15, 15);
g.fillOval(ovalXLocation, ovalYLocation, 15, 15);
}
}
}
}
/**
* This is the Main Application driver
* for the Game of Life
*
* @param args: Command-line arguments
*
*/
public static void main (String args[]) {
// create an instnace of GameOfLifeBoard
GameOfLifeBoard board = new GameOfLifeBoard();
// create an instance of JFrame window
JFrame window = new JFrame();
// add title
window.setTitle("Game Of Life");
// window size
window.setSize(new Dimension(510, 600));
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set window to be visible
window.setVisible(true);
// create an instance of JPanel for the toolbar
JPanel game = new JPanel();
// add JPanel with obj ref to JFrame
window.add(game);
// add instance of board into Panel
game.add(board);
// add content pane to window
Container contentPane = window.getContentPane();
contentPane.setLayout(new BorderLayout());
// create Panels for buttons
Container contentPane2 = new JPanel();
Container contentPane3 = new JPanel();
// add panels to content pane
contentPane2.add(board.createPanel1());
contentPane3.add(board.createPanel2());
// add panel with our obj ref
contentPane.add(game, BorderLayout.NORTH);
// add buttons to main Pane
contentPane.add(contentPane2);
// add buttons to main Pane
contentPane.add(contentPane3, BorderLayout.SOUTH);
window.validate();
window.pack();
}
}
Explanation / Answer
--------------------
Please check the comments above in code.. i have changed runGame variable to volatile and introduced a thread. Let me know through comments, if you need some help.