Swing GUI Question

Hello,

I am programming a game and using Swing as my GUI. I did some changes, so I can have a custom GUI. This is what I have done:

I have made the game map a JLabel on a JFrame. Then I have overridden the JLabel so that I can have movable windows, placed on top of this game map(that is a JLabel). This works fine, and all is well.

The problem is that I have to call the repaint of the game map 10 times a second (More on this later). What occurs is a repaint to all of the game label’s children are done. So, my GUI gets repainted 10 times a second. This causes a great performance decrease and I am not too pleased with it.

Does anyone know how to only repaint what is on the game label and not the children (yet still make sure they render correctly)? I know there is a way to ignore repaints, but I believe that is only for hardware calls, and not software.

The performance decrease I speak of is my dual core CPU runs at 20-25% during repainting. I have done some modifications, so that if I do not need to draw (fast moving projectiles, these draw at 100 MS), I won’t draw (there are no projectiles to draw). Character movement is done at 250 MS, so if I do not need to draw 10 times a second, I will draw at least 4 times. This keeps the CPU down to 8-12%, which is more acceptable.

From my investigations, it seems I am out of luck.

Thanks for any advice/help!

PS: I wanted to try this forum first before going to Sun’s.

If you ignore repaints, you can use something called “active rendering”. This means that you can paint whatever you want whenever you want. I believe the full screen exclusive mode trail in the Java Tutorial contains information on this, but I could be wrong. In any case, you should be able to find information about it on this forum and in many tutorials.

In most implementations, you will have a BufferStrategy that switches between two buffers. That will complicate things a little bit because you’ll have to remember to redraw the children of the JLabel not only when they’re updated but also in the next frame.

I have to ask, why not just make the children of the JLabel not be children of the JLabel? That seems to be the most straightforward solution. Override the JFrame (or some child component) so that the movable windows can be moved within the JFrame, but limit the area they can move in to over the JLabel.

Hi Dayrinni,

Have a look at the source for this game I made:
http://www.java-gaming.org/forums/index.php?topic=18019.0

This is how it works, ignoring details:
The game is rendered in its own thread (active rendering) and it is done in the render method of ViewPane.
ViewPane is added to a JDesktopPane (I actually sub-class it and its called GameDesktopPane), and the JDesktopPane is the JFrame’s contentPane.
When viewPane.render() is called in the game loop, the render method checks whether any components are added to the JDesktopPane. If there aren’t any, it just renders the game.
If there are, it means a JInternalFrame has been added and is floating around and needs to be painted. Since swing can’t be painted from your own thread, it can only be painted from the Event Dispatch Thread (EDT), I call SwingUtilities.invokeAndWait(new Runnable(public void run(){ jInternalFrameAddedToJDesktopPane.paintAll(graphicsOfGame); }););
This paints the swing menus in the EDT (avoiding thread problems), on top of the game graphics, which is what you want.
Of course rendering is slowed when you have to paint a menu, but you don’t need to have menus always visible.
To have a ‘menu’ button always visible, as well as a JTextField to use for chatting, I actually put the ViewPane inside a JPanel, and in the bottom of the JPanel I put the menu button and the textField. Since they don’t over-lap, the buttons and textfield are not rendered in the game, so performance of the game is not affected.

Anyway, have a look at the source to see the details. It’s kind of tricky, and there’s no tutorial that sun provide or anyone provides for doing this. Swing is not meant to be actively rendered. When I have a bit of time I’ll write up a proper example of how to do this properly.

Good luck :slight_smile:
Keith

Thanks for both of your replies. I will investigate this and then get back to ya!