Chapter 4

!

Visual Design





In Chapter 3 you saw how easy it is to use the widget classes in the java.awt package to build interesting looking (if not particularly functional) applets. You can now create and customize your own buttons, text containers, choice boxes, labels, and lists. You don't though, have any means of grouping widgets together into a single complex component. Neither do you yet have a good way to control their placement on the screen. In this lab you'll see how to address both of these problems.
Java provides us with a variety of container types which can be used to hold and group widgets, as well as a few layout managers which help us control how the components and containers in our applet display on the screen. Once you see how these containers and layout managers operate, you'll be able to describe arbitrarily complicated interfaces in a consistent and platform-independent way.


Lab Objectives

In this lab, you will:
 o

Run and experiment with this chapter's lablet, Ovenator.

 o

Practice using Java containers to arrange the widgets that make up Ovenator.

 o

See the effects of using different layout managers on the appearance of this applet.

 o

Extend the Ovenator interface to add and position containers of your own.

Exercises

  1. We'll start this lab as we've started the others, by running the lablet to see how it performs in its original state.

    1. Compile and run Ovenator now. It should look pretty much like Figure 4.6 [p. 139 ] in the text.

    2. Inspect the lablet's source code and identify the containers that were used to produce the microwave's interface. Below, give the names of each of the five containers and the layout managers each uses.
      Answer:


  2. Let's edit the source code to introduce some errors--or at least changes--in how Ovenator uses its containers and layout managers. As we've done in past labs, make each of these changes to the original lablet. Then, compile and run the edited version. In each case, explain the error that you've caused or the difference in behavior that results from the change.

    1. Change the line in the init() function that reads
      add("East", myControls);
      to read
      add(myControls);
      Result:


    2. Remove (or "comment out") from the ControlPanel constructor the line that reads
      setLayout(new GridLayout(4, 1);
      Result:


    3. Change the line in the TimeButtonsPanel constructor that reads
      setLayout(new GridLayout(1, 4);
      to read
      setLayout(new GridLayout(2, 2);
      Result:


    4. Remove or comment out the line in the CookButtonsPanel constructor that reads
      bMicro = new Button("Micro");
      Result:


    5. Remove or comment out the line in the CookButtonsPanel constructor that reads
      add(bTimer);
      Result:



  3. Now let's make some changes to Ovenator that will affect the applet's appearance in positive ways (that is, without introducing errors).

    1. First, notice how by grouping widgets into containers (the auxiliary Panel classes in our lablet), we can easily control the properties--like the color--of the containers as a whole. Change the time buttons from yellow on black to an arresting (and quite ugly) cyan on magenta, using a 10-point italic font. Compile and run your applet. Feh! Quick! Change it back to its original state!

    2. Since you're almost certainly reading this in English, you'd probably agree that the "Start" button should be to the left of the "Stop" button. Change the lablet to make it so.

    3. Despite its sleek and stylish appearance, Ovenator is missing something important--a way to open the door. On many microwave ovens, this is accomplished by means of a button that, when pushed, releases the door. Add a button to the control panel that would, in our virtual world, open the oven's door.


  4. It is time to make some more significant changes to Ovenator. Each of the following extensions will require you to add new components to tha applet and position them on the screen. You can either add new components to Ovenator's existing panels, or you can define new panels and add them to the applet directly. Feel free to rearrange any of the existing panels to accomodate your extensions.

    1. Add four more "cooking" buttons to turn your oven into a fancy combination convection/microwave oven. Name the buttons "Warm", "Bake", "Broil", and "Incinerate".

    2. Replace the four time buttons on the TimeButtonsPanel with a keypad that looks like this:

    3. Now change the keypad so that it looks like this:
      This is a bit tricky. You'll have to figure out how to get the blank cell immediately to the left of the "0" button.

      Note: In some environments, this modification causes significant changes to the look of the applet. If this happens to you, make sure you can explain exactly what causes the control panel and the canvas to behave as they do.


Postlab Exercises

  1. Write an applet that displays a graphing calculator. You can use a real calculator as a model, or invent one of your own.

  2. Do the same for an elevator control panel.

  3. Go back to the Gigobite lablet of Chapter 3 and lay out its widgets so that they keep their orientations when the applet is resized. For example, since the three labels associated with the sandwiches, sides, and drinks lists should always be to the immediate left of the lists, you should invent a LabeledList class that groups a label and a list together in one panel.

Last updated: December 4, 1998
Rick Decker
Department of Computer Science
Hamilton College
Clinton, NY 13323