1

How can i add JPanel with JButtons to Graphics?

I only want to add JButtons, becouse i want to make pause menu.

Window Code:

public JFrame frame;
private BufferedImage image;
private Canvas canvas;
private BufferStrategy bs;
private Graphics g;

public Window(GameContainer gc)
{
    image = new BufferedImage(gc.getWidth(), gc.getHeight(), BufferedImage.TYPE_INT_RGB);
    canvas = new Canvas();
    Dimension s = new Dimension((int)(gc.getWidth() * gc.getScaleX()), (int)(gc.getHeight() * gc.getScaleY()));
    canvas.setPreferredSize(s);
    canvas.setMaximumSize(s);
    canvas.setMinimumSize(s);

    frame = new JFrame(gc.getTitle());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    //frame.setUndecorated(true);
    frame.add(canvas, BorderLayout.CENTER);

    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);

    canvas.createBufferStrategy(2);
    bs = canvas.getBufferStrategy();
    g = bs.getDrawGraphics();
}

public void update()
{
    g.drawImage(image, 0, 0, canvas.getWidth(), canvas.getHeight(), null);
    bs.show();
}

Some player code:

if(player.getPaused())
    {
        JPanel mainPanel = new JPanel(new GridBagLayout());
        JPanel panel = new JPanel(new GridLayout(0, 1));

        JButton button = new JButton("Resume");

        panel.add(button);
        mainPanel.add(panel);
        w.frame.add(mainPanel);
    }

Everything works fine but when i add in other class JPanel with JButtons it doesn't apear.

Without graphics it works.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Norbert
  • 77
  • 6
  • 2
    Please post a [mcve] . – Arnaud Dec 28 '17 at 13:55
  • I’m not sure you can do that. I think you need to add the panel to a surrounding panel or to the window. – Ole V.V. Dec 28 '17 at 14:47
  • If I understand correctly, you want to add some buttons above your current `JFrame`? – Frakcool Dec 28 '17 at 16:33
  • @Frakcool Yes above graphics. – Norbert Dec 28 '17 at 16:38
  • 1
    A [`JLayeredPane`](https://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html) might be what you're looking for... [Here](https://stackoverflow.com/questions/30204194/jlabel-and-jlayeredpane-how-to-display-an-image-over-another-image/30204602#30204602) is an example. I'll try to elaborate an answer in a few hours since atm I'm a little bit busy – Frakcool Dec 28 '17 at 16:39

0 Answers0