0

I'm making a board game in Java, and I am trying to render the board itself in JPanel. The way I'm trying to do this is by inserting a image of the board (which is represented by a JLabel) and a grid (which is also represented by a JPanel). The idea is for each grid square to correspond to the squares on the image of the board.

Here is what it currently looks like

Here is what it currently looks like.

As you can see, it's not aligning correctly. Here is the code I tried to accomplish this:

public class BoardImage {
    ImagePanel test = new ImagePanel();
    GridPanel grid = new GridPanel();

    public void returnBoardPanel() {
        JFrame frame = new JFrame("JPanel Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel image = new JLabel();
        image = test.createImage();
        image.setLayout(new BorderLayout());
        frame.setContentPane(image);
        frame.add(grid.paintMe());
        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);
    }

    public static void main (String[] agrs) {
        BoardImage test = new BoardImage();
        test.returnBoardPanel();
    }
}

class ImagePanel extends JPanel{
    public JLabel createImage() {
        JLabel temp = new JLabel();
        ImageIcon icon = new ImageIcon(this.getImage("/boardEdit.jpeg"));
        temp.setIcon(icon);
        temp.setBounds(0, 0, 552, 575); //needed in order for 24x25 to work
        return temp;
    }

    public Image getImage(String filePath) {
        int width, height;
        Image tempImage = null;
        try {
            /* Loads the image, and assigns it to the tempImage var */
            URL imagePath = BoardImage.class.getResource(filePath);
            tempImage = Toolkit.getDefaultToolkit().getImage(imagePath);
        }
        catch(Exception e){ //if the filePath does not exist, or something else messed up
            System.err.println("We were not able to load the requested image form the given filePath: " + "\n" + filePath);
        }
        return tempImage;
    }
}

class GridPanel extends JPanel{
    private JLabel[][] grid;

    public GridPanel paintMe(){
        this.setBounds(0, 0, 552, 575); //needed in order for 24x25 to work
        this.fillGrid();
        this.setOpaque(false);
        return this;
    }

    public void fillGrid() {
        this.setLayout(new GridLayout(24, 25));

        grid = new JLabel[24][25];

        for (int i = 0; i < 24; i++) {
            for (int j = 0; j < 25; j++) {
                grid[i][j] = new JLabel();
                grid[i][j].setBorder(new LineBorder(Color.BLACK));//keep this until it works
                grid[i][j].setOpaque(false);
                this.add(grid[i][j]);
            }
        }
    }
}

I've tried everything I've found online, and on SO, but I can't seem to figure it out. Any suggestions?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
JKing
  • 73
  • 6
  • Your layering appears to be working, and all it seems that you have to do is juggle your numbers a bit so that your grids line up. This is something *you* will need to work on, as I doubt anyone here wants to do this for you. Note that for my money, I wouldn't layer JPanels, but rather draw the image in a single JPanel's paintComponent method along with the grid. – Hovercraft Full Of Eels Feb 10 '18 at 14:48
  • 1
    @HovercraftFullOfEels Those are great points. OTOH if the squares in the board game are anything like the places in a chess board, it might make sense to carve the image up and use each sub-image as the icon for a `JButton` as seen in [this answer](https://stackoverflow.com/a/10862262/418556). – Andrew Thompson Feb 10 '18 at 15:38
  • I'm looking into BufferedImages now, it looks like that's exactly what I need. Thank you! – JKing Feb 10 '18 at 17:49

0 Answers0