Concepts / Introduction to GUI Programming

Introduction to GUI Programming

SimpleGraphicsStarter.java is the text's first example of a GUI program, meaning a program whose result is a graphical interface or picture.

  • Programming

The Question to Keep in Mind

Most of the programs you have written so far probably communicated through text: a line of output in a console, a number printed, or a message typed back to the user. This article introduces a different kind of program, one whose result is a picture that appears in a window. The example is called SimpleGraphicsStarter.java, which comes from Section 3.9 of the text. Its job is easy to state: it draws a large number of disks, and each disk is randomly colored and randomly positioned.

As you read, keep one question in mind: after the program has finished drawing, what does the graphics window look like? The rest of this article builds the answer step by step, starting from the idea of a graphical user interface and ending with a before and after view of the window.

What Makes a Program a GUI Program

Definition

A GUI program is a program whose result is a graphical interface or picture. SimpleGraphicsStarter.java is the text's first example of a GUI program, meaning a program whose result is a graphical interface or picture.

The text describes SimpleGraphicsStarter.java as the first example of a GUI program in the course. This is a useful starting point because the program is small enough that you can see the whole idea at once: it produces a graphical result, and that result is what the program is about. Later programs in the text build on this same idea, so it is worth understanding this first example clearly before adding more complexity.

Reading the Disk-Drawing Program

The defining feature of SimpleGraphicsStarter.java is that it generates graphics with random properties. The program draws a large number of disks, and each disk is randomly colored and randomly positioned. The word randomly applies to two properties of each disk: its color and its location in the window.

Because the properties are random, the program does not draw one fixed picture that you could predict in advance. It draws many disks, and each one has its own color and its own place. Taken together, the drawing is a collection of disks that all differ from one another. Thinking of the final picture as a collection, rather than as a single object, is the key to reading the program's result.

Best practice

When you read the result of a graphics program, describe the kinds of properties the output has rather than expecting one specific picture. For this program, the right description is that the window holds a large number of disks, each with a random color and a random position.

Tracing the Window Before and After

To read the result, compare the graphics window before and after drawing. The figure below shows the window at two moments. Before the drawing, the window has no disks in it. After the program has drawn, the window holds a large number of disks. Each of those disks has its own color and its own position, which is what the source means by randomly colored and randomly positioned. The change from the first state to the second is the whole effect of the program.

Empty windowno disks yetDrawing diskseach disk gets a randomcolor and positionWindow with manydiskseach disk differs from theothersprogram drawsdrawing finishes
What is in the window before the drawing code runs, what changes during drawing, and what the window holds at the end?

Notice what the diagram does not claim. It does not say which color any particular disk receives or where it lands. The source only states that each disk is randomly colored and randomly positioned, so the trace tells you the kind of change that happens and the kind of picture that results, not a fixed list of exact values.

Predicting the Window Before Drawing

What do you think happens?

Before the drawing code runs, what does the graphics window contain?

Reveal answer
Answer:

No disks. The window has no disks in it before the drawing.

The drawing is the change that puts disks into the window. The starting state is an empty window, and the final state holds many disks.

Why Each Run Looks Different

Running the same graphics program twice does not give you the same picture twice. The code and its structure stay identical from one run to the next. What changes is the set of random values chosen for each disk's color and position, so each run produces disks with different properties.

This is why the question to ask is about the kinds of properties the output has. Two runs can both show many disks, each with its own color and place, while looking different in every specific detail. The shared structure of the output is predictable even though its exact appearance is not.

  • Common mistake

    Expecting the same picture on every run

    Why it fails

    Each disk's color and position are random, so a fixed picture is not what the program produces.

    Do this instead

    Describe the output's properties: many disks, each randomly colored and randomly positioned.

  • Common mistake

    Treating the final picture as one object

    Why it fails

    The drawing is a collection of disks that all differ from one another.

    Do this instead

    Think of the window as holding a collection, and look at how the disks differ.

The Repeated Pattern Generic Programming Removes

Definition

Generic programming means writing code that works for many types of data instead of rewriting nearly identical code for each type.

Imagine you have written a working dynamic array for integers, a container that grows as you add items. Now you need the same thing for strings, then for decimal numbers, then for colors. If the only real difference between these versions is the name of the element type, you are copying the same logic over and over and editing one word each time. Programmers find this tedious, and every copy must be maintained separately. Generic programming is the response to that situation. Its goal is to write the code once, in a form that works for many different types of data.

Dynamic array of intadd, grow, read logicArrayList<T>one class, type left openDynamic array ofStringsame add, grow, read logicDynamic array ofColorsame add, grow, read logic
How do separate type-specific versions of the same code differ, and what single generic version replaces them?

Consider what changes between a dynamic array of integers and a dynamic array of colors. The steps for adding an item, growing the storage, and reading an item back are the same. The only detail that differs is the name of the type of item being stored. When two pieces of code differ only by a type name, the underlying idea is general, and the type is an input you have not yet abstracted away. Generic programming removes the repeated copies by leaving that type open.

Reading ArrayList<String>

Java's approach to generic programming is parameterized types. A class such as ArrayList leaves its element type open, and it is used as ArrayList<String>, ArrayList<Color>, or more generally ArrayList<T>. The figure below labels each part of ArrayList<String>.

ArrayListthe generic class<opens the type parameterStringthe element type this listholds>closes the type parameter
Which part of ArrayList<String> is the generic class, which is the type parameter, and what does String stand for?

Read ArrayList<String> as follows: ArrayList is the class, and String is the type parameter that fills the open slot. The type inside the angle brackets is the element type, which means this list is used for strings. There is only one ArrayList class. The type inside the angle brackets is what changes between uses, so ArrayList<Color> uses the same class with Color in that slot.

java
Notes on the code
  • The class name is the same in both lines; only the type in angle brackets differs.
  • names holds String values and colors holds Color values, from one class.

Mistakes Learners Make with Parameterized Types

  • Common mistake

    Thinking ArrayList<String> and ArrayList<Color> are two different classes

    Why it fails

    The source states there is only one ArrayList class; the type inside the brackets is what changes.

    Do this instead

    Read the angle brackets as the slot where the element type is supplied.

  • Common mistake

    Treating the type parameter as the class name

    Why it fails

    In ArrayList<String>, ArrayList is the class and String fills the type parameter.

    Do this instead

    Name the generic class first, then name what the type parameter stands for.

  • Common mistake

    Copying a type-specific version for each new data type

    Why it fails

    When code differs only by a type name, the same logic is duplicated and must be maintained in every copy.

    Do this instead

    Write the logic once, with the type left open, as generic programming intends.

Practice: Reading the Window and the Type

EASY

Using the disk-drawing program, describe the window before drawing and after drawing, then name the properties that vary between runs. Next, explain in one sentence what the type parameter in ArrayList<Color> stands for.

Hints
  • Start with what the window contains before any disks are drawn.
  • Describe the properties of each disk, not one exact picture.
  • In ArrayList<Color>, look at what fills the slot inside the angle brackets.

Key Takeaways

  • A GUI program is a program whose result is a graphical interface or picture; SimpleGraphicsStarter.java is the text's first example of one.
  • SimpleGraphicsStarter.java draws a large number of disks, and each disk is randomly colored and randomly positioned, so the window changes from empty to holding many differing disks.
  • Each run of the same graphics program produces different properties because the random colors and positions change, while the code and its structure stay the same.
  • Generic programming writes code once for many data types, removing the copies that differ only by a type name; Java expresses this with parameterized types such as ArrayList<String>, where the type parameter fills the open slot.