0

What data type is the source of the event passed to the listener? Define a variable to hold that type:
<TheDataType> selectedSquare = null; //Define the variable to hold the selected square

if(selectedSquare ==  null)
   selectedSquare = ae.getSource(); // save reference to the selected square .
0

Would I put this outside of the Button Handler class, or inside of it?

0

Define the variable at the class level so it is aviable for all calls to the listener method.
The executable code goes in the listener.

0

What is the source of the event passed in the event object?
Your code compares it to what's in the squares array.

0

Did you code and test it for the three possible conditions when the listener is called: first, second same and second new?

0

I am going to use two variables, both of the data type JButton: one is called selectedSquare, the other called newSqaure. How would I use the variables to pint out the location of the selected sqaure and where the icon should go if the new square is then selected?

0

When you click on a button the listener is called with an event object. In the listener you should add code as has been discussed earlier in the thread. These last discussions were about how to save and work with a "selected" square. First click saves it, later clicks test and use it as a source or clear it if was the same square.

0

Could you point this out to me in my code? This is a previously modified code.

0

All that I have changed is adding the variables selectedSqaure and newSquare at the beginning. I have no idea what to change out of a fear of not being able to change it back. I have yet to think like a programmer on what to do next. I do as I think, not think then do

0

You'll have to change that to think then do. Otherwise you'll be wasting a lot of time doing things that end in a dead end.

If you have only added a variable definition you need to add the logic that uses that variable.

Edited by NormR1

0

I think I may be in one. Do you have any suggestions? I don't have the time to scrap this code and start from the drawing board. I have already told my lab partner that if we don't figure out how to do the AI by Monday, then we would simply make it a two player game.

0

We have been talking about the first step in the list: selecting a square and remembering it
Most of the possibilities have been discussed several times. Make a list of the steps the code will have to do to do the selection. Get that done first. That will be the guide you'll use to write the code.

The only code to scrap is what is currently in the listener. It needs to be redesigned.

0

Sorry it took so long to reply. Should I scrap everything in the ButtonHandler Class, including the for loops?

0

Could I at least get an example of coding that could work? While the handler may not work the way I need it to work, I have seen it work in action.

0

Have you worked on saving and using the "selected" square yet? We talked about it a lot. My ideas are all based on that working.

0

I have but nothing is working. I keep thinking I have something, but it doesn't compile. I work better after seeing a good example, and I have tried looking at other types of code, but they work by clicking and dragging a piece, or the code just doesn't make sense to me. I am using an icon as the piece, and I would just like to see an example of when you click on a square, and then click on another square, the icon will move.

0

Post the code you have that saves the "selected" square and then does the tests we talked about earlier.

0
      private class ButtonHandler implements ActionListener
      {
         public void actionPerformed( ActionEvent ae )
         {
            for ( int i = 0; i < SIDE; i++ )
            {
               for ( int j = 0; j < SIDE; j++ )
               {
                  if(ae.getSource() == squares[i][j] && squares[i][j].getIcon() == blackPiece)
                  {
                     squares[i][j] = selectedSquare;        
                  }
                        if(selectedSquare == null && squares[i][j].getBackground() == Color.black)
                        {
                            selectedSquare = temp;
                            temp = newSquare;
                            selectedSquare.setIcon(null);
                            newSquare.setIcon(blackPiece);
                        }                   
               }
            }
         }
      }

I keep getting a null pointer exception

0

First get rid of the for loops. That code is not being used.

The selectedSqr should be the value of the source of the event when a button/square is clicked.
On The first click you save it, on the second click you use it as the source of the move. You detect the first click by seeing that selectedSqr is null. It won't be null for the second click.

Edited by NormR1

0
      private class ButtonHandler implements ActionListener
      {
         public void actionPerformed( ActionEvent ae )
         {
                  if(ae.getSource() == squares[i][j] && squares[i][j].getIcon() == blackPiece)
                  {
                     squares[i][j] = selectedSquare;
                            selectedSquare = ae.getSource();        
                  }
                        if(selectedSquare == null && squares[i][j].getBackground() == Color.black)
                        {
                            selectedSquare = temp;
                            temp = newSquare;
                            selectedSquare.setIcon(null);
                            newSquare.setIcon(blackPiece);
                        }                   
         }
      }

How does this look?

0

This is what I mean. I have spent most of my life working by example, but because I don't have a good example to go off of, I don't know what I am supposed to do. BTW, this is just the coding for the ButtonHandler class, not the whole class.

0

Most of the logic to handle button clicks will be in the listener.

You are going to have a hard time with programming if you can only copy from existing code. You must be able to think through what steps the program needs to take to do a task.

Without a logical description of what a method is supposed to do you can not write the code.

Edited by NormR1

0

The second if statement was supposed to set the information in the selected square to a temporary section, which would transfer the status of the selected square to the new square, which would set the icon in the new square, and delete the icon in the old square.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.