e.getSource Buttons in a Checkers game are not working. GUI

119 views Asked by At

I am programming using DrJava. All of this code fall under the action performed section in a GUI.

Everything in here is working properly except for those 2 e.getSource buttons.

The 2 statements are the if (e.getSource()== b[a-9]) and if (e.getSource()== b[a-7]).

I added action listeners to every button.

When I click on the button, no actions are being performed.

I put a System.out.println to see if they work, but once clicked on, it is also not printing it onto the screen.

This obviously means they're not working, but why? If you want the whole code to try, just ask and I'll give it to you. This is my CPT for grade 11.

if(player==1) 
{
    for(int a=41; a<64; a++) 
    {
        if (e.getSource()==b[a]) 
        {
            board();
            b[a].setBackground(new Color(0,255,0));
            if(!b[a].getText().equals(b[a-9].getText())&&!b[a].getText().equals(b[a-7].getText())) 
            {
                b[a-9].setBackground(new Color(0,255,0));
                b[a-7].setBackground(new Color(0,255,0));

                //THIS IS NOT WORKING____________________________________
                if (e.getSource()== b[a-9])
                {
                    System.out.println("NOT WORKING");
                    b[a-9].setText(piece1);
                    b[a].setText("");
                    board();
                    player2();
                    player=2;
                }

                if (e.getSource()== b[a-7])
                {
                    System.out.println("NOT WORKING");
                    b[a-7].setText(piece1);
                    b[a].setText("");
                    board();
                    player2();
                    player=2;
                }
                //___________________________________________________________      
                if(e.getSource()==b[47])
                {
                    board();
                    b[47].setBackground(new Color(0,255,0));
                    if(!b[47].getText().equals(b[47-9].getText()))
                    {
                        b[47-9].setBackground(new Color(0,255,0));
                    }
                }
                if(e.getSource()==b[48])
                {
                    board();
                    b[48].setBackground(new Color(0,255,0));
                    if(!b[48].getText().equals(b[48-7].getText()))
                    {
                        b[48-9].setBackground(new Color(0,255,0));
                    }
                }
            }   
        }
    }      
}

Thank you.

1

There are 1 answers

1
camickr On BEST ANSWER

First you have this check:

if (e.getSource()==b[a]) 

Then if that condition is true you have this check:

if (e.getSource()== b[a-9])

Well that can never be true because the same button can't equal button (a) and (a-9) at the same time.

Same with the (a-7) condition. It can never be equal to (a) and (a-7) at the same time.