CS 105 Lab 6 -- Spring 2000

In lab 3, we led you through the construction of a simple Java program in order to give you some familiarity with the MetroWerks programming environment and an introduction to the programming process.

This week we will concentrate on increasing your understanding of the programming process itself. We will again lead you through the construction of a simple program. This time, however, we won't tell you exactly what Java instructions you need to type. We will explain what methods are needed and what each method should do in English. Your job will be to translate our English descriptions of the methods into Java.

In addition to giving you the experience of writing Java instructions of your own, this week's lab is also designed to give you some experience using the Java primitives for constructing user interface components. Many of the details you will need to work with these Java mechanisms are not included in this document. Instead, they have been discussed in class and can be found in a separate "Java Programming with JavaTools" handout. The following link will take you to this JavaTools documentation . Printed copies of this handout will also be made available to you during lab. The handout describes all of the Java graphics primitives and user interface components. You will only be concerned with the graphics primitives for drawing images on the canvas and the "Slider", "Button" and "TextField" user interface components.

The applet you construct in this lab will implement the behavior of a common component of many "real" program you use regularly. For example, if you view a page in Navigator that is too small to fit in your browser's window, Navigator adds scroll bars to the window to let you control the portion of the page's content that is visible. Scroll bars serve a similar purpose in may other practical programs. For this lab, we want you to implement a not-so-practical program that provides such scroll bars.

Your applet will provide controls to allow its user to select an image to display (either from a file or from the web) by typing in the name or URL of the image. It will display the image in a window including scroll bars. In the case that the image is larger than the program's screen window, the person using the program will be able to move the image about in the window with the scroll bars. In fact, even if the image is small, your program will still provide scroll bars since this will keep the program simpler than it would have to be if it were smart enough to decide when the scroll bars were really necessary.

A working version of the completed applet is included to the right so that you can understand its functionality. Unfortunately, this applet will not function correctly under Netscape Navigator on a Mac.

The applet displays a button containing the word "Fetch" on the left side of its window, and, at the bottom of the window it provides an area where the name of an image to be viewed can be entered. When the "Fetch" button is depressed with the mouse, the image whose name has been entered is displayed on the program's canvas (which consists of the center region of the program window). At the top and right sides of the program window, scroll bars are displayed. These can be use to move the image vertically or horizontally relative to the program window in order to view portions of the image that did not originally fit in the canvas.


To simplify printing, a version of the complete instructions for this lab as a single page is also available. Otherwise, follow the links below to read the remainder of this lab description.
  • Preparation
  • Displaying an Image
  • Adding the Image Loading Interface
  • Loading New Images
  • Adding the Scroll Bars
  • Scrolling
  • A Final Detail
  • Finishing Up
  • If you have questions about the instructions for this lab, you are encouraged to ask them through the discussion area for this lab in the course discussion forum.