Discussion 10 handout



We have given you the source code to a simple GUI program consisting of two classes:

The Main.main() method sets up the user interface, which is laid out as shown:

GUI component layout

However, the user interface is only partially hooked up to do things. You should be able to move the walker around the world using the arrow keys, and you can see the code for that in WorldView. However, the text field doesn’t do anything, and neither does the quit button.

After creating the application window, the main thread sleeps for a bit, blocking the main thread. But as you can see, the window remains responsive, since Swing runs on a separate thread. When the main thread eventually returns, the application keeps running until the window is closed; alternatively, if you close the window first, the program will keep running until the main thread finishes its task. It’s like having two programs in one!


  1. Currently the text field has no label to suggest what it does. Add a label to the left of the field that tells the user what it’s for. The idea is that it’s supposed to set the name of the walker.
  2. Next, let’s wire up the text field to change the name of the walker when the user presses Enter. You need to specify code for the ActionListener. Notice that WorldView has a mutator for setting the name, which should do the trick. Notice also that that mutator ends with a call to repaint(), so that the UI gets redrawn with the new name.
  3. The Quit button doesn’t actually do anything. We can add an ActionListener to make the program exit. Explore the following two options:
    1. Call System.exit() to kill the entire program immediately.
    2. Call window.dispose() to close the window without disturbing other threads.
    Discuss: How is the main thread impacted by your choice?
  4. Now let’s upgrade the WorldView component a little. The walker currently has legs but no arms. Can you figure out how to modify the rendering code in paintComponent() so there are arms too?
  5. The WorldView component is watching for arrow keys to be pressed. Let’s make the spacebar do something. Here you have a chance to be creative—it’s up to you what the spacebar should do.


  1. Open the assignment page for “Discussion activity 10” in CMSX
  2. [Recorder] Find the “Group Management” section and invite each group member
  3. [Others] Refresh the page and accept your invitation
  4. [Recorder] Submit your version of “Main.java” and “WorldView.java” to CMSX.

Ensure that your group is formed and your work submitted before the Friday evening deadline.

Tips and reminders