Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java!. Could someone have a look at my codes please. My codes are not working the way I expected. This is the initial state, generation

Java!. Could someone have a look at my codes please. My codes are not working the way I expected.

This is the initial state, generation is 0

image text in transcribed

but after click start, it should become like this and generation is 1..

image text in transcribed

My current codes is only producing " the generation: .. " but the pattern stay the same after click start. I don't know how to make the pattern changed. Pls help

import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.*;

public class runMain extends JFrame implements ActionListener,Runnable // implementing runnable interface to use features of Thread class... { int time=0; // time variable to count time // Label the number of generation private JLabel generationLabel;

// Label the start/stop/reset buttons private JButton start; private JButton stop; private JButton restart; Thread t; //Thread class object String msg; // Variable to intialize time to generate label boolean stopflag; boolean flag=true; // Label the cells private MarkLabel[][] mark;

// Declare state of the game private boolean gameLife = false; /** * Construct game of life with user interface */ public runMain(int ROW, int COL){ // Set up window super("Life - Controller"); setLocation(25,25); setSize(450,450); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Creating the labels for cells on board game mark = new MarkLabel[ROW + 2][COL + 2]; for (int row = 0; row

t.stop(); // stopping thread flag=true; // Return the value return; } // If reset button is clicked, reset everything else if(act == restart){ gameLife = false;

// Reset every cell for( int row = 1; row

} catch(InterruptedException e) {

} } public void stop() // stop method of thread class { stopflag=true; t=null; } public void start() { t=new Thread(this); t.start(); stopflag=false; } /** * This is the main method which runs the program * @param args The command-line arguments */ public static void main(String[] args) { // 19*19 size gameboard new runMain(19,19); } }

/////////////////////////////////////////////////

class MarkLabel extends JLabel implements MouseListener { // Declare the color for gameboard and clicked cells static final Color[] color = {Color.GRAY, Color.RED}; // Declare state of the game and number of neighbor cells private int state, newState, nCell; // Set up the array private MarkLabel[] neighborCell = new MarkLabel[8]; MarkLabel() { // State of the game DEAD state = newState = 0; // Show up color setOpaque(true); setBackground(color[0]); // To select new ALIVE cells addMouseListener(this); } // Adding neighbor cell void nextAdd(MarkLabel n) { neighborCell[nCell++] = n; } // Check cells whether it should stay ALIVE or DEAD void checkState() { int Alive = 0; for(int i = 0; i 3) newState = 0; } // If DEAD cells with 3 LIVE neighbors will become ALIVE else { if(Alive == 3) newState = 1; } } // Update the state void updateState() { if(state != newState) { state = newState; setBackground(color[state]); } } // Call when the game is reseted void restart() { if(state == 1 || newState == 1) { state = newState = 0; setBackground(color[state]); } } /** * This method takes action from clicking mouse from the user */ // If mouse is click on a cell, it become ALIVE @Override public void mouseClicked(MouseEvent e) { state = newState = 1; setBackground(color[1]); } // If click on a ALIVE cell, hold and drag that cell, it will become DEAD @Override public void mouseReleased(MouseEvent e) { state = newState = 0; setBackground(color[0]); } // Do nothing for all of these below public void mousePressed(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } }

Stop Reset Start Generation:0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

what is a peer Group? Importance?

Answered: 1 week ago