1

I've been trying to implement a game using a BufferStrategy within a Canvas in a JFrame, and I just can't seem to get my animated .gif images (implemented as new ImageIcon().getImage()) to play without flickering.

I think the problem lies with the .gif refresh rate and the rate at which the canvas is being redrawn, but this is only a guess. I've been trying various things for hours and searching all over the internet for some solution, but one has yet to present itself. Any help would be greatly appreciated.

On a side note, the .gif works fine. It doesn't flicker in image viewing programs like XnView, and it also played fine as a JLabel within a super simple JFrame program.

joey rohan
  • 3,505
  • 5
  • 33
  • 70
  • Increase your heap space, may help. – joey rohan Feb 02 '13 at 14:14
  • 4
    1) Don't mix AWT with Swing. 2) Use a JLabel. 3) Why do you want to use a `Canvas` if it works properly with a `JLabel`? Why do you want to change something that works? – Guillaume Polet Feb 02 '13 at 14:31
  • +1 to Guillaume, once i did the same thing, and the flickering went away when i adjusted my heap space, but the actual reason was mixing AWT with Swing. – joey rohan Feb 02 '13 at 14:51
  • I tried removing all Swing components making everything Frame and Canvas. I tried increasing my heap space to 1gb and it still flickered. – Collin Smith Feb 02 '13 at 19:53
  • The reason why I am not using `JLabel` is because I wanted to try and directly paint the image on the `Canvas` myself. – Collin Smith Feb 02 '13 at 20:11
  • I think I found where the problem is. I made a simple program using only a `JFrame` a `JPanel` and a `JLabel`. That worked fine, so I added in a `BufferedImage` background to my `JPanel` and it still worked. The last thing I tried changing was removing my `JLabel` and having `JPanel.paintComponent()` directly draw my animated `.gif` which reproduced the flickering effect. I want to see if I can try and restrict repaints of the `.gif` to happen only when the animation is finished, but I also need to somehow keep what I've drawn on top of my background (recreate what I had with the JLabel). – Collin Smith Feb 02 '13 at 20:38
  • 1
    See [Show an animated BG in Swing](http://stackoverflow.com/questions/10836832/show-an-animated-bg-in-swing) for tips. Abandon AWT. You won't get good help with it since most people have forgotten the details. – Andrew Thompson Feb 03 '13 at 00:59

0 Answers0