Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Add spice to your splash screen with animation

Create a dynamic splash screen in Java

While developing a new feature for a product, I decided to create a splash screen that illustrated the metaphor of pieces falling into place. A little animation seemed the perfect trick for the job. To spice up your own splash screen, you can use the code presented in this article. Check Resources for the complete source.

Figure 1 illustrates the idea for the animation, using my company's slogan as the example. To create the desired effect, we want to chop up the slogan into its individual characters. These characters will fly in from all sides of the splash screen, ultimately falling into place to create the slogan. To keep the view realistic, the characters' trajectories should replicate something like a cannonball shot from a cannon. So the code needs to incorporate some basic physics—don't worry, the math is really simple.

Figure 1. Each character flies in from three sides

Figure 2 shows the final result after the animation completes. We'll also use a gradient paint for the background to add a little texture.

Figure 2. Animated splash screen

Calculate the trajectory

First things first—we need to decide how the characters, or sprites, will move about the screen. Animators use the term sprite to describe a small moving bitmap. We're going to randomly assign a starting position for each sprite along the top three sides of the view—left, top, right—in traditional x, y coordinates (i.e, not screen coordinates). We assign a final position based on the location of the sprite in the slogan text. The essence of our problem is this: we need to calculate an initial velocity for both the x and y directions for each sprite such that each sprite lands in the proper position after the allotted simulation time. Since we want a realistic trajectory, we'll want to include a "gravity field" in the y direction that pulls each sprite towards its final destination. Then we can launch our "cannonball" sprites toward their final targets.

The first equation we need comes from basic physics:

Equation 1 says that our sprite's final position is equal to velocity multiplied by time, plus the initial position. Since there isn't a gravity field along the horizontal axis, the velocity for each sprite along the x direction will be constant. Each sprite will start at a different x position, so the distance each sprite needs to cover will differ. Therefore, each sprite's velocity—though constant—will differ from the velocity of the other sprites.

The y direction is trickier since we want to include a gravity field, which will pull the cannonball (sprite) back towards ground (the final sprite position). In this case, velocity isn't going to be constant, so we need another equation from Physics 101:

Equation 2 says that our sprite's velocity equals acceleration multiplied by time, plus the sprite's initial velocity. Like gravity, our acceleration will be constant. So, we can plug Equation 2 into Equation 1 for the y direction and get:

Write the application

Now that we've figured out the basic physics, we're ready to start writing some code. We start by creating a new class, AnimationBanner, to serve as the entry point. Let's use a few static fields to capture the fundamental parameters for the simulation:

1 | 2 | 3 | 4 |  Next >

Discuss

Start a new discussion or jump into one of the threads below:

Subject Replies Last post
. Add spice to your splash screen
By JavaWorldAdministrator
0 05/07/08 05:18 AM
by Anonymous
. New feature in Mustang (Java 6)
By Tim Yates
0 04/22/08 06:02 AM
by Anonymous
. Letters falling "through"
By SomeTester
0 04/21/08 06:37 AM
by Anonymous
. Incorrect physics
By Anonymous
0 04/05/08 06:04 PM
by Anonymous
. Not so random
By AnotherTester
0 04/02/08 09:25 AM
by Anonymous


Resources