2

I am writing GUI for klondike solitaire game in Swing for my school assignment. What I am struggling with is this. I can't make cards appear on each other with an offset, like in this picture:

https://i.stack.imgur.com/v6p27.jpg

I have already tried a few Layout managers, like GridBagLayout or OverlayLayout and I can|t get either of them to work. Here's my current code:

Here I create a new JPanel, set it to OverlayLayout, call RenderPack which is shown in the next snippet and attaches to parent JPanel which is GridBagLayout.

JPanel p = new JPanel();
LayoutManager layout = new OverlayLayout(p);
p.setLayout(layout);
RenderPack pack = new RenderPack(stack,p);
pack.drawStack();
this.location.gridy = 1;
this.location.gridx = i;
this.add(p,location);

This is a file where I render each card from the stack (RenderCard.getLabel() just returns label with ImageIcon of a card.) and attach it to parent container.

package ui;

import backend.model.cards.interfaces.ICardStack;

import javax.swing.*;

public class RenderPack
{
    ICardStack stack;
    JPanel parent;

    public RenderPack(ICardStack stack, JPanel parent)
    {
        this.stack = stack;
        this.parent = parent;
    }

    public void drawStack()
    {
        for (int i = this.stack.size() - 1; i >= 0; i--)
        {
            RenderCard card = new RenderCard(this.stack.get(i));
            JLabel label = card.getLabel();
            if (i != 0)
            {
                label.setAlignmentY(-0.2f*i);
            }
            this.parent.add(label);
        }
    }
}

What I am looking for with this question is hint on using OverlayLayout to work how I want it to, or hint on another Layout Manager which would be more suitable. Thanks

camickr
  • 321,443
  • 19
  • 166
  • 288
kosciCZ
  • 21
  • 4
  • 1
    How to overlap your layouts, I think, you can find here: http://stackoverflow.com/questions/852631/java-swing-how-to-show-a-panel-on-top-of-another-panel and a little bit here: http://stackoverflow.com/questions/16555080/overlapping-jpanels-in-the-gui – Vasyl Lyashkevych May 07 '17 at 17:42

0 Answers0