0

I've been studying Java at my University, I'm quite new but I'm trying to do my best to improve. I'm trying to set up a GUI that displays a grid of Grass Tiles (using JLabels with Grass icons), but I want to add some other icons on top of the grass (Character icons) so that I can see a character standing on the grass. I thought I could do it by using JLayeredPane and add another JLabel on the same location but with a higher layer priority, however it doesn't seem to work. Any advice on what I should do?

Thanks :)

EDIT: I managed to do this using MigLayout and the setOpaque(false) instruction. Thanks for your answers :)

  • 2
    There are a number of ways you could do this. You could apply a layout manager to the grass labels, so you can add components to them, this way you could use a `GridLayout` simply for displaying the grass tiles or you could make a custom component which paints it's background using the grass image, again, adding a layout manager to it so you can add components. Both have merits, the custom panel is more complicated, but affords you more control of the size of the component, the label is simpler, but will only ever be the size of the icon, not it's content – MadProgrammer Feb 07 '17 at 09:35
  • It's fine if the labels are the size of the icon. If you have time, could you please post a sample code for this? – Giulio Bassetti Feb 07 '17 at 11:05
  • Instead of editing your question with "I solved this by..." please add an answer to your question (including the code in the form of a [mcve]) so anyone who comes to see this question in the future, know how to do it if they come with a similar problem or question. @MadProgrammer didn't post a sample code for this because you posted no code to work with, but here are some [examples](http://stackoverflow.com/questions/19125707/simplest-way-to-set-image-as-jpanel-background) and [here](http://stackoverflow.com/questions/1466240/how-to-set-an-image-as-a-background-for-frame-in-swing-gui-of-java) – Frakcool Feb 07 '17 at 14:55

1 Answers1

0

This is a fragment of the code I used to solve this problem.

    private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 457, 330);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JLayeredPane panel = new JLayeredPane();
    panel.setBounds(0, 0, 457, 330);
    frame.getContentPane().add(panel);
    panel.setLayout(null);

    JPanel background = new JPanel();
    background.setBounds(0, 0, 457, 330);
    panel.add(background);
    background.setLayout(new MigLayout("","",""));
    //In my particular problem I used some constraints, but they're not needed

    backgroundLabels = new JLabel[NCOLS][NROWS];
    for (int i = 0; i < NCOLS; i++)
        for (int j = 0; j < NROWS; j++) {
            backgroundLabels[i][j] = new JLabel(backgroundIcon);
            //Assuming you have a backgroundIcon variable where you saved your icon
            background.add(backgroundLabels[i][j], "cell " + i + " " + j);
        }

    JPanel characterPanel = new JPanel();
    panel.setLayer(characterPanel, 1);
    characterPanel.setBounds(0, 0, 457, 330);
    panel.add(characterPanel);
    characterPanel.setOpaque(false);
    characterPanel.setLayout(new MigLayout("","",""));

    characterLabels = new JLabel[NCOLS][NROWS];
    for (int i = 0; i < NCOLS; i++)
        for (int j = 0; j < NROWS; j++) {
            characterLabels[i][j] = new JLabel("");
            //Creates empty character labels. You can then add icons using setIcon()
            characterPanel.add(characterLabels[i][j], "cell " + i + " " + j);
        }

}
  • `setLayout(null)` and `setBounds()` are bad practices, you're mixing `null-layout` with [layout managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html), please stay with the layout managers only. Read [null layout is evil](http://www.leepoint.net/GUI/layouts/nulllayout.html) and the answers in [this question](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) – Frakcool Feb 07 '17 at 17:07