Skip to main content

[JAVA2D] Flickering Animation

1 reply [Last post]
Anonymous

I've written a program that generates images in real time, which if
viewed in sequence would form an animation. These images are built one
at a time in a 800x600 offscreen VolatileImage buffer (I'm doing my own
double buffereing). I then call repaint() and blit this buffer to a
JComponent that I've subclassed to display my animation. Painting code
is in the paintComponent() method. I'm repainting anywhere from 10 to
30 frames per second. This is handled in a second thread that sleeps
for the appropriate number of milliseconds, then wakes up, causes the
next image to be generated, and calls repaint() on my component's JFrame
ancestor.

Even though my machine is fast enough to generate and blit in the time
provided, the animation still apprears jagged. There is occasional
horizontal shearing, presumably from the blitted image being only
partially rendered when the screen refresh occurs. The image also
appears to shake, giving me the impression that Swing is somehow caching
previous copies of frames rendered and mixing them into the curent
frames rendered. The whole thing reminds me of the old days when you
had to override update() to stop AWT from clearing the screen.

I've tried this both single buffered and double buffered JRootPanes
(since I'm doing my own double buffereing, I figure I don't need to use
Swing's).

Any ideas what's going wrong? Is my second thread causing the trouble?
What's the recommend way to get smooth animation?

Mark McKay
--
http://www.kitfox.com

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Chet Haase

Mark,

I'm not sure about some of the issues you raise (such as the "shake"
effect you are seeing), but the simple Blt's of images to the
screen can trigger tearing artifacts due to not being sync'd
the the vertical retrace of the screen.

The best way to get smooth (non-tearing) animation is to use
a fullscreen flipping buffer instead of doing drawImage()
from a buffering image. This is the way that games get
smooth animation. Flipping is automatically sync'd to
the vertical refresh of the monitor (and is thus pegged
to the refresh rate of the monitor, or even subdivisions
of that number). The buffering operation is faster
because it only involves a pointer switch (back <-> front)
instead of a copy of all the pixels.

Anyway, take a look at the BufferStrategy APIs. You will
need to be in fullscreen mode on Windows in order to
take advantage of buffer flipping. Take a look at the
fullscreen tutorial (a bit dated, but still essentially
correct) for more information on this:
http://java.sun.com/docs/books/tutorial/extra/fullscreen/

Chet.

Mark McKay wrote:

> I've written a program that generates images in real time, which if
> viewed in sequence would form an animation. These images are built one
> at a time in a 800x600 offscreen VolatileImage buffer (I'm doing my own
> double buffereing). I then call repaint() and blit this buffer to a
> JComponent that I've subclassed to display my animation. Painting code
> is in the paintComponent() method. I'm repainting anywhere from 10 to
> 30 frames per second. This is handled in a second thread that sleeps
> for the appropriate number of milliseconds, then wakes up, causes the
> next image to be generated, and calls repaint() on my component's JFrame
> ancestor.
>
> Even though my machine is fast enough to generate and blit in the time
> provided, the animation still apprears jagged. There is occasional
> horizontal shearing, presumably from the blitted image being only
> partially rendered when the screen refresh occurs. The image also
> appears to shake, giving me the impression that Swing is somehow caching
> previous copies of frames rendered and mixing them into the curent
> frames rendered. The whole thing reminds me of the old days when you
> had to override update() to stop AWT from clearing the screen.
>
> I've tried this both single buffered and double buffered JRootPanes
> (since I'm doing my own double buffereing, I figure I don't need to use
> Swing's).
>
> Any ideas what's going wrong? Is my second thread causing the trouble?
> What's the recommend way to get smooth animation?
>
> Mark McKay
> --
> http://www.kitfox.com
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".