1

I have an image that has to make up a 8x8 grid, so it is a background for the board.

I have been told it is possible to do this using ImageIcon and a JLabel, which I tried and it doesn't seem to work.

  • It doesn't allow me to add components to it (piece, which is also a JLabel).
  • Also when the program is running and I click on a square- it disappears, which is not ideal as it is supposed to be the background.

Here is the code:

      for (int i = 0; i < 8; i++)
       {
        for (int j = 0; j < 8; j++)
        {
            square=new JLabel();
            square.setIcon(icon);
            chessBoard.add( square );
        }
       }

The full code: http://pastebin.com/YdavUmGz

Am I doing something horribly wrong with this background image?

Any help would be appreciated, thanks in advance.

PeakGen
  • 21,894
  • 86
  • 261
  • 463
user1902535
  • 155
  • 1
  • 3
  • 10
  • How are you loading the image? Is the image not appearing? You can only set one IconImage to one JLabel, so if you want 64 (8 X 8) images, you need 64 JLabels. It would be simpler to just have one big image that is a grid (8 X 8). Just a thought. – John Mar 21 '13 at 02:51
  • The images are appearing fine exactly how I need it, however I feel like the movement code is affecting it because I can drag the squares. But it's supposed to be the background for the pieces. – user1902535 Mar 21 '13 at 02:53
  • 1
    I'd get rid of the `JLayeredPane`. Simply use the `GridLayout` for the time been. You can add other components to `JLabel`, so you should start be setting the grid label's layout to something like `BorderLayout`. Then when you click on, you can simply add your `JLabel` piece to it. – MadProgrammer Mar 21 '13 at 02:54
  • Where would I put the MouseListener then? and would the mouse dragging still work? Because they are attacked to the JLayeredPane. – user1902535 Mar 21 '13 at 02:59
  • @user1902535 It depends. You could place the mouse listener on each individual "grid" label. DnD would still work and you could even use the `GlassPane` instead – MadProgrammer Mar 21 '13 at 03:01
  • I would be extremely grateful if you could show me an example of either implementation, as I'm quite confused about how to go about this. (My first time using Java/Swing) – user1902535 Mar 21 '13 at 03:09
  • 1
    This posting shows one way to drag images around a chess board: http://stackoverflow.com/a/4674686/131872 – camickr Mar 21 '13 at 03:14
  • @MadProgrammer ehhh ... again proposing that ... suboptimal approach of adding something to a non-container. Simply don't - the outcome is undetermined, undefined, implementation/laf dependent, can change (read: break) without notice. DontDontDont! – kleopatra Mar 21 '13 at 11:34
  • @kleopatra This is probably something we will never agree on. My personal preference would be to use a custom panel capable of painting the background, but sometimes we need a simpler solution. JLable (for good or bad) is a container. The label it self is painted, by the UI in the paintComponent method BEFORE the children. The only risk (as I see it) is that the UI will change the layout IN the paint routine, which is a significant NO NO and would be more worrying then me adding something to the label. JLabel is the only component that has Icon support by default - unless you make your own ;) – MadProgrammer Mar 21 '13 at 20:20
  • it's completely undefined what the outcome is - relying on undefined stuff in productions is .. worst practice. As experienced developers, we shouldn't start the ch.. cough ...newbies doing it - at least not until they can weigh the pros/cons themselves. – kleopatra Mar 22 '13 at 09:18

1 Answers1

3

Are you looking for something like this?

import java.awt.*;
import javax.swing.*;

public class ChessBoard extends JFrame {

    private JPanel panel;

    public ChessBoard() {
        panel = new JPanel();
        panel.setLayout(new GridLayout(8, 8, 0, 0)); //Create the board
        //Add JLabels
        for (int i = 0; i < 64; i++) {
            JLabel label = new JLabel();
            label.setIcon(
                    new ImageIcon(getClass().getResource("images/face.png")));
            panel.add(label);
        }
        //Add the panel to the JFrame
        this.add(panel);
        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(
                    UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ChessBoard();
            }
        });
    }
}

enter image description here

mKorbel
  • 109,525
  • 20
  • 134
  • 319
PeakGen
  • 21,894
  • 86
  • 261
  • 463