So I am relatively new to programming and I am trying to create a basic checkers game.What I am having trouble with at the moment is when I click one of my JButtons that contains a piece I need to essentially replace it with a new button at it's specific location. With the code I have thus far though it is removing said JButton but not replacing it with a new one.
I have been trying to achieve this only on the 0,0 piece before applying it to the whole board.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class checkersBeBitchin extends JFrame{
JButton[][] squares = new JButton[8][8];
Container box = getContentPane();
int[][] pieceTracker = new int[8][8];
JPanel board = new JPanel();
/**
* @param args
*/
public static void main(String[] args) {
checkersBeBitchin begin = new checkersBeBitchin();
}
public checkersBeBitchin(){
box.setLayout(new BorderLayout());
makeBoard();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(600,600);
setTitle("Checkers");
}
@SuppressWarnings("deprecation")
private void makeBoard() {
board.setLayout(new GridLayout(8,8));
for (int i=0; i<8; i++){
for (int j=0; j<8; j++) {
squares[i][j] = new JButton();
ActionListener actionListener = new Board();
squares[i][j].addActionListener(actionListener);
if((i%2 != 0 && j%2 !=0) ||(i%2==0 && j%2 == 0) ){
squares[i][j].setBackground(Color.black);
squares[i][j].setLabel("xy "+i+j);
//System.out.println("Black"+i+","+j); debugging
if(i<3){
int blue = 1;
Icon piece = new ImageIcon(getClass().getResource("/resources/piece.png"));
JButton button = new JButton();
JLabel bluePiece =new JLabel(piece);
pieceTracker[i][j]=blue;
//squares[i][j].setRolloverIcon("image dir") to make it prettier down the road.
squares[i][j].add(bluePiece);
ActionListener Listener = new Blue();
button.addActionListener(Listener);
}
else if (i>4){
int red=-1;
Icon piece = new ImageIcon(getClass().getResource("/resources/piece2.png"));
JButton button = new JButton(piece);
JLabel redPiece =new JLabel(piece);
squares[i][j].add(redPiece);
pieceTracker[i][j]=red;
ActionListener Listener = new Red();
button.addActionListener(Listener);
//squares[i][j].setRolloverSelectedIcon("/resources/piece2alt.png");
}
}
else{
squares[i][j].setBackground(Color.white);
squares[i][j].setLabel("xy "+i+j);
pieceTracker[i][j]=0;
//System.out.println("White"+i+","+j); //debugging
}
board.add(squares[i][j]);
}
}
box.add(board, BorderLayout.CENTER);
}
private class Blue implements ActionListener{
public void actionPerformed (ActionEvent e){
System.out.println("You sexy Blue beast.");
Object x = e.getSource();
Component comp = (Component) x;
board.remove(comp);
JButton replace = new JButton();
replace.setBackground(Color.GREEN);
squares.toString();
squares[5][5].add(replace);
board.revalidate();
board.repaint();
System.err.println(x);
}
}
private class Red implements ActionListener{
public void actionPerformed (ActionEvent e){
// System.out.println("You sexy Red beast.");
Object x = e.getSource();
Component comp = (Component) x;
board.remove(comp);
board.revalidate();
board.repaint();
}
}
private class Board implements ActionListener{
public void actionPerformed (ActionEvent e){
Object x = e.getSource();
Component comp = (Component) x;
// System.out.println(comp);
board.remove(comp); //removes the piece
// System.out.println("You clicked the board. Good for you.");
if (e.getActionCommand().equals("xy 00")){
System.out.println("Awesome");
// String s = e.getActionCommand();
String jj = Character.toString(s.charAt(4));
String ii = Character.toString(s.charAt(3));
// System.out.println("j:"+jj+" i:"+ii);
int coordsI = Integer.parseInt(ii);
int coordsJ = Integer.parseInt(jj);
JButton replace = new JButton();
replace.setBackground(Color.GREEN);
// squares.toString();
squares[coordsI][coordsJ].add(replace);
board.revalidate();
board.repaint();
}
board.revalidate();
board.repaint();
}
}
}
Instead of adding / removing buttons, I would keep the state of a square in a model. By doing such, you could toggle the state of any square knowing its new state.
public class MyCheckerSquare extends JButton {
private CheckerPiece presentPiece;
public MyCheckerSquare(Color background) {
super();
setBackground(background);
}
public void setCheckerPiece(CheckerPiece piece) {
presentPiece = piece;
// Change the icon regarding the given piece
}
}
public enum CheckerPiece {
BLACK, RED
}
All the checkers pieces would be handled by another class, sort of a controller.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.