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.
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, rebelsky.io.SimpleGUI 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.
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 rebelsky.io.SimpleGUI
Depending on time restrictions, I may update the SimpleGUI to include menus, but they won't do much at first.
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
Circles.java. You can also type
% /home/rebelsky/bin/example Circles.java
to make a copy.
Once you've made a copy, compile it, run it, and scan the code, paying attention to the following notes.
SimpleGUIAs you may note from looking at the code, to use the SimpleGUI, you'll need to do a variety of things.
java.awt.*. This tells Java to import all of
the AWT classes. Since a typical program will use many of them,
rebelsky.io.SimpleGUI. Any time you use a
class, you must import.
SimpleGUI. This tells Java that
your objects can do anything that SimpleGUIs do, often
using the same instructions.
public void paintStuff(Graphics g)
that paints whatever you want on the screen. To see what you can paint,
look at
java.awt.Graphics
or the sample
code.
public void handleKey(char key)
that decides what to do when any key is pressed.
main() routine. Note that it does
not include a call to System.exit(). This is because the
SimpleGUI needs to do its own exit.
public static void main(String[] args)
{
YourClass gui = new YourClass();
gui.setTitle("Whatever you want");
gui.setCaption("Whatever you want");
gui.run();
} // main(String[])
As you look over the code in
Circles.java
and the notes above, you may want to reflect on the following:
setTitle,
setCaption, and run for objects in the
current class, even though they aren't defined.
super.handleKey?
switch statement does?
paintStuff and handleKey get
called?
switch statement
You may have noted that
Circles.java
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:
...
break;
case value2:
...
break;
...
default:
...
break;
} // 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.
Rename Circles.java to Ovals.java 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.
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 %.
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 rebelsky@math.grin.edu