0

I am writing a Java 2D video game. I am using only the Java 2D api, and all updates are driven off a single update timer. I perform all the drawing in a JPanel utilizing paintComponent(), and I use Volatile Images for all of the graphics images from what I have read should be a performance increase.

In spite of all this, at times my video game starts flickering like crazy. The whole screen starts flashing. The game is written in Java 6, and I am running on Mac OS X 10.10.1.

Any ideas on how to fix this?

Thanks.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
user1104028
  • 381
  • 7
  • 18
  • 1
    How frequently are you calling your paint method? Are you using `repaint()` to repaint your panel? Or are you explicitly calling `paintComponent()`? – initramfs Jan 02 '15 at 16:33
  • Can you provide more detail about what you're doing? Why JPanel over JComponent (affects opacity)? Can you repaint regions instead of entire component? Can you do movement via XOR draws to eliminate repaints? etc. – Jeff Jan 02 '15 at 16:54
  • Volatile Images can disappear at anytime, this could cause flickering the image becomes invalidated for some reason. Consider using a compatible BufferedImage instead. I could be wrong but I thought the use of volatile image was less required now with the change to overall rendering pipeline – MadProgrammer Jan 02 '15 at 20:38

2 Answers2

0

According this post you shouldn't call paintComponent() directly. Try calling paint() instead.

Edit: Sorry, I confused paintComponent() and paintComponents(). Maybe you could show some code?

Community
  • 1
  • 1
jlink
  • 682
  • 7
  • 24
  • Also make sure that you do not call `paint()` too often. Only call it when something has changed on the screen and you need to update the painted canvas. By calling `paintComponent()` directly or overly re-painting, you will most likely have some flickering. – NoseKnowsAll Jan 02 '15 at 16:27
  • OP is using JPanel which is a Swing component, thus **should** override `paintComponent()`. The post says don't override `paintComponents()` which is a different method to `paintComponent()`. – initramfs Jan 02 '15 at 16:31
  • From what I've read, thought should call paintComponent() because I've updated a JPanel and supposed to only call paint() if you were updating a Canvas? I'll give it a whirl and see what happens. – user1104028 Jan 02 '15 at 16:33
  • You should NEVER call a paint method directly, instead you should wait until the system calls it... – MadProgrammer Jan 02 '15 at 20:30
0

Call setDoubleBuffered(true) on your main window/frame/panel. Or draw to another component and switch when ready.

The flickering occurs because you quickly redraw on the same component. Redrawing in the background, and then quickly switch to the now newly drawn picture gets rid of this. This is called double buffering. Read more here.

Matsemann
  • 21,083
  • 19
  • 56
  • 89
  • 3
    Isn't double buffering enabled by default on Swing components? – Jeff Jan 02 '15 at 16:52
  • According the doc, yes: " You may have already noticed that Swing uses this technique in many of its components, usually enabled by default, using the setDoubleBuffered method." – jlink Jan 02 '15 at 16:53