Virtual Core 5

Java Tutorial

I.3.S


Introduction

In the first two lessons, we developed a number of simple applets which had essentially the same structure:  they involved sending a sequence of messages to an object in the Graphics class.  We could continue to explore the capabilities of the Graphics class -- it offers a variety of sophisticated drawing operations -- but our goal in this course is to get a broad picture of what computer programming (particularly using Java) looks like.  Let's consider some other aspects of what computers offer us:
  Over the next several lessons we will introduce the concepts and Java syntax that are necessary to utilize these kinds of computing power.

Repetition:  Advertising Banners

If you've spent much time surfing the web, you've certainly seen "animated" advertising banners that look something like this:

[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:

  1. The word "BUY", in white letters, in the center of a black filled rectangle,
  2. The word "STUFF", in white letters, in the center of a black filled rectangle,
  3. The word "HERE", in red letters, in the center of a green filled rectangle.
  4. Exercise

    Write three complete applets to display each of these images.  Remember that for the animation we're designing to look good, the rectangles and text will have to be in the same place on the screen.

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 {
    sleep(500);
} catch(Exception e) {}
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.

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.

Adding Repetition with the while Statement

In the previous exercise, you wrote an applet that displayed our "animation" one time.  Now we have to find out how to display the animation repeatedly.  When we want to have the computer repeat some action we need to give it two kinds of information:
  1. A description of the action to be repeated,
  2. How long (or how many times) to repeat the action
The first information we can easily provide:  it's essentially the "body" of the applet you wrote in the previous exercise.  The second kind of information (about how many repetitions to perform) can in general be very complex, but in our case it's simple:  we want to repeat "forever" (or until the user leaves our web page).  This kind of repetition is called an infinite loop.

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>) {
    <sequence-of-statements>
}
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
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: Note carefully that, since 0 is always less than 1, our expression will always be true, so the sequence of statements will be executed forever!
 

Exercises

  1. Give three different conditions that could be used to construct an infinite while loop.
  2. Give two different conditions that could not be used to construct an infinite while loop.
  3. Write a complete Java applet that displays a filled rectangle that flashes from green to red and back, infinitely.