Discussion 10 handout
Objectives
- Add new components to a graphical user interface (GUI) layout
- Respond to action and keyboard events
- Query state of user input components
- Customize component drawing
- Illustrate concurrency of main and GUI threads
- Explore interaction of threads and exit behavior
Overview
We have given you the source code to a simple GUI program consisting of two classes:
Main
: A main program that starts a GUI, then does some other work in the background.WorldView
: A custom component for rendering a view of a simple “world” with a person in it, and handling keyboard input to move the person around.
The Main.main()
method sets up the user interface, which is laid out as shown:
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!
Tasks
- 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.
- 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 thatWorldView
has a mutator for setting the name, which should do the trick. Notice also that that mutator ends with a call torepaint()
, so that the UI gets redrawn with the new name. - The Quit button doesn’t actually do anything. We can add an
ActionListener
to make the program exit. Explore the following two options:- Call
System.exit()
to kill the entire program immediately. - Call
window.dispose()
to close the window without disturbing other threads.
- Call
- 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 inpaintComponent()
so there are arms too? - 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.
Submission
- Open the assignment page for “Discussion activity 10” in CMSX
- [Recorder] Find the “Group Management” section and invite each group member
- [Others] Refresh the page and accept your invitation
- [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
- Discussion is not a race. Focus on the current activity being facilitated by your TA and engage your whole group to propose and explain ideas.
- Elect a recorder to maintain the “master” copy of your work (others are still encouraged to jot down ideas on scratch paper). Rotate this position each week.
- It is a violation of academic integrity to credit someone for work if they were not present when the work was done, and the whole group is accountable. Your CMS group must only include classmates who attended section with you on that day. Remember that our participation policies accommodate occasional absences without penalty.
- It is your individual responsibility to ensure that you are grouped on CMS and that your group’s work is submitted before the deadline. Log into CMS the day after section to ensure that everything is in order, and contact your groupmates if not. It would be prudent for multiple members to photograph the group’s work.
- Only one group member (typically the recorder) needs to upload a submission. But their submission must not be uploaded until after all group members have confirmed their membership in CMS (contact your TA if you have trouble grouping in CMS).