![]() |
Lab 13
|
lab13_programs
from the class download directory. If your browser supports
Java (which should be the case with public computers here),
click here to see a live
demonstration.
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.
Your code for the
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, let's take a 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!
Balloon
class goes into the file Balloon.java
.
Several of the instance variables are already given. Let's think and decide whether
we should add any more!
You need to write the code for the
following methods (write them in this order):
Balloon()
-- the constructor. It assigns values to the instance variables
(as usual).
resetState()
-- sets the balloon to the initial
state, i.e. touching 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.
BalloonAnimation.java
contains the code for the
animation class for the balloons. In the file, code for creation of one is given.
Add more balloons after you get the first one to behave correctly.
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 screen starts off as a blue sky:
NightBackground
, SmallStar
, and
Moon
. Your task is to fill in missing parts of the code
for the first two classes, and create the third (Moon) from scratch.
As you create each class, add instance of it 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 instance method
darker()
, on the Color
object). 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-coordinates
within the given 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 in 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!). 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.
Wonderful student-created animations