0

I am new to Java and I want to make a simple game of chess perhaps later will add sockets. First I wanted to start designing the board to which use GridLayout JLabel as shown . My problem is that I want to add to each Label an image to simulate the " chip " of the game but do not know how, I tried many things but does not leave me, I want help please.

This code generated my table of 8x8

public class Ventana extends javax.swing.JFrame {

    private static final int COLUMNAS = 8;
    private static final int FILAS = 8;

    public Ventana() {

        initComponents();

        ImageIcon fNegra = new ImageIcon("Images/FichaNegra.png");
        ImageIcon fRoja = new ImageIcon("Images/FichaRoja.png");

        jPanel1.setLayout(new GridLayout(FILAS, COLUMNAS));

        JLabel [][] jLabel = new JLabel[FILAS][COLUMNAS];

        for (int i=0;i<FILAS;i++)
            for (int j=0;j<COLUMNAS;j++)
            {
                jLabel[i][j] = new JLabel();
                jLabel[i][j].setOpaque(true);

                if((i+j)%2 == 0)
                {
                    jLabel[i][j].setBackground(Color.WHITE);
                    jPanel1.add(jLabel[i][j]);
                }
                else
                {
                    jLabel[i][j].setBackground(Color.GRAY);
                    jPanel1.add(jLabel[i][j]);
                }       
            }
        this.add(jPanel1);
        this.setVisible(true);
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jonatan
  • 47
  • 8
  • 1
    You could have a look at [this example](http://stackoverflow.com/questions/14548808/scale-the-imageicon-automatically-to-label-size/14553003#14553003) or [this example](http://stackoverflow.com/questions/25798156/resizing-icon-to-fit-on-jbutton-in-java) or even [Stretch Icon](https://tips4java.wordpress.com/2012/03/31/stretch-icon/) – MadProgrammer Jun 01 '15 at 04:00
  • 1
    *"My problem is that I want to add to each Label an image to simulate the " chip " of the game but do not know how"* - `JLabel#setIcon`? – MadProgrammer Jun 01 '15 at 04:02
  • Thank you. Something like this "JLabel [ i] [ j ] .setIcon ( fNegra ) ;" But I can not see the icon in the JLabel . I want to add the pieces of chess board – Jonatan Jun 01 '15 at 04:54
  • So? Instead, set the background color of the JLbel, and use a "blank" transparent image, which is the same size as all the other pieces, this way, the label will be able to calculate the size it needs to be laid out – MadProgrammer Jun 01 '15 at 05:00
  • I tried this: textField [ i] [ j ] = new JLabel ("H " fNegra , textField [ i] [ j ] .center ) ; textField [ i] [ j ] .setOpaque ( true); but just show me the letter " H" and not the image , sorry I'm still newbies in this... I also removed the background to the image of the piece but does not know if it's relevant that ... – Jonatan Jun 01 '15 at 06:21
  • [How to Use Labels](http://docs.oracle.com/javase/tutorial/uiswing/components/label.html) – MadProgrammer Jun 01 '15 at 06:40
  • See also [Making a robust, resizable Swing Chess GUI](http://stackoverflow.com/q/21142686/418556). – Andrew Thompson Jun 01 '15 at 07:16
  • Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Jun 01 '15 at 07:19
  • Well I got insert the icon button with this line textField [ i] [ j ] .setIcon (new ImageIcon ( this.getClass () getResource ( " Images / FichaNegra.png " ) ) . ) ; someone could explain why this if I could put the icon , and the other is not ... – Jonatan Jun 03 '15 at 03:19

0 Answers0