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

Animate Transitions Between Tabs #8
Chapter 1, Basic JComponents
|
37
HACK
Another Example
Because TransitionTabbedPane makes it so easy to build new animations, I
thought I’d add another one. This is the old venetian blinds effect, where
vertical bars cover the old screen and uncover the new one; Example 1-19
puts it together.
JTabbedPane tab = new InOutPane( );
tab.addTab("t1",new JButton("Test Button 1"));
tab.addTab("t2",new JButton("Test Button 2"));
frame.getContentPane( ).add(tab);
frame.pack( );
frame.show( );
}
}
Figure 1-23. Two tabs, before transition effect begins
Figure 1-24. Tab transition at mid-point
Example 1-18. Testing out tabbed animation transitions (continued)
38
|
Chapter 1, Basic JComponents
#8 Animate Transitions Between Tabs
HACK
Just like InOutPane, VenetianPane selectively draws the old tab and then cal-
culates the placement of animated rectangles. In this case, there is a blind
rectangle that spans the entire screen from top to bottom, but has the width
of the current step. As a result of the step growing, this rectangle gets bigger
Example 1-19. Creating a venetian blinds effect
public class VenetianPane extends TransitionTabbedPane {
public void paintTransition(Graphics2D g2, int step,
Rectangle size, Image prev) {
int length = getAnimationLength( );
int half = length/2;
// create a blind
Rectangle blind = new Rectangle( );
// calculate the fade out part
if(step >= 0 && step < half) {
// draw the saved version of the old tab component
if(prev != null) {
g2.drawImage(prev,(int)size.getX(),(int)size.getY( ),null);
}
// calculate the growing blind
blind = new Rectangle(
(int)size.getX( ),
(int)size.getY( ),
step,
(int)size.getHeight( ));
}
// calculate the fade in part
if(step >= half && step < length) {
// calculate the shrinking blind
blind = new Rectangle(
(int)size.getX( ),
(int)size.getY( ),
length-step,
(int)size.getHeight( ));
blind.translate(step-half,0);
}
// draw the blinds
for(int i=0; i<size.getWidth( )/half; i++) {
g2.setColor(Color.white);
g2.fill(blind);
blind.translate(half,0);
}
}
}

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