# Rendering another screen on top of main game screen in fullscreen mode

my game runs in fullscreen mode and uses active rendering. The graphics are drawn on the fullscreen window in each game loop:

    public void render() {
Window w = screen.getFullScreenWindow();
Graphics2D g = screen.getGraphics();
renderer.render(g, level, w.getWidth(), w.getHeight());
g.dispose();
screen.update();
}


This is the screen.update() method:

public void update(){
Window w = device.getFullScreenWindow();
if(w != null){
BufferStrategy s = w.getBufferStrategy();
if(!s.contentsLost()){
s.show();
}
}
}


I want to display another screen on my main game screen (menu, inventory etc). Lets say I have a JPanel inventory, which has a grid of inventory cells (manually drawn) and some Swing components like JPopupMenu. So i tried adding that to my window and repainting it in the game loop, which worked okay most of the time... but sometimes the panel wouldn't get displayed. Blindly moving things around in the inventory worked, but it just didn't display. When i alt-tabbed out and back again, it displayed properly.

I also tried drawing the rest of the inventory on my full screen window and using a JPanel to display only the buttons and popupmenus. The inventory displayed properly, but the Swing components keep flickering. I'm guessing this is because I don't know how to combine active and passive rendering.

    public void render() {
Graphics2D g = screen.getGraphics();
invManager.render(g);
g.dispose();
screen.update();
invPanel.repaint();
}


Should i use something else instead of a JPanel? I don't really need active rendering for these screens, but I don't understand why they sometimes just don't display. Or maybe I should just make my own custom components instead of using Swing? I also read somewhere that using multiple panels/frames in a game is bad practice so should I draw everything on one window/frame/panel?

If I CAN use JPanels for this, should I add and remove them every time the inventory is toggled? Or just change their visibility?

If you use lightweight components, such as Swing components, you may have to fiddle with them a bit so that they draw using your Graphics, and not directly as a result of calling the paint method. Feel free to call Swing methods such as paintComponents, paintComponent, paintBorder, and paintChildren directly from your rendering loop.

Also check if some other Root pane besides Content pane would fit better for your purposes: How to Use Root Panes.

Instead of using another panel, I would use the same game container. I would load a semi transparent image and keep it in center. Then, I would draw the inventory etc., on the transparent image so that the game's previous screen is visible at the background.

## Rendering Swing Components

Suppose you have a JPanel and you wan't to render it. Set the panel to be double buffered and don't add it to the container.

Instead, every time you draw, translate the graphics and call the paint() method of the component.

Don't forget to resize the background image to the container size. Now you can set the layout of the panel and add your other swing components directly to it.

• I think this answer may just introduce more questions. – MichaelHouse Sep 11 '12 at 17:05
• Okay, what about the Swing components? As i understand you can't render them actively so how should i do that? So far i just get flickering whatever i try... – delos Sep 11 '12 at 23:23
• How to set the background image is here robbamforth.wordpress.com/2009/02/02/… – Sri Harsha Chilakapati Sep 13 '12 at 1:59