import java.awt.*;
import javax.swing.*;
import java.util.Timer;
public class GUI extends JPanel
{
private static final long serialVersionUID = 1L;
private static final int ROWS = 50;
private static final int COLS = 30;
public JButton[][] buttons = new JButton[ROWS][COLS];
public GUI()
{
setLayout(new GridLayout(ROWS, COLS, 1,1));
for (int row = 0; row < buttons.length; row++) {
for (int col = 0; col < buttons[row].length; col++) {
JButton button = new JButton("");
add(button);
buttons[row][col] = button;
}
}
buttons[0][0].setBackground(Color.BLACK);
}
public void start()
{
GUI gui = new GUI();
JFrame frame = new JFrame("The Game Of Life");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(gui);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public void run()
{
this.start();
this.setColor('g', 0, 0);
}
public void setColor(char c, int i, int j)
{
setLayout(new GridLayout(ROWS, COLS, 1,1));
for (int row = 0; row < buttons.length; row++) {
for (int col = 0; col < buttons[row].length; col++) {
JButton button = new JButton("");
add(button);
buttons[row][col] = button;
switch(c){
case 'g':
buttons[i][j].setBackground(Color.GREEN);
case 'r':
buttons[i][j].setBackground(Color.RED);
case 'b':
buttons[i][j].setBackground(Color.BLACK);
}
}
}
}
}
I can get the "buttons[0][0]" to change color but nowhere else has seemed to work (the button[0][0] is there as an example that its the only place where it will change the button color) the class GUI is part of a system that is supposed to create the biology "game of life" this is the code for the Gui interface I need the code to create a grid of 50x30 buttons and I will end up making each button change color according to the attributes of the other buttons around it. The biggest problem at this point is I cant get the buttons to change color at all unless its done in the GUI constructor. if anybody can help me I would be incredibly thankful this is part of a CS project. If you think you need more info I'm happy to post whatever is needed just ask. P.S. I realize the code may be a little messy I have been fiddling with it so I apologize.
import java.awt.*;
import javax.swing.*;
public class View extends JPanel
{
private static final long serialVersionUID = 1L;
private static final int ROWS = 50;
private static final int COLS = 30;
GUI gui = new GUI();
JFrame frame;
public View()
{
}
public void run()
{
//timer.start();
gui.run();
}
public void setButtons(Cell[][] colorset)
{
for(int i = 0; i<ROWS; i++)
{
for(int j = 0; j<COLS; j++)
{
switch(colorset[i][j].getCurrent())
{
case 0:
gui.setColor('g', i, j);
case 1:
gui.setColor('r', i, j);
case 2:
gui.setColor('b', i, j);
default:
}
}
}
//this.run();
}
}'