Lecture 13

Agenda

Announcements

Last Word on Interfaces

On Friday, we discussed Java interfaces, and hopefully everyone agrees that they can be a very useful mechanism to help write reusable and easily-extensible software.

In general, it is a good idea to declare your variables using the most general type possible. In the LaundrySorter example, there were only two types of Laundry and we already saw a benefit. The more general you can make your programs, the more likely it is you can reuse them and extend them to new situations. Remember, you can declare your variables of the interface type (e.g., Laundry) and only refer to the actual class types (e.g., Tshirt, Pants) when you construct them.

We will see more examples soon.

Last Word on Debugging

Keep the debugging tips in mind as you work on Frogger and the upcoming test program. Develop and test your programs incrementally, so you always know what you've changed recently. Once you notice a bug, you want to narrow down the possible causes, and one helpful tool for that is System.out.println to dump debugging output to your Java console window.

AWT

As you discovered in the BoxBall lab, drawing even simple graphic controls can be painful and tedious. How many times did you type

private final static double .... = ...;
while writing that program to set up constants describing the locations of the buttons, the dimensions of the buttons and the place where the text in the buttons belonged?

Now, imagine how much worse it could be if you wanted to include a more sophisticated interface for BoxBall. For example:

  1. We might just want fancier buttons (buttons with shading and the ability to blink when you click them).

    Demo: Boxball using Buttons

    This is a lot like our original program except that the buttons are fancier and they do things like change color when you press them.

  2. Another possibility would be to replace the buttons with a drop-down menu.

    Demo: Boxball using a Choice/Menu

    Here, the drawing work would be spread out between the begin method and the onMousePress, onMouseDrag and onMouseRelease methods.

  3. We might want a scrollbar instead of buttons so that we could adjust the level more precisely.

    Demo: Boxball using a Scrollbar

    Don't worry about how to make the scrollbar work. Just think about how hard it would be to draw it in the first place. You would have to place the little arrows on the ends, draw the regions with different shading, etc.

Fortunately, these seemingly complex user interface objects can be created easily in Java (not so in some other languages!).

Scrollbars

We begin with scrollbars.

Warning: I call them scrollbars, but some say that they should be called sliders unless they are actually being used to scroll something.

We start with scrollbars because:

If we look at the code for Boxball Using a Scrollbar, we find two statments responsible for displaying the scrollbar:

// create and display the game-level scrollbar
level = new Scrollbar(Scrollbar.VERTICAL, EASY_HEIGHT, 1, 
                      HARD_HEIGHT, EASY_HEIGHT);
add (level, BorderLayout.EAST);

First, we construct a Scrollbar. You might expect that the parameters of the constructor to describe things like the width of the scrollbar, it's position within the window, etc. It turns out that this is not the case. There are two ways you can see this:

The add method is what actually places a component on the screen. We will see other variants later, but for now, all you can specify for the location of the component is one of the four compass points.

Let's see what the Scrollbar constructor parameters mean. The scrollbar is somehow going to determine the position of the boundary above which we have to drop the ball. It would be nice if, after the scrollbox is moved, we could just ask the scrollbar (using some method) where the line should be placed. This will only be possible if the scrollbar knows the allowable range of boundary line positions. This is what (most of) the parameters to the construction are for.

Choices

A pop-up menu is created using an AWT Choice object. These pop-up menus are also known as "selection boxes" or "drop-down boxes."

Creating these menus is simpler, but involves an extra method.

The constructor for a Choice takes no parameters. It simply creates an empty menu.

	popup = new Choice()

Then, to fill the menu in with items we issue statements of the form:

popup.add("Easy");

The menu is again placed on the display using the add primitive.

Buttons

Buttons are very easy to create, but we see that add is a bit more complex.

First, to create a Button:

new Button("Easy");
Where the parameter simply specifies the text label for the Button.

In this program, we need three buttons, one for each level. To add these to the screen, we need to use three different compass points.

So what we do is to create a Panel, and add the buttons to the panel. The panel will do something intelligent to arrange the buttons. We will see later how to have more control over exactly how the buttons are displayed. The panel is then added to the screen at a compass point, like the items we saw previously.