views:

315

answers:

1

I'm using the Animator class from the Timing Framework to perform a simple animation (moving a JPanel), roughly based on examples in Filthy Rich Clients

I've overridden timingEvent() as follows

public void timingEvent(float arg0) {

    float fraction = this.animator.getTimingFraction();
    fraction = Math.min(1.0f, fraction);

    if (fraction >= 1.0f) {
        this.mainGUI.initiateSwitchToMainScreenTransition();
    } else if (fraction < 0.5f) {
        this.translateY = (int) (MAX_Y * (2 * fraction));
        repaint();
    }     
}

I've overriden paint() to use the value in translateY to move the panel down.

The animation itself works perfectly.

My problem is the call to initiateSwitchToMainScreenTransition(), which I want to be performed when the animation is complete. Whilst this works, there is a sizeable delay between the animation ending and the call firing (debugging has confirmed that the problem isn't the length of the time taken by initiateSwitchToMainScreenTransition() - the delay occurs before the call).

Is there a better way to achieve this, i.e. without the delay?

A: 

If you are implementing org.jdesktop.animation.timing.TimingTarget then there is an end() method that should be called when your animation is complete - that seems to work for me when I have used it. I do not think you can guarantee that timingEvent will get called after the fraction has reached 1.0.

Your implementation does not seem to do anything for the second half of the timer (when fraction is between 0.5 and 1.0). If you want the animation to continue for the entire duration of the timer you could rearrange your code like this:

@Override
public void timingEvent(float fraction) {
    translateY = (int) (MAX_Y * fraction);
    repaint();
}     

@Override
public void end() {
    mainGUI.initiateSwitchToMainScreenTransition();
}
Russ Hayward
Thanks for that. I actually noticed the end() method last night and switched to using it. However it hasn't got rid of the delay unfortunately. I've set my animation to take 1800ms but it seems that it pretty much completes in 1000ms, resulting in an 800ms delay before my code is fired.
William