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:
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