O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

36
|
Chapter 1, Basic JComponents
#8 Animate Transitions Between Tabs
HACK
InOutPane implements only the paintTransition( ) method, leaving all of the
harder tasks to the parent class. First, it determines how long the animation
will be, and then it calculates an offset to grow and shrink the white rectan-
gle. If the drawing process is currently in the first half of the animation (
step
< half
), then it draws the previous tab below the rectangle, creating the illu-
sion that old tab content is still really on screen with the rectangle growing
above it. For the second half of the animation, it just draws the rectangle,
letting the real tab (the new one) shine through as the rectangle shrinks.
Putting It All Together
Because TransitionTabbedPane is just a JTabbedPane subclass, it can be used
wherever the original would be. Example 1-18 creates a frame with two tabs,
each containing a button. The running program looks like Figure 1-23. As
you switch between the tabs, you will see an animation like that shown in
Figure 1-24.
// draw the saved version of the old tab component
if(prev != null) {
g2.drawImage(prev,(int)size.getX(),(int)size.getY( ),null);
}
offset = (int)((10-state)*scale);
}
// calculate the fade in part
if(state >= half && state < length) {
g2.setColor(Color.white);
offset = (int)((state-10)*scale);
}
// do the drawing
g2.setColor(Color.white);
Rectangle area = new Rectangle((int)(size.getX( )+offset),
(int)(size.getY( )+offset),
(int)(size.getWidth( )-offset*2),
(int)(size.getHeight( )-offset*2));
g2.fill(area);
}
}
Example 1-18. Testing out tabbed animation transitions
public class TabFadeTest {
public static void main(String[] args) {
JFrame frame = new JFrame("Fade Tabs");
Example 1-17. Setting up an animated transition (continued)

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required