Layers of JPanels? - Game Develloping

Hi all,

I am trying to build a game application. In my game, i want to have two layers : one for the background and one for the foreground where some labels will be moved.

I have successfully built the background as a JPanel added to a JFrame (setContentPane()). But i have some problems about how to place the foreground on top of the background.

I want to have six labels (representing players) in the foreground, which will be moving.

Please help me on how to do the previous things. I would be glad to provide any specific detail you may need.

Thanks in advance,

[605 byte] By [PirateManiaca] at [2007-11-27 10:51:03]
# 1

"How to Use Layered Panes"

http://java.sun.com/docs/books/tutorial/uiswing/components/layeredpane.html

camickra at 2007-7-29 11:28:54 > top of Java-index,Desktop,Core GUI APIs...
# 2

Yes, i am using JLayeredPane....

But for some reason, when i do something like this :

JPanel background = new JPanel();

//ADD COMPONENTS TO BACKGROUND

JPanel foreground = new JPanel();

//ADD COMPONENTS TO FOREGROUND

JLayeredPane pane = new JLayeredPane();

pane.add(background, new Integer(0));

pane.add(foreground, new Integer(1));

i am getting wrong results. First of all the two panels seem to be empty but in fact are builted ok (i have set the background panel as content pane of my frame and everything is fine)...

Secondly, it seems that i am not able to use the pack() method. Everything then disappears except the window title and menu.

Finally when i explicitly specify background and foreground size, the window's menu is disappearing.

Any idea?

I would be grateful if you could give me some details on how to add two panels the one in front of another, without the one of them hides the other

Thanks in advance

PirateManiaca at 2007-7-29 11:28:54 > top of Java-index,Desktop,Core GUI APIs...
# 3

I think the problem lies elsewhere with your code.

Can you post a small, compilable example demonstrating your problem?

c0demonk3ya at 2007-7-29 11:28:54 > top of Java-index,Desktop,Core GUI APIs...
# 4

JPanel background = new JPanel(new BorderLayout());

// set the background of background

JPanel foreground = new JPanel();

// add components to foreground

foreground.setOpaque(false);

background.add(foreground, BorderLayout.CENTER);

dwga at 2007-7-29 11:28:54 > top of Java-index,Desktop,Core GUI APIs...
# 5

The problem is JLayeredPane's behaviour, in fact, layout.

JLayeredPane does not have any layout, so to say, it does not respect its any of the children's size, IF, the size of the child is not explicitly mentioned.

That means:

1. JLayeredPane lp = new JLayeredPane();

2. JPanel front = new JPanel();

3. front.setSize(300,300);//specify size according to your req.

4. front.add(new JLabel("Front"));

5. JPanel back = new JPanel();

6. back.setSize(300,300);//specify size according to your req.

7. back.add(new JLabel("Back"));

8. lp.add(back, 0);

9. lp.add(front, 1);

...add(lp);

:

The above code will properly with line numbers (3 & 6) but wont display anything without those lines.

vijaywadnerea at 2007-7-29 11:28:54 > top of Java-index,Desktop,Core GUI APIs...