im making a java swing game. I heard that swing components don't use active rendering(you can only override paint methods), and for that reason, i have been using BufferStrategy with Canvas. Now i have discover the getGraphics()
method from JComponent and JPanel. If we can do active render in swing components, why game tutorials still override paint()
and paintComponent()
?

- 43
- 12
-
1I would not recommend using the paint() and paintComponent() methods... Generally for games I render an Image either onto a JPanel or directly into the JFrame(depending on the type of game of course..) – TheJavaCoder16 Jan 18 '15 at 21:54
-
2Well, you can't use Swing components other than on the Event Dispatch Thread, so why not use the existing paint mechanism? You're reinventing the wheel otherwise. – markspace Jan 18 '15 at 21:54
-
How do you render an image into a Jpanel without overriding? – Le Ploit Jan 18 '15 at 22:01
-
If i use getGraphics, the returned graphics object is not in the Event Dispatch Thread? – Le Ploit Jan 18 '15 at 22:02
-
2Don't EVER use `getGraphics`, it can return `null` is nothing more the a snap-shot of the last paint cycle. Anything you paint to it will be removed on the next paint cycle. In Swing you DON'T control the paint process and a paint cycle could be initiated for any number of reasons, many of which you don't have control over or would notified of (other then when paint was called) – MadProgrammer Jan 18 '15 at 23:01
-
See also how to properly do rendering to an image outside of `paintComponent`: http://stackoverflow.com/a/21266240/2891664 – Radiodef Jan 18 '15 at 23:40
1 Answers
Don't EVER use getGraphics
, it can return null
and is nothing more the a snap-shot of the last paint cycle.
Anything you paint to it will be removed on the next paint cycle. In Swing you DON'T control the paint process and a paint cycle could be initiated for any number of reasons, many of which you don't have control over or would notified of (other then when paint was called)
The basic answer is, if you want control over the paint process, you MUST use a BufferStrategy
or implement you own off screen drawing routines. There is NO means by which you can achieve a true active painting process within the Swing API, you can fake it to a certain extent, but Swing will still be able to perform it's own painting cycles when it sees fit.
Have a look at Painting in AWT and Swing and Performing Custom Painting for more details about how painting works in Swing

- 343,457
- 22
- 230
- 366
-
But the official oracle API suggests me to do it : public Graphics getGraphics() Returns this component's graphics context, which lets you draw on a component. Use this method to get a Graphics object and then invoke operations on that object to draw on the component. If i have understand, with swing you can emulate active render, but you cant use it like in awt right? – Le Ploit Jan 18 '15 at 23:52
-
@LePloit No, it doesn't work this way, in fact, `getGraphics` is one of those methods that just shouldn't be exposed this way. What it does, is returns the `Graphics` context which was last used to paint the component. Because painting is destructive, the next time that the component is repainted, anything you painted to the `Graphics` context will be lost, which can cause flickering...very annoying. Best bet, DON'T use `getGraphics`, it really doesn't do what you want. In 15 years, I've NEVER had a need to call it. Swing uses a passive rendering approach and you can't change it. – MadProgrammer Jan 19 '15 at 00:00