I would like to use the Java Trident library to animate screen transitions.
I have a series of panels arranged in a CardLayout. When a JButton is clicked on one panel, a new panel is shown. I would like to implement some sort of fade-in animation instead of having it just appear instantly.
I have read that slide-in animations are possible just by moving panels off-screen and changing the x-coordinates when I want to transition.
I’ve tried setting something up, but I know I’m not doing it correctly. I need to call cl.show() to actually show the panel I want:
CardLayout cl = (CardLayout)(GameContainer.getLayout());
screenChangeTimeline = new Timeline(cl);
screenChangeTimeline.addPropertyToInterpolate("layout", "MainMenuPanel", "TwoPlayerPanel");
screenChangeTimeline.setDuration(1500);
GameContainer is the panel that has the CardLayout and all other panels are located inside it.
It maybe easier to create animation effect using images, not the actual sliding of panels. Capture images of the panel you’re hiding and the panel you are about to show. Animate these images using a sliding effect. Once the animation if finished, swap the actual panels. Here is a sample that demonstrates similar idea. It uses an auxiliary card to scroll an image combined from two panels.
You may be interested in the ready solution – Animatingcardlayout. It provides an extension to CardLayout and a set of various animations.
Also, check out a series of articles by Kirill Grouchnikov (creator of Trident) – Animating layouts.