Fundamentals of Computer Science II (CSC-152 98S)

[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Labs] [Assignments] [Examples] [Bailey Docs] [SamR Docs] [Tutorial] [API]

Lab 3: Graphical User Interfaces

Purpose: In this lab, you will attempt to enter and run your first Java Graphical User Interface (GUI) programs. In addition to allowing you to work with more graphical programs, it will also help you improve your understanding of subclassing. This laboratory also introduces the switch statement, the standard Java documentation, and Java's random number generation library.

If you want to start coding before reading the background material, jump to the section labelled Programming.


GUIs in Java

One of the nicer features of Java is its inclusion of a number of libraries for providing interactive, window-based interfaces for your programs. Such interfaces are often called GUIs, which is short for Graphical User Interfaces. The primary library that Java provides for building such interfaces is the Abtract Windowing Toolkit or just AWT.

Unfortunately, it can be difficult to build even a simple interactive program with the AWT, as you must consider not only the pieces of your program but also handle a number of events. To simplify matters, I've set up a simple class, that you can subclass in order to build simple interactive programs. I've only begun work on SimpleGUI, so it is likely to gain new features as time goes on.

Running SimpleGUI

By default, SimpleGUI does very little that is interesting. It pops up a window and prints instructions for quitting. You can observe this by typing
% ji

Depending on time restrictions, I may update the SimpleGUI to include menus, but they won't do much at first.

A Subclass of SimpleGUI

The power of SimpleGUI comes from the subclasses we can create. In particular, since SimpleGUI and the AWT handle the main work of putting up a window and watching the outside world, you can write programs that provide simple interactivity by providing only two methods, paintStuff(Graphics g), which draws on the screen, and handleKey(), which determines what to do when a key is pressed. You'll also need a simple main (described below).

I've used SimpleGUI to build a simple interactive circle drawing program (an optimist might call it the start of a sight for a submarine game or some such). Make a copy of the code, saving it as You can also type
% /home/rebelsky/bin/example
to make a copy.

Once you've made a copy, compile it, run it, and scan the code, paying attention to the following notes.

Using SimpleGUI

As you may note from looking at the code, to use the SimpleGUI, you'll need to do a variety of things.


As you look over the code in and the notes above, you may want to reflect on the following:

The switch statement

You may have noted that uses a switch statement in its implementation of handleKey. What is a switch statement? It's a generalization of the if statement.

A switch statement has the form

switch (expression)
  case value1:
  case value2:
} // switch
The switch evaluates the expression and compares it to the values. When it matches a value, it executes the corresponding code up to the break. If it doesn't find a matching value, it executes the code marked with default. In effect, switch is a less general form of Scheme's cond.


Extension: Ovals

Rename to and change the appropriate parts of the code to reflect the new name. Then, allow the user to change the width and height of the "circles" (which then become ovals). You will most likely need to change handleKey to handle different keypresses and assign meanings to those keypresses.


Write a new class, Splatter that subclasses SimpleGUI. Your class should "splatter" a shape at a random point on the screen, allowing the user to select some aspect of the splattering (e.g., shape or color) by the key pressed.

Random Numbers

How do you get a random number? Using objects from class java.util.Random. If you read the documentation you will note that you can get an integer with random.nextInt(). You can bring a random number down to a reasonable range with the modulus operator, which is written %.

[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Labs] [Assignments] [Examples] [Bailey Docs] [SamR Docs] [Tutorial] [API]

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

Source text last modified Sun Feb 1 21:31:29 1998.

This page generated on Sun Feb 1 21:34:49 1998 by SiteWeaver.

Contact our webmaster at