Animation

The goal of this lab is to learn how to do cool things with the Java animation package. To start the lab, please download lab12_programs from the class download directory. Run the project in the folder test. If your browser supports Java, you can click here to see a live demonstration (try it with FireFox or Internet Explorer).

These are the two animations that we will work on today.

Part 1. Balloons

In this animation, balloons go up to the ceiling, hang there for a while, and then go down. The position, the size, the speed of balloons and the amount of time they are stuck to the ceiling varies.

The file BalloonAnimation.java contains the code for the animation class for the balloons. In the file, creation of all balloons except for one is commented out. Uncomment the other balloons after you get the first one to behave correctly.

Your code for balloons goes into the file Balloon.java. The constructor and some instance variables are already defined, do not change the constructor! You need to write the code for the following methods (write them in this order):

  1. resetState() -- sets the balloon to the initial state, i.e. on the floor. You can use variables height and width, which Balloon inherits from Sprite. They refer to the height and the width of the screen.
  2. drawState(Graphics g) -- draws the balloon at the given position.
  3. updateState() -- controls the movements of the balloon. The balloon should go up (incrementing its height by d on every frame) until it reaches the ceiling, then it hangs at the ceiling for the number of frames given in timeStuck (how do you count the time?), and then goes down, decrementing the height by d, until it reaches the floor.
Make sure to test the reset button: reset the animation, and then play it again. It should work exactly as the first time.

Part 2. Starry night sky

The applet starts off as a blue sky:

The sky slowly gets darker and a few stars appear:

In the end (frame 100) the sky is dark, filled with stars, and the moon appears in the left upper corner:

Now, let's take a look at the programming details.

There are three kinds of Sprites in this animation: NightBackground, SmallStar, and Moon. Your task is to fill in missing parts of the code for these three classes and add them to the animation in the file StarryNightAnimation using the addSprite() method.

NightBackground is a sprite that inherits from ColorBackground. The constructor for NightBackground sets the initial color of the background to blue. You need to make the color darker (by invoking the method darker() ). Making the background darker on every frame would turn it black very soon, so we introduce a delay: the color stays the same for the number of frames specified by the the parameter delay. Your task is to fill in the resetState() and the updateState() methods of NightBackground.

Stars are sprites of the class SmallStar. The resetState() method generates random x- and y-coordinates within the applet's screen, the radius (a number between 1 and 5), and the time the star will appear on the sky (a number between 1 and 100). Your task is finish the method resetState() and write the methods drawState() and updateState(). Hint: the star should be drawn only after it has become visible.

The moon is a sprite of the class Moon. Its coordinates, radius and the time of appearance are set by the constructor. It is drawn as a yellow vertical half-circle. Use the same trick as you used for stars to make the moon appear at the given time.

You want a more astronomically correct animation? O.K....

You may have noticed that stars show through the dark half of the moon (hey, that's not right!). The stars that we'd like to get rid of are indicated with red arrows in the picture above. Fix this problem by adding the other half of the moon drawn in the background color. You need to pass the delay as a parameter to the moon constructor, since you need to figure out the background color at the time when the moon appears.

Now that you are done with the lab exercises, you can create your own animations!