bpuskas
bpuskas

Reputation: 25

Java GUI update

I want to write a simple color changer game I wrote some code, but not updating the GUI Only one of the clicked buttons, but the other is not. What is the problem? How could more easily identify the dynamic buttons?

 `
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;

public class MainFrame extends JFrame { public int[] table = { 2, 2, 2, 0, 1, 0, 1, 1, 0, 0, 1, 0, 3, 3, 3,}; public int click; public int[] clicked = {-1, -1}; Color color[] = {Color.white, Color.lightGray, Color.red, Color.blue, Color.green}; public MainFrame() { setLayout(new java.awt.GridLayout(5, 3)); setSize(400, 600); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); for (int i = 0; i < 15; ++i) { final JButton b = new JButton(String.valueOf(i)); add(b); if (table[i] == 0) { b.setEnabled(false); b.setBackground(null); } b.setBackground(color[table[i]]); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent f) { if (f.paramString().contains("cmd=0,")) { click = 0; } else if (f.paramString().contains("cmd=1,")) { click = 1; } else if (f.paramString().contains("cmd=2,")) { click = 2; } else if (f.paramString().contains("cmd=4,")) { click = 4; } else if (f.paramString().contains("cmd=6,")) { click = 6; } else if (f.paramString().contains("cmd=7,")) { click = 7; } else if (f.paramString().contains("cmd=10,")) { click = 10; } else if (f.paramString().contains("cmd=12,")) { click = 12; } else if (f.paramString().contains("cmd=13,")) { click = 13; } else if (f.paramString().contains("cmd=14,")) { click = 14; } if (clicked[0] == -1) { clicked[0] = click; } else if (clicked[1] == -1) { clicked[1] = click; } if (clicked[0] != -1 && clicked[1] != -1) { int temp = table[clicked[0]]; table[clicked[0]] = table[clicked[1]]; table[clicked[1]] = temp; b.setBackground(color[table[clicked[0]]]); b.setBackground(color[table[clicked[1]]]); clicked[0] = -1; clicked[1] = -1; } } } ); } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new MainFrame(); } }); } }

`

Upvotes: 2

Views: 94

Answers (3)

bpuskas
bpuskas

Reputation: 25

Thank you for your answers! They were very helpful!

The game is: Initial state: all of 3 blue square on below, and all of 3 red square overhead Final state: all of 3 red square on below, and all of 3 blue square overhead Replace the blue and red squares. One step only blue or red squares to swap with gray squares . Just neighboring squares exchanged.

Now is almost ready non computer solution I'll try further develop the game The correct solution is to be found in the computer breadth-first search or depth-first-search method.

Here's the code:

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class MainFrame extends JFrame {

    public int[] table = {
        2, 2, 2,
        0, 1, 0,
        1, 1, 0,
        0, 1, 0,
        3, 3, 3,};
    public int click;
    public int[] clicked = {-1, -1};
    Color color[] = {Color.white, Color.lightGray, Color.red, Color.blue, Color.green};
    public final JButton[] buttons = new JButton[15];

public boolean possibleSwap (int clicked1, int clicked2){
boolean swap = false;
if ((Math.abs(clicked[0]-clicked[1])==1 || Math.abs(clicked[0]-clicked[1])==3 )&&((clicked[0] != -1 && clicked[1] != -1) && ((table[clicked[0]] == 1 && table[clicked[1]] > 1) || (table[clicked[1]] == 1 && table[clicked[0]] > 1)))){
swap = true;}
return swap;
}


    public MainFrame() {


        setLayout(new java.awt.GridLayout(5, 3));
        setSize(400, 600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);


        for (int i = 0; i < 15; ++i) {
            buttons[i] = new JButton(String.valueOf(i));
            buttons[i].setActionCommand(String.valueOf(i));
            buttons[i].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent f) {
                    click = Integer.parseInt(f.getActionCommand());
                    System.out.println("click:: " + click);
                    if (clicked[0] == -1) {
                        clicked[0] = click;
                    } else if (clicked[1] == -1) {
                        clicked[1] = click;
                    }

                    if (possibleSwap(clicked[0],clicked[1])) {
                        int temp = table[clicked[0]];
                        table[clicked[0]] = table[clicked[1]];
                        table[clicked[1]] = temp;

                        // get button references from array using index -> set swapped background color
                        buttons[clicked[0]].setBackground(color[table[clicked[0]]]);
                        buttons[clicked[1]].setBackground(color[table[clicked[1]]]);

                        System.out.println("clicked[0]:: " + clicked[0]);
                        System.out.println("clicked[1]:: " + clicked[1]);

                        revalidate();


                        clicked[0] = -1;
                        clicked[1] = -1;
                    }

                }
            });



            add(buttons[i]);
            if (table[i] == 0) {
                buttons[i].setEnabled(false);
                buttons[i].setBackground(null);

            } else {
                buttons[i].setBackground(color[table[i]]);
            }

        }
        for (int j = 0; j < table.length; j++) {
            System.out.print(table[j] + ", ");
            if ((j + 1) % 3 == 0) {
                System.out.print("\n");
            }
        }

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MainFrame();
            }
        });
    }
}

Upvotes: 0

trooper
trooper

Reputation: 4512

Keep each of your buttons in an array. You can use the index of the array to reference the buttons later.

public final JButton[] buttons = new JButton[15];

You can also store the ID (array index) on each button using setActionCommand.

for (int i = 0; i < 15; ++i) 
{
    buttons[i] = new JButton(String.valueOf(i));
    buttons[i].setActionCommand(String.valueOf(i));
    ...
}

Later you can retrieve that ID from the ActionEvent using getActionCommand.

buttons[i].addActionListener(new ActionListener() 
{
    public void actionPerformed(ActionEvent f) 
    {
        click = Integer.parseInt(f.getActionCommand());

        if (clicked[0] == -1) {
            clicked[0] = click;
        } else if (clicked[1] == -1) {
            clicked[1] = click;
        }
        ...
    }
});

Now that you are able to identify the buttons, you can correct your color swapping logic to change the colors on the appropriate buttons.

if (clicked[0] != -1 && clicked[1] != -1) 
{
    int temp = table[clicked[0]];
    table[clicked[0]] = table[clicked[1]];
    table[clicked[1]] = temp;

    // get button references from array using index -> set swapped background color
    buttons[clicked[0]].setBackground(color[table[clicked[0]]]);
    buttons[clicked[1]].setBackground(color[table[clicked[1]]]);

    clicked[0] = -1;
    clicked[1] = -1;
}

Upvotes: 1

FredK
FredK

Reputation: 4084

 if ( table[i] == 0 ) {
    b.setEnabled( false );
    b.setBackground( null );
 }

if table[I] is zero, you disable that button. that means you get nothing if you click in it.

Why do you add a new action listener for each button, and in each listener check for each of the other buttons/? You should create a single listener and add it to all of the buttons, then use event.getSource() to find which button was clicked.

Upvotes: 0

Related Questions