[something here]
The basic idea is that these banners show you a sequence of images -- repeatedly. In this lesson, we'll find out how to do this kind of repeated task (although our graphics won't look quite as nice).
The first step in designing one of these banners is to design the images from which the animation will be built. As an example, let's build a sequence of three images:
Putting these images together to form an animation will take a couple steps -- and then we'll have to figure out how to make it repeat.
So how might we construct our animation? Let's try just drawing
these images one after another.
Exercise
Write a compete applet that draws the three images one after another.
What happens when we run this applet? We just see the last
image. Why? Because the computer is able to display these images
on the screen faster than our eyes are able to perceive them. In
order for this to be an attractive animation, we need to make sure the
computer pauses between displaying each image. Essentially, we will
tell the computer to wait for some number of milliseconds in between each
one. The syntax for doing this, however, is a bit more complicated
than the concept. To tell the computer to wait for, say, 500 milliseconds
(i.e. half a second), we write
try {Yes, this is somewhat confusing, but for the moment it will be enough just to remember that these three lines of text are exactly what is necessary to tell the computer to pause.
sleep(500);
} catch(Exception e) {}
Now we can use these instructions to make the computer pause after drawing
each image.
Exercise
Write a compete applet that draws the three images one after another with appropriate pauses.
How to write an "infinite loop" in Java? Java actually provides a few different ways to write loops (infinite or not); for our simple infinite loop we will use the simplest technique Java offers. This involves a new kind of statement, the while statement (remember that the only other kind of statement we know is the "message-sending" statement).
To refresh our memory, the "message-sending" statement has this general
form:
<object-identifier>.<message-name>(<possible-parameters>);where the items enclosed in <> are filled in with the appropriate names and values.
The while statement has this general form:
while (<repetition-condition>) {It's easy to see where the two kinds of information we need fit into this format: the action to be repeated is given as a sequence of statements; information about repetition is provided as a "condition" enclosed in parentheses. The interesting part of a while statement lies in this "condition". How does it work? In our case, we will use a condition so simple it's almost silly: our loop will start with
<sequence-of-statements>
}
while (0 < 1) {When the computer encounters a while statement, it "evaluates" the expression. In this case, the expression will always be true, no matter what (since 0 is always less than 1). Once it has evaluated the expression, the computer will do one of two things:
Exercises
- Give three different conditions that could be used to construct an infinite while loop.
- Give two different conditions that could not be used to construct an infinite while loop.
- Write a complete Java applet that displays a filled rectangle that flashes from green to red and back, infinitely.