lab12_programs
from the class download directory. Run the project Test.mcp
in the folder Test
.
These are the two animations that we will work on today.
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
Your code for balloons goes into the file
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, when we have enjoyed the view, let's look at the programming details.
There are three kinds of Sprites in this animation:
Stars are sprites of the class
The moon is a sprite of the class
Now that you are done with the lab exercises, you can create your own animations!
BalloonAnimation.java
is the code for the animation class
for the balloons. In the file creation of all balloons except for one is commented out.
Uncomment it after you get the first balloon to behave correctly.
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 (I recommend you to do it in this order):
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.
drawState(Graphics g)
-- draws the balloon at the given position.
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.
Part 2. Starry night sky
The applet starts off with as a blue sky:
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 which 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
.
SmallStar
. The resetState()
method generates
random x and y coordinate 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.
Moon
. Its coordinates, radius, and the time of appearance
are set by the constructor. It's 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.