Hello, I'm new to this site, and i really need help with something. I have been working on a checkerboard project for my second programming class, and now I'm stuck. I now how to set the icons, but now I am absolutely confounded as to how I move the icon from one jbutton to the nu=ext as a way of movement. I want to be able to do this by use of a set of mouse clicks. I also need to figure out how to do the AI for the opponent. Someone told me that i should set up a separate class for the pieces. I really need help, and this is only my first year doing programming. Any help would be appreciated.

   import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
   import java.util.Scanner;

   public class CheckerBoard extends JFrame
   {
      Scanner scan = new Scanner(System.in);    
      public static final int SIDE = 8;
      private Container contents;
      private JButton [][] squares;
      Icon redPiece = new ImageIcon("RedPiece.png");
      Icon blackPiece = new ImageIcon("BlackPiece.png");
      Icon kingPiece = new ImageIcon("Ring.png");

      public CheckerBoard( )
      {
         super( "Click a square to reveal its position" );
         contents = getContentPane( );

      // set layout to an 8-by-8 Grid
         contents.setLayout( new GridLayout( SIDE, SIDE ) );

         squares = new JButton[SIDE][SIDE];

         ButtonHandler bh = new ButtonHandler( );

         for ( int i = 0; i < SIDE; i++ )
         {
            for ( int j = 0; j < SIDE; j++ )
            {
            // instantiate JButton array
               squares[i][j] = new JButton( );

            // make every other square red
               if ( ( i + j ) % 2 == 0  )
                  squares[i][j].setBackground( Color.RED );

               if ( ( i + j ) % 2 != 0  )   
                  squares[i][j].setBackground( Color.BLACK );

               if(squares[i][j].getBackground()==Color.black && i>=5)
                  squares[i][j].setIcon(blackPiece);
               if(squares[i][j].getBackground()==Color.black && i<3)
                  squares[i][j].setIcon(redPiece);      
            // add the JButton
               contents.add( squares[i][j] );

            // register listener on button
               squares[i][j].addActionListener( bh );
            }
         }

         setSize( 400, 400 );
         setVisible( true );
      }

      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].setIcon(null);       
                  }
                  else if(ae.getSource() == squares[i][j] && squares[i][j].getIcon() == null && squares[i][j].getBackground() != Color.red)
                  {
                     squares[i][j].setIcon(blackPiece);
                  }
                  else if(ae.getSource() == squares[i][j] && squares[i][j].getIcon() == redPiece && squares[i][j].getBackground() != Color.red && i==0)
                  {
                     squares[i][j].setIcon(kingPiece);
                  }



               }
            }
         }
      }



      public static void main( String [] args )
      {
         CheckerBoard myGame = new CheckerBoard( );
         myGame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      }
   }

Recommended Answers

All 85 Replies

I now how to set the icons, but now I am absolutely confounded as to how I move the icon from one jbutton to the nu=ext as a way of movement. I want to be able to do this by use of a set of mouse clicks.

Have you figured out how to save the source button and target buttons so you can move an icon from one to the other?

No. I know I need to spend more time programming this summer, but this is due next Wednesday, but I digress. Someone told me to do this in a separate class, but I'm confused as to what I should do.

You need to think through what you want the code in the actionPerformed() method to do.
I have no idea what the current code in that method is supposed to do.

Describe what you want the code in that method to do and then make a list of the steps (in pseudo code) that the code needs to do to accomplish that task.

1) When a button is clicked, it stays selected.
2) when another button is clicked that is diagonally one row above the button, it moves the icon from the previously selected button to the newly selected button.
3) With the piece now moved, the new button is now deselected.
4) when a button is clicked diagonally two rows above another button which contains the icon of the opponents piece, the player's piece is moved to the newly selected button, and the icon of the opponents piece is removed, depending on whether the player's/ opponent's piece is coming from the left or right .
5) When either the opponent or the player reaches the opposite end of the board, the piece is turned into a king piece, which now has the ablility to move to a button either diagonally above or below one row.
The program ends when either the player's pieces or the opponent's pieces are removed, or all the icons are erased.
Also, the AI would have to move down diagonally below one row.

Now work on the code for the items in the list.
1) Select a button and keep it selected

This sounds similar to the separate class someone suggested to me, this is it, but I'm not sure how to develop it.

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;


public class Checker extends JButton {

    private boolean isSelected;

    public Checker(Icon arg0) {
        super(arg0);
        isSelected = false;
    }

    public void setSelected(boolean s) {
        isSelected = s;
    }

    private boolean isCurrentlySelected() {
        return isSelected;

    }

}

If a button is set as selected, how will you find it in the 8x8 grid of buttons?
How will you keep there from being more than one button selected?

Would I have the code choose the button based on the coordinates. If that is the case, do I have to set up a another array with the coordinates of the buttons?

Would I have the code choose the button based on the coordinates.

I thought the user clicked on a button to select it.
The questions are:
How to remember: if a button is selected and which button is selected
How to only select one button, Sort of goes with the if... part mentioned above
Would there be a way to unselect a button if the user changes his mind?

Well, this would only apply to black colored squares/ buttons, so I could modify the coding so that if a redsquare is clicked, the previous square would be disconnected.

Is that how a users could unselect a square? That's a side issue. The main issue is the selected square.

In that case, a user should be able to deselect a square by clicking on it again

That is a side issue. The main issue is the selected square.

I am not understanding what you mean. The code should deselect only the black square with an icon if it was previously selected, and if a new square diagonally one row above the selected sqaure is clicked on, then the icon should be erased from the old square and placed on the new square.

Deselection is a side issue. The main problem is keeping track of the selected square. How is that to be done?

Unfortunately, I don't know how to. To be perfectly honest, this is the very first time I have ever used jButtons. I'm sorry if I am too inexperienced.

Its not using the JButton that is important, it is keep track of which one was selected. When the listener method is called the code gets a reference to the source of the event. If that reference is saved it can be compared similiar to the ways your code already does.

But the issue is that I'm not sure what I'm supposed to keep track of: the coordinates of the buttons being pressed, or the mouse itself?

1) When a button is clicked, it stays selected.

I was working on this step.

keep track of: the coordinates of the buttons

Your design specs listed in the earlier post does not mention the location of the button in the 8x8 grid. You need to revisit that list of steps the code needs to take to include that.

Should I add something to the code in order to access a list of the coordinates of the squares?

Should I add something to the code

You should first finish the design for the code. When the design looks like it could work, THEN add to the code. Your current design has left out the location of the square and how to find the squares that are diagonal from that square.

Done for tonight see you tomorrow.

That is the issue I am having trouble with. I don't know how to locate the square.

Save that for the next step. Work on one step at a time. How to select a square and remember it.
Do you have that working? Can yoi click on a square and get one of three responses: first click, second click on new square, second click on same square. also the ignore clicks on the red squares

Get that to work before moving on to the next step. Have you got a new design for this section of code?
Wrk on the steps this section of code needs to do. Then code them.

I have no idea how to remember a square. With the design the code has right now, if you click on a black square with an icon, it removes the icon, when you click on a black square without an icon, it adds the icon. As I said before, this the the very first time I have used JButtons, so I don't have many ideas on how to do these things.

how to remember a square

One easy way is to have a class variable that is either null (if no square selected) or the reference to the selected square.
This logic so far has nothing more to do with JButtons than the code that you have already written.

I understand this, but when i think about how to code this, my mind draws a blank.

Define the variable and give it an initial value of null.
In the listener, test if the variable is null, it will be on the first click. Save the value of the source of the event in the variable. On the next call to the listener, the variable won't be null. Then test if the same square or a different square.

Do you mean like, a variable named selected is set to null, and if the correct conditons are met, a boolean will determine whether it is true or not

No need for a boolean variable, just compare the contents of the last square's reference variable with the source of the event.

The variable has the value of the source of the event from the first click.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.