Sign in to follow this  
Concentrate

Smooth Animation[ Java ]

Recommended Posts

Concentrate    181
Hello,

I have a list of JPanels, which are used to represent frames. What would be a good procedures to play the list of frames, 1-by-1 with reducing flickering as much as possible. Currently right now, I'm just updating the contentPanel by removing the frame and then adding in the new frame. But this is giving me flickering.

Share this post


Link to post
Share on other sites
boogyman19946    1487
How are you replacing those frames?

I'm not particularly familiar with Java's way of rendering, but it sounds like you are replacing the frame and drawing to it after it was replaced. This will give you flickering because you're not employing a double buffer, you just have two surfaces. In order to prevent flicker, the image must be drawn component by component off screen onto a buffer which later replaces the image on screen, therefore preventing overdraw (the cause of the flickering).

Now, if the machine isn't fast enough to draw the back buffer onto screen fast enough, then obviously you're going to have flickering because the image simply isn't drawn fast enough. Try reducing the quality of what is drawn .

Share this post


Link to post
Share on other sites
Concentrate    181
Maybe some code:

public class AnimationRunner extends javax.swing.JFrame implements java.awt.event.ActionListener{
private JPanel _content;
private JFrame _window;
private JSlider _speedController;
private int _startingFrame;
AnimationTimer _timer;

public AnimationRunner(){
super("Animation");
initialize();
setContent();
endSetup();
}
public void actionPerformed(java.awt.event.ActionEvent e){
_startingFrame = (_startingFrame + 1) % AnimationMainController.frameController.size();
AnimationFrameCanvas f = AnimationMainController.frameController.frame(_startingFrame);
f.setVisible(true);
_content.removeAll();
_content.add(f);
_content.add(_speedController,java.awt.BorderLayout.SOUTH);
_window.setContentPane(_content);
}

}



so right not its simply just updating the content panel by adding in a new frame.

I'm not sure how to exactly use double buffering with what I have.

So when one creates a new AnimationRunner(), it pops out a window and creates the flickering animation. I think the flickering is due to replacing the whole frame
in the contentPanel with another frame. How can I solve this? Thank you.

Share this post


Link to post
Share on other sites
JoeCooper    350
Is there a reason you're doing animation this way, rather than overriding the paint(Graphics) method of a JComponent?

If you do that, you can use javax.swing.Timer to call the repaint() method repeatedly, which will indirectly call the paint(...) method.

If you use System.currentTimeMillis(), you can fetch the start time of the animation, and every time paint(...) is called, check the time elapsed to determine which frame should be on screen during a given call.

Swing uses double buffering by default to reduce tearing & flickering. Without getting into details, just paint to the Graphics object passed to paint(...) and it will actually go to an off-screen buffer.

Lastly, I don't recommend removing & adding components like that, but for future reference, if you're ever in a ridiculous, shall we say, out-of-spec situation, you can force a repaint by using a component's getGraphics() method to fetch a Graphics object which you can then pass to paint(...).

Share this post


Link to post
Share on other sites
JoeCooper    350
One more thing. The nature of your (OP) post gives me the impression you're new to Java animation, so, I'm gonna add this here link to my page. The "spinner box" on the top, with source, is a very simple program that features a custom JComponent that does animation. Dig through there and have a glance at Spinner.java.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this