1
for(y=0;y<length;y++){
    for(x=0;x<width;x++){

            grid[x][y]=new JButton(" ");
            grid[x][y].addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                ((JButton)e.getSource()).setBorder(border);;
                System.out.println("Where do you want to move this piece");
            }
        }); 
            grid[x][y].addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent r){
                    grid[x][y]=grid[x][y-1];
                    ((JButton)r.getSource()).setBorder(border);;
                    System.out.println("Derp");
                }
            });
            frame.add(grid[x][y]);
    }
}

I want to click a piece and then when you click a different square, the piece will move up one. I know this is wrong and I will show the legal moves but for right now I need to get the pieces moving. Please help!! I am completely stuck until I can get this figured out.

user2774713
  • 79
  • 1
  • 2
  • 6
  • 1
    So `grid` is an array of `JButton` and you are just moving buttons around in the grid? - That's what the code looks like. You should consider splitting your "game state" and your "game view" into different classes. – John3136 Sep 15 '13 at 23:42
  • What makes you think the code is wrong? – Tony Ennis Sep 15 '13 at 23:50
  • 1
    See also this [example](http://stackoverflow.com/a/2562685/230513) and [variation](http://stackoverflow.com/a/2563350/230513). – trashgod Sep 16 '13 at 00:02
  • I just want the first Jbutton that I clicked moved to the position of the second spot I clicked. e=r if it were that simple – user2774713 Sep 16 '13 at 03:13
  • Maybe put in a call to `repaint()`. Graphics can be finicky so it's good to borrow from a working example. – clwhisk Sep 16 '13 at 04:38

1 Answers1

2

First off, make sure all syntax is correct as I see double semi colons on this line:

((JButton)e.getSource()).setBorder(border);;

Also this line should be ouside the ActioneListener, as you want to set the border of the button, not when you click but when you setup the actions.

If you don't want to change much in the current way you are doing this, try this: You should create a GameObject class have it extend JButton also have it set the border of the button to a size that you will like. Now create a grid[x][y] of game objects. Then create a Piece class and a Space class, both should extends GameObject. The piece class should overwrite the paint(Graphics g) method of the JButton and render an image of the piece. Then when you click on a piece raise a flag and keep a reference of this piece in the main class, then when you click on a space the program will swap between the two game objects. (Note: You'll have to populate the grid with Space class instances.)

Dealing with JButtons directly as "Pieces" can be tricky and is not a very good way to implementing a Chess game.

I strongly recommend if you really want to create a chess game, to take a look at this tutorial:

http://proghammer.wordpress.com/2010/08/08/chess00-introduction/

It's for beginners, step by step. I created a chess game with this :D Enjoy.

John smith3
  • 139
  • 1
  • 9
  • I just want the first Jbutton that I clicked moved to the position of the second spot I clicked. e=r if it were that simple – user2774713 Sep 16 '13 at 02:10