Transparent Game Menu

Heloha,

I would like to have a jPanel, which is transparent, drawn ontop of my game screen(which extends jPanel).
In my jFrame (or Main class), I add the game screen, and then my menu to the container.
However, when i do this, even when i the setOpaque is flagged false on the menu beforehand, it shows that nasty grey colour over the jframe and i cant see my game screen :emo:

Any feedback would be much appreciated… Ive spent the past 5 hours programming different methods and cant seem to get it right… :clue:

Could you show us what you have so far? It sounds like you are adding 2 components to the JFrame at the same time. Remember that a JFrame’s contentPane is preset with a BorderLayout so adding another component to the center will remove the first and add the second.

Avoid using swing/awt gui above your game panel, you can use it on the side if you want. For GUI above your game screen, you need to build them yourself from scratch or use a library design for it.

There’s an excellent explanation of this on StackOverflow: http://stackoverflow.com/questions/2451990/setopaquetrue-false-java

Maybe set the background color of the topmost JPanel to a transparent color?

g.setBackground(new Color(0,0,0,0));

The nice thing about this suggestion is that it only takes about 30 seconds to try it out and reject it if it doesn’t work.

I sort of do something like this when my game pauses. What I do is just stop updating and drawing the game and then draw some text over the game. So the game is frozen and there is text saying something like “Click to resume”. I also sometimes draw an opaque background under the text to make sure it can be read properly. You also wouldn’t have to use a another JPanel this way. Unless you need another JPanel.

If you still want to do this you can try this. Just remember to use an 800x600 image or adjust getPreferredSize to return the correct size for your image.



import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test extends JPanel {
    private Image image;

    public Test() {
        setOpaque(false);
        setLayout(new FlowLayout());
        add(new JButton("TEST BUTTON"));
    }
    public void paintComponent(Graphics g) {
        if(image != null) {
            g.drawImage(image, 0, 0, this);
        }
        super.paintComponent(g);
    }

    public void setImage(Image image) {
        this.image = image;
    }

    public Dimension getPreferredSize() {
        return new Dimension(800, 600);
    }

    public static void main(String args[]) throws IOException {
         Test t = new Test();
         t.setImage(ImageIO.read(new File("C:/Test/800x600_wallpaper_zixpkcom-1.jpg")));
         JFrame f = new JFrame();
         f.setLayout(new BorderLayout());
         f.add(t, BorderLayout.CENTER);
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         f.pack();
         f.setVisible(true);
    }
}

Hi all, and thank you for your replies :slight_smile:

What I’ve done, in resort as I could not solve my problem after trying all your suggestions, is instead use my Graphics2D object and a mouse event listener in combination with a Point variable to draw some boxes and listen for a click on them if the Rectangle drawn contains the Point co-ordinates whilst a ‘ingame’ boolean variable (and other related variables) is/are false.

This also proved as a funky way to animate and make the menu more elaborate :smiley: