Chess board in java

0

I have been asked to create a chess board and now, all right, the problem arises when I want to give color to the boxes and I do not understand why. The code below creates the board, the problem is here:

        if((y+x+1)%2==0){
            Casillas[y][x].setBackground(Color.BLACK);
        }else{
            Casillas[y][x].setBackground(Color.WHITE);
        }

I get the following error

prueba.java:16: error: cannot find symbol

            Casillas[y][x].setBackground(Color.BLACK);
                                                  ^
symbol:   method setBackground(Color)
 location: class String
   prueba.java:18: error: cannot find symbol
                    Casillas[y][x].setBackground(Color.WHITE);
                                                  ^
   symbol:   method setBackground(Color)
   location: class String
   2 errors

I really do not understand, I have seen other codes to create a board, but, honestly I do not want to copy and paste, I would like to understand the code that I have created, to address this problem.

import java.awt.Color;
import java.awt.*;


import javax.swing.*;

public class prueba {

public static JPanel TableroAjedrez() {
    JPanel PanelTableroAjedrez = new JPanel();
    String[][] Casillas = new String[8][8];
    for(int y=0; y < Casillas.length; y++) {
        for(int x=0; x < Casillas[y].length; x++) {
            PanelTableroAjedrez.add(new JButton(Casillas[y][x]));
            if((y+x+1)%2==0){
                Casillas[y][x].setBackground(Color.BLACK);
            }else{
                Casillas[y][x].setBackground(Color.WHITE);
            }
        }
    }

    PanelTableroAjedrez.setLayout(new GridLayout(8, 8));


    return PanelTableroAjedrez;
}       






public static void main(String[] args) {
    JFrame Ventana = new JFrame("Tablero de Ajedrez");
    Ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    Ventana.setLayout(new GridLayout(1, 1));
    Ventana.add(TableroAjedrez());

    Ventana.setLocationRelativeTo(null);
    Ventana.setPreferredSize(new Dimension(500, 500));
    Ventana.pack();
    Ventana.setVisible(true);
}

}

    
asked by Barly Espinal 20.07.2018 в 05:55
source

1 answer

4

What happens is that the array called Casillas is of type String [] [] that is:

Casillas[y][x]

It is a variable of type String and the class String does not have a method called setBackground .

What I understand you want is to implement the chess board so that each box is a JButton . A JButton if you have the setBackground method.

So I suggest you replace your for with the following:

for(int x=0; x < Casillas[y].length; x++) {
    // en lugar de hacer el add a new JButton 
    // primero declaras una variable asignandole lo que ya pasabas
    // en el metodo add
    final JButton jButton = new JButton(Casillas[y][x]);
    PanelTableroAjedrez.add(jButton);
    if((y+x+1)%2==0){
        // al botón le pones el color negro como en tu condición
        jButton.setBackground(Color.BLACK);
    }else{
        // o le pones el color blanco aquí tu logica es practicamente la misma
        jButton.setBackground(Color.WHITE);
    }
}

I hope I have been clear. I remain attentive to any comment

    
answered by 20.07.2018 / 08:21
source