Swing and GlassPane for games

Hi all!

I wonder if it is posible to have two/three GlassPanes over eachother for use i games etc.

I have tried this and used two gifs, but every time the panes seems to melt togheter, making the gifs come side by side instead of one over the other.

It looks like they have become the same GlassPane.

The strange thing is tough, that if i add a third layer with the same pic as layer 2, the pic seems to write over the layer 2, just like i want to, but it seems only to work with the same gif pic :S

I am using, SWING, can I do what I want with that, or is there another way thats better and easier?

Can someone pleas help me?

And please go easy on me i am kind of a newbie. :(

CODE:

import java.awt.*;

import javax.swing.*;

import javax.swing.border.*;

import java.net.*;

public class Fonster extends JFrame{

//This test contains 3 layers.

//But the third layer seems to overlapp the second, is this

//becouse the have the same gif?

JPanel pan1 = new JPanel();

JPanel Layer1 = (JPanel) this.getGlassPane();

JPanel Layer2 = (JPanel) this.getGlassPane();

JPanel Layer3 = (JPanel) this.getGlassPane();

JScrollPane GameWin = new JScrollPane(pan1);

Image sand = Toolkit.getDefaultToolkit().getImage("sand.gif");

Image grass = Toolkit.getDefaultToolkit().getImage("grass.gif");

JLabel tile = new JLabel(new ImageIcon(sand));

JLabel tile2 = new JLabel(new ImageIcon(grass));

public Fonster(){

Container c = getContentPane();

c.setLayout(new FlowLayout());

pan1.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));

pan1.setPreferredSize(new Dimension(800, 600));

pan1.setMinimumSize(pan1.getPreferredSize());

pan1.setMaximumSize(pan1.getPreferredSize());

pan1.setAlignmentX(RIGHT_ALIGNMENT);

pan1.setBackground(Color.white);

pan1.setOpaque(true);

Layer1.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));

c.add(GameWin);

pan1.add(Layer1);

Layer1.add(tile);

pan1.add(Layer2);

pan1.add(Layer3);

Layer1.setVisible(true);

Layer2.setVisible(true);

Layer3.setVisible(true);

Layer2.add(tile2);

Layer3.setVisible(true);

Layer3.add(tile);

setSize(800, 600);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public static void main(String[] args){

Fonster c1 = new Fonster();

}

}

:END CODE

[2598 byte] By [SAJBER] at [2007-11-25 20:30:09]
# 1

The glassPane can be replaced with any JPanel and layout you want.

So replace it with your own as follows:

JPanel glassPane = new JPanel();

((JFrame)yourWindow).getRootPane().setGlassPane(glassPane);

glassPane.setLayout(new JLayeredPane());

JLayeredPane respects z-order so that one different components are layered one on top of the other. This allows for images to pass over each other.

Although this is what I think you asked for it is not however a good choice.

Because you want the objects to intersect, collide, and so forth.

There is no simple explaination here except to tell you to go buy a good Java animation and gamming book. Java game programming is far more complex than application programming, and your going to need significant help in learning it.

RobertAlkire at 2007-7-4 17:18:57 > top of Java-index,Desktop,Sun Java Desktop System...