youassassin
youassassin

Reputation: 188

GUI not updating as it should

I am not familiar with GUI's that much and this is my first attempt at one. I have created a GUI for my A* algorithm. It is set up as a grid of 15 JButtons. The problem I am running into is when I reset the GUI (reset button) the backgrounds don't fully change unless I click another JButton. Needless to say, I am stumped.

Edit: The backgrounds of 70-95% of the grid change to their actual color. If I click another button then 100% of the grid is correct. I also notice that if I hold down on the Reset button for a longer period of time the more backgrounds change properly.

Here's a simplified version of my problem.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.util.*;
public class Driver {
    static Random rand = new Random();
    public static void main(String[ ] args) {
        final int GRID_SIZE = 15;
        GUI run = new GUI();
        int[][] a = new int[GRID_SIZE][GRID_SIZE];
        for(int x = 0; x < a.length; x++)
            for(int y = 0; y < a[x].length; y++)
            {
                a[x][y] = rand.nextInt(10);
                switch(a[x][y])
                {
                case 4: run.getGrid()[x][y].setBackground(Color.MAGENTA);
                    break;
                case 5: run.getGrid()[x][y].setBackground(Color.CYAN);
                    break;
                case 7:
                case 8:
                case 9: run.getGrid()[x][y].setBackground(Color.BLACK);
                    break;
                }
            }
        run.getReset().addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                for(int x = 0; x < a.length; x++)
                    for(int y = 0; y < a[x].length; y++)
                    {
                        a[x][y] = rand.nextInt(10);
                        switch(a[x][y])
                        {
                        case 4: run.getGrid()[x][y].setBackground(Color.MAGENTA);
                            break;
                        case 5: run.getGrid()[x][y].setBackground(Color.CYAN);
                            break;
                        case 7:
                        case 8:
                        case 9: run.getGrid()[x][y].setBackground(Color.BLACK);
                            break;
                        default: run.getGrid()[x][y].setBackground(null);
                        }
                    }
            }
        });
    }
}

Here's the GUI

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.LineBorder;

public class GUI extends JFrame {
    private final int WINDOW_WIDTH, WINDOW_HEIGHT, GRID_SIZE;
    private JButton[][] grid;
    private JButton reset;
    private GridBagLayout gbl;
    private GridBagConstraints gbc;

    public GUI(){
        GRID_SIZE = 15;
        WINDOW_WIDTH = 500;
        WINDOW_HEIGHT = 500;
        gbl = new GridBagLayout();
        gbc = new GridBagConstraints();
        reset = new JButton("Reset");
        grid = new JButton[this.GRID_SIZE][this.GRID_SIZE];
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(gbl);
        this.setSize(this.WINDOW_WIDTH, this.WINDOW_HEIGHT);
        this.buildButtons();
        this.setVisible(true);
    }
    private void buildButtons()
    {
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = .5;
        for(int i = 0; i < grid.length; i++)
        {
            gbc.ipady = this.WINDOW_HEIGHT/(this.GRID_SIZE * 3);
            for(int j = 0; j < grid[i].length; j++)
            {
                grid[i][j] = new JButton();
                grid[i][j].setOpaque(true);
                grid[i][j].setBorder(LineBorder.createBlackLineBorder());
                grid[i][j].setText(i*this.GRID_SIZE+j+"");
                gbc.gridx = i;
                gbc.gridy = j+1;
                this.add(grid[i][j], gbc);
            }
        }
        gbc.gridx = 0;
        gbc.weighty = .5;
        gbc.gridy = this.GRID_SIZE+1;
        gbc.anchor = GridBagConstraints.PAGE_END;
        gbc.ipady = this.WINDOW_HEIGHT/(this.GRID_SIZE + 2);
        gbc.gridwidth = this.GRID_SIZE;
        this.add(reset, gbc);
    }
    public JButton[][] getGrid()
    {
        return this.grid;
    }
    public JButton getReset()
    {
        return this.reset;
    }
}

My guess is the JButtons' backgrounds are being set after the ActionListener goes back into "listening". I just have no clue how to figure this out.

Upvotes: 1

Views: 76

Answers (1)

c0der
c0der

Reputation: 18812

I made some changes to Driver class. See comments and don't hesitate to ask if it is not clear or does not answer your question:

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;

public class Driver {

    static Random rand = new Random();
    private static final int GRID_SIZE = 15;

    private int[][] a; //make it a field
    private GUI run; //make it a field
    Color defaultColor;

    //add constructor
    Driver(){

        run = new GUI();
        a = new int[GRID_SIZE][GRID_SIZE];
        defaultColor = run.getBackground();

        //the following code is used twice : use a method
        reDrawGui();

        run.getReset().addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {

                reDrawGui();
            }
        });
    }

    /**
     *
     */
    private void reDrawGui() {

        for(int x = 0; x < a.length; x++) {

            for(int y = 0; y < a[x].length; y++)
            {
                a[x][y] = rand.nextInt(10);
                JButton button = run.getGrid()[x][y];

                switch(a[x][y])
                {
                case 4: button.setBackground(Color.MAGENTA);
                    break;
                case 5: button.setBackground(Color.CYAN);
                    break;
                case 7:
                case 8:
                case 9:button.setBackground(Color.BLACK);
                    break;
                 default : button.setBackground(defaultColor);
                }

            }
        }
        //revalidate and repaint after gui changed
        run.revalidate(); run.repaint();
    }

    public static void main(String[ ] args) {

        //move intialization code to constructor
        new Driver();
    }
}

Upvotes: 1

Related Questions