I've just started coding video games and I've heard that doing all your drawing on a JPanel and attaching that panel onto a JFrame is better than simply drawing onto the JFrame. I was just wondering why is this better?
It is better for various reasons, including:
paintComponent(Graphics)
method. Top-level Swing containers (e.g. JFrame
, JApplet
, JWindow
) have only paint(Graphics)
. As a result of the common method for painting, people who answer often forget about this difference between common and top-level components, and therefore suggest silly advice. ;)JComponent
can be added to a JFrame
, or a JApplet
, or a JDialog
, or a constraint of a layout in a JPanel
, or a JInternalFrame
, or a.. Just when you think your GUI is complete, the client says "Hey, wouldn't it be great to throw in a tool-bar & offer it as an applet as well?" The JComponent
approach adapts easily to that.JFrame
to be 'exactly the right size' to contain the GUI (using pack()
). That is especially the case when other components are added as well.Now, two minor disagreements with the advice offered.
If the entire component is custom painted, it might be better to simply put a BufferedImage
inside an ImageIcon
/JLabel
combo. Here is an example of painting to an image.
When it comes time to update it:
getGraphics()
for a Graphics
or createGraphics()
for a Graphics2D
repaint()
on the label.It is an easy way to do Double Buffering.
Let me elaborate a bit. In video games, to avoid the flicker caused by redrawing and display smoother graphics, people usually use double buffering. They create an offscreen surface, draw everything on that and then display the entire off-screen surface on the screen in one go.
In Java2D and Swing, the easiest way to do this, is to simply do your game sprite drawing on a JPanel, and then add the JPanel to a JFrame.
Secondly, by drawing things on a JPanel, you allow more GUI widgets and other graphical objects to be displayed on the JFrame without having to paint them manually in the game loop. For example buttons, menus, other panels, even other rendering JPanels.
Thirdly, it allows you to use automatically translated co-ordinates for your game. You can draw everything from the top-left to the bottom-right without having to worry about the window manager specific things like window border-widths, task panes, titles etc.!
Moreover, this is not just a convention only used by Game Programmers in Java. Creating a separate Game Board, Render Panel or Graphics Widget is quite popular when programming games using a library with an internal mainloop such as a GUI Toolkit. You can use a User Form in Windows Forms and a Drawing Board in GTK+.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With