Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 475553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:24:00+00:00 2026-05-13T00:24:00+00:00

Within this program, we need to create an 8×8 grid of LifeCell widgets. The

  • 0

Within this program, we need to create an 8×8 grid of “LifeCell” widgets. The instructor did not mention that the widgets had to be an object of Shape so I went ahead and used the GridLayout class. The GridLayout class works fine (as well as I know, since there is no visual aid to confirm.) The object of the program is to play the Game of Life where a user can click on one of the LifeCell widgets and toggle between states being ‘alive’ or ‘dead.

My question relies heavily on getting the cells to be painted. It could be a problem with my code, but I am not 100% sure.

Program2.java

public class Program2 extends JPanel implements ActionListener {
private LifeCell[][] board; // Board of life cells.
private JButton next; // Press for next generation.
private JFrame frame; // The program frame.

public Program2() {
    // The usual boilerplate constructor that pastes the main
    // panel into a frame and displays the frame. It should
    // invoke the "init" method before packing the frame
    frame = new JFrame("LIFECELL!");
    frame.setContentPane(this);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.init();
    frame.pack();
    frame.setVisible(true);
}
    public void init() {
    // Create the user interface on the main panel. Construct
    // the LifeCell widgets, add them to the panel, and store
    // them in the two-dimensional array "board". Create the
    // "next" button that will show the next generation.
    LifeCell[][] board = new LifeCell[8][8];
    this.setPreferredSize(new Dimension(600, 600));
    this.setBackground(Color.white);
    this.setLayout(new GridLayout(8, 8));
    // here is where I initialize the LifeCell widgets
    for (int u = 0; u < 8; u++) {
        for (int r = 0; r < 8; r++) {
            board[u][r] = new LifeCell(board, u, r);
            this.add(board[u][r]);
            this.setVisible(true);

        }
    }

LifeCell.java

 public class LifeCell extends JPanel implements MouseListener {
   private LifeCell[][] board; // A reference to the board array.
   private boolean alive;      // Stores the state of the cell.
   private int row, col;       // Position of the cell on the board.
   private int count;          // Stores number of living neighbors.

   public LifeCell(LifeCell[][] b, int r, int c) {
       // Initialize the life cell as dead.  Store the reference
       // to the board array and the board position passed as
       // arguments.  Initialize the neighbor count to zero.
       // Register the cell as listener to its own mouse events.
       this.board = b;
       this.row = r;
       this.col = c;
       this.alive = false;
       this.count = 0;
       addMouseListener(this);
   }   

and here is the paintComponent method:

   public void paintComponent(Graphics gr) {
       // Paint the cell.  The cell must be painted differently
       // when alive than when dead, so the user can clearly see
       // the state of the cell.
           Graphics2D g = (Graphics2D) gr;
           super.paintComponent(gr);
           g.setPaint(Color.BLUE);
   }

I do not need the exact solution to fix it, but I am at wits end trying to get it to work.

Thanks.

EDIT:

I added more segment of Program2.java class, I can check back tomorrow I am heading off to bed, I appreciate all the help guys.

EDIT #2:

My real confusion gets to when I populate my frame with an 8×8 GridLayout each individual “cell” for lack of better words is of type LifeCell. How can I paint each LifeCell different colors? If that makes any sense at all to you guys, I can try to revise it as much as I can. And camickr, I will look at that website, thank you.

Assignment can be found here to avoid any and all confusion regarding my question and/or the code snippet.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:24:00+00:00Added an answer on May 13, 2026 at 12:24 am

    alt text

    You’re in the right track.

    If you want to use an existing component ( such as a JPanel, JLabel, JButton etc ) it it much better that you honor what the component already does, and just parametrize what is needed.

    So in your case you’re using a JPanel, this ( and other JComponents ) have a background property that you can change. So, instead of trying to paint the component your self ( which is what is failing right now ) just set that value and let the paint paint itself.

    You can add a “getLifeColor” which return different colors depending on the cell state:

       private Color getLifeColor() {
           return this.alive?liveColor:deadColor;
       } 
    

    And then just have the cell painting the background with this color:

      public void paintComponent(Graphics gr) {
           setBackground( getLifeColor() );
           super.paintComponent( gr );
      }
    

    After that you just have to set the state of the cell to live or dead and the component will appear with the corresponding color:

    alt text

    Here’s the short self contained correct example ( SSCCE ) of the code you posted + the live/dead color usage. I think you can continue from there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From within my web application (ASP.NET/C#) I would need to create a button that,
This is with reference to the below question: Execute program from within a C
I have a simple WPF application that uses ClickOnce to handle installing. Within this
I need to read afile with matrix data and create matrix within my prrogram.
I need to create a winform in Visual Studio 2010 using C# that searches
Here is my problem, I need to create a javascript function that takes 2
I need to create a regular expression that allows a string to contain any
Okay, so I have this Silverlight client program. I'm not allowed to use the
I have a Java program and I need to create a Windows Service from
I have subclassed a UITableViewCell and within this class I want to get it's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.