CSCI E-70
Harvard University Extension School

Spring 2005

Java for Graphical User Applications

Assignment 1

02 Feb 2005 / Due 18 Feb 2005

A.  Reading

Focus on that which relates directly to lecture materials.

http://java.sun.com/docs/books/tutorial/uiswing/14start/index.html and subsequent pages.

Preparatory, for next lecture:

Geary pp. 1-16 [Swing intro]

Geary pp. 71-120 [skim for conceptual discussions only; ignore advanced code]

Geary pp. 271-280 [BoxLayout layout manager and Box utility class]

B.  Questions: 100 pts. Approx. 10% of “homeworks” grade.

-1. (10 points) Survey

Fill out the student survey and turn it in at the end of class.

-0.5. (10 points) UBB and the Syllabus.

Sign on to the UBB: http://ubb.dce.harvard.edu. Locate the CSCI-E70 General Discussion board. Read the on-line syllabus throughout. In one post, introduce yourself, and pose a thoughtful question or two about the course or any of the material arising from the syllabus, especially homework 1.

-0.25 (0 points) TWSOGMM

Make sure your Harvard account, password, e-mail address, use of e-mail services, location & use of Harvard computing facilities, registration status are all sorted out to taste. www.extension.harvard.edu and www.fas.harvard.edu/computing should have everything you need. If you give us a separate e-mail and work from home entirely, you don’t actually need to use any physical Harvard computing facilities – but you DO need an account to submit work! – see below..

0. (0 points) Software Installation

If you have high-speed Internet and will be using your own equipment:

Follow the links on the resource page of the class website to install the JBuilder 2005 Integrated Development Environment. Note that the bare Java tools and libraries, still usable by themselves, are found in C:\Borland\JBuilder2005\jdk1.4. Start a new project, create the src directory yourserlf, and import the assignment code into it. Ensure you can modify and run the code. Also install JDK 5.0, also linked from the resource page.

If you do not have high-speed Internet and will be using your own equipment:

Use the CD supplied in class (thanks, Pat) to do the above.

Note the location of the JDK “bin” directory (by default C:\Program Files\Java\jdk1.5.0_01\bin) and add it to your system PATH environment variable for future convenience.

If you will be using exclusively the Harvard computing facilities:

Familiarize yourself with the facilities, their hours, and the location of the JBuilder/JDK1.5 within the lab computers. Ensure that you can start a new project and manipulate and run the assignment code, and be able to transfer the complete project directory between your Harvard account (long term storage) and the local disk (for development). You’ll want to use Harvard-supplied SecureFX which can transfer entire directory structures without archiving/zipping first.

In all cases:

Download, compile and run the introductory example from Lecture 1: Elmo.java

You should try command line commands:

C:\e70\hw1> javac Elmo.java

[Ensure that a Elmo.class file has just been created in the same directory.]

C:\e70\hw1> java –classpath . Elmo

[The same window as shown in lecture should come up and respond to mouse events.]

[NOTE: Type in the full path to java or carefully edit your system %PATH%. Note: Pre-XP Windows insists on maintaining a pre-historic version of ‘java.exe’ in something like ‘C:\winnt\system32’. Careful not to inflict it on your program!]

1. (15 points) Navigating the JavaDoc API pages

Follow the links on the resource page to the JDK 1.5 API documentation on sun.com.

Answer the following simple questions to familiarize with this documentation set, and get a peek at upcoming material:

a. What are all of the ancestors, in ascending order towards the root, of the JFrame class?

b. What are all of the other immediate children of the grandparent class of JFrame?

c. What distinguishes the classes in Part B from all other Swing components?

d. On the Overview section of the API docs, there are general descriptions of the purpose of every package in the J2SE. Take a moment to skim over these, especially the GUI-related ones. In one of the descriptions, there is highly inaccurate information. Which one is it? Explain and suggest a corrected summary. Feel free to comment on more than one for a point or two of extra credit. You already know enough from the syllabus and lecture, so you may safely ignore anything you’ve never heard of.

2. (55 points) Modifying Elmo

The introductory program is cute but not very interesting. In this exercise, you will add to the code so that the program is...cute but a little bit interesting.

One half of the challenge is mastering some layout basics: telling Swing what you want where. The other half is in organizing your action handlers and event parsing & dispatching. The third half is instantiating all the objects you’ll need, and finding the right methods to customize them as directed.

a.  Find a cute icon of your choosing from any source, even drawing it yourself, or perhaps a Web clip-art site. Create an image file out of it, put it in an ‘images’ directory, add that directory as a JBuilder library, and add that library to the project’s list of required libraries. Make the icon appear next to the “Hee-hee” text when the button is pressed. You’ll need to use the ImageIcon class.

b.  Create a keyboard equivalent so that Alt-<some letter> causes the “Tickle me” button to be pressed. You will have to look up mnemonics.

c.  Add a subordinate panel as the next element in the main vertical Box. Add two buttons and some labels in a horizontal Box.

i.  Create a “Reset” button that changes the button back to “Tickle me” with no icon.

ii.  Create a “Disable” button that has two states:

1.  When it says “Disable”, it should de-activate the “Tickle me” button so it cannot be pressed, and change its own text to “Enable”

2.  When it says “Enable”, it should activate the “Tickle me” button so it can be pressed, and change its own text to “Disable”

iii.  Arrange the action handler such that every time the “Tickle me” button is pressed, the text of the label changes to “Number of clicks: n” where n is the number of times the “Tickle me” button has been pressed.

d.  Recall the coding & commenting standards from lecture. Start practicing now!

e.  Call it something better than “Elmo”.

f.  Turn in three screen captures of the program in different states. Since the images are small you can lay them out on one page. Turn in the complete JBuilder project.

3. (10 points) State diagram

Using standard FSM notation, supply the state diagram for the modified program. Note: do not worry about the stages of a single click as in the slides; that level of pedantic detail can now be subsumed. In other words, no longer notate the ButtonModel. In yet more words, every button-press-release is an atomic action. Concentrate instead on the application model which uses the buttons and labels as building blocks. The states should reflect the different possible appearances and behaviors of the application. Recall that the particular notation we will be using for this class is:

·  descriptive short name within each state circle

·  input that caused the transition on top of every transition arc

·  side-effects incurred during the transition (both visually and to internal variables) below every transition arc

4. Extra credit

a.  (up to 2 points) See 1.d.

b.  (up to 1 point) Make the theme of the application something more grown up, changing all label and button text and picture. Find something more clever than “number of clicks” for the label.

c.  (up to 3 points) Make your comments in full JavaDoc style according to Sun’s JavaDoc specifications. You can either infer from existing code or read java.sun.com/j2se/javadoc/index.html. This material will be covered in lecture 4 or 5 and required thereafter.

d.  (up to 3 points) User-editable text! Add a JTextField that integrates its data into the application in some apparent way, perhaps using another “Submit” JButton that takes the text, manipulates it, and puts it somewhere else in the application.

– Page 4 of 4 –