1

I'm trying to create a gui for a hand that contain cards. The cards themselves are instances of a class that extend JPanel.

I need to create a gui that holds up to 7 card objects which stack overlapping on top of eachother, much like you would

hold cards with hand

(ignore the JFrames).

Is it possible to achieve with Swing, and if so could you point me to a proper layoutmanager?

seenukarthi
  • 8,241
  • 10
  • 47
  • 68
13120dde
  • 11
  • 1
  • Create the cards as [Image](http://docs.oracle.com/javase/7/docs/api/java/awt/Image.html)s, keep the cards in a GUI model class, and draw the cards on a JPanel. – Gilbert Le Blanc Apr 01 '16 at 17:41
  • We considered creating the cards as images but decided on making them as objects for several reasons (like changing the various values of the card's elements and to be able to initiate different type of cards by passing in arguments to the constructor, hence a fast way to make new cards). – 13120dde Apr 02 '16 at 12:48

2 Answers2

2

i think in your case you need to use a JLayeredPane. you can add any No of JPanels to fit inside a JLayeredPane

layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(300, 310));
layeredPane.setBorder(BorderFactory.createTitledBorder(
                                    "jlayered example"));
layeredPane.addMouseMotionListener(new MouseMotionAdapter() {
});

for (int i = 0; i < ...number of panels...; i++) {
    JPanel panel = new Panel("panel"+i);
    layeredPane.add(panel, new Integer(i));
}

click https://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html to check how to use JlayeredPane

and if you want the jpanels to overlap on top of each other just set the layout to null layout and add Jpanels inside it.

Priyamal
  • 2,919
  • 2
  • 25
  • 52
  • 1
    Please don't recommend the use of `null` layout, see [Why is it frowned upon to use a null layout in swing](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) and [Null Layout is Evil](http://www.fredosaurus.com/notes-java/GUI/layouts/nulllayout.html) – Frakcool Apr 01 '16 at 18:11
  • 1
    Thanx for the tip, started coding a hand class with layeredPane and it seems to work so far. Got some functionality working such as addCard() and playCard() but I the playCard() method have some issues when I attempt to remove a layer...however more testing need to be done :) – 13120dde Apr 02 '16 at 12:53
1

if so could you point me to a proper layoutmanager?

The JDK doesn't have a layout manager for this.

Check out the Overlap Layout which is a layout designed for this purpose.

It works by controlling the Z-Order of each component added to the panel to allow placement in the 3rd dimension.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • This seems to be just the thing we are looking for, however would it be possible to make the POP_UP show the card in its position but above other cards instead of showing it in reserved space. We are trying to implement such a method with JLayeredPane and mouselistener's mouseEntered() method (thats why a JLayeredPane seems to be a feasible layoutmanager). – 13120dde Apr 02 '16 at 13:03
  • @13120dde, As I mentioned the layering works by playing with the Z-Order of each card. If you want the clicked card on top then you just change its Z-Order position to 0. Then you need to revalidate() and repaint() the parent panel to the component is painted in its new Z-Order. – camickr Apr 02 '16 at 15:40