Homework 5 - Swing and GUIs


[Homepage] | [General Lab Info] | [TA's & Tutors] | [FAQ's] | [Homework] | [Excellent Programs] | [Exams]

Reading: Chapters 8 and 9.

Due: At 8:00am, Friday March 1, 2002, submit is turned off automatically. Work turned in after that time will not be accepted for grading.

DRAFT

This is a draft. I expect to continue to refine it over the next few days to a week. The essence will remain the same although some details may change.

Program Description

For this assignment you are to implement a drawing program. There are traditionally two types of drawing programs. One draws objects which can then be manipulated (resized, deleted, etc.). The other draws pixels on the screen. The latter are usually called painting programs. SimplePaint from chapter 8 of the text is an example of a "painting" program. Your program for this assignment is a "drawing" program. This program will be somewhat similar to hw3, but this time done using Java's GUI components and event handling.

Your program must support the following capabilities:

Some implementation suggestions

Here are some standard Java classes and methods that I found useful in my implementation. You are not required to use any of these but I think you should look closely at them. They will save you a lot of work.

There is an interface, java.awt.Shape that you should use to play the same role as the Shape interface from hw3. There are standard Java classes that implement Shape for the shapes you need, java.awt.Rectangle, java.awt.Polygon, java.awt.geom.Ellipse2D.Float. I used a polygon with only 2 points to represent lines.

You are free to use any reasonable notion of closeness for selecting the nearest object. If in doubt, you can always ask. For my solution I create a small square (6 pixels on a side) and then see if the outline of any of the shapes intersects the rectangle. This is trivially accomplished using the method intersects() implemented by all classes the implement java.awt.Shape.

The class java.awt.Rectangle has a method add(Point). I used this to turn 2 mouse clicks into a rectangle. Create a rectangle with sides 0 by 0 at the first point, then "add" the second point to the first rectangle. You can use the same trick with ovals by first creating a rectangle (that you never draw), and then using the setFrame() method of Ellipse2D.Float to adjust the oval/ellipse to fit the rectangle.

The class java.awt.Polygon has a method addPoint() which can be used to add points one at a time to a polygon.

All input events support the method getWhen(). You can use this to detect double clicks by noticing that 2 clicks happen close together. I used 500 milliseconds as the double click time interval (less than 500 ms => double click).

Class and Collaboration Diagrams

In addition to submitting your source for this assignment, please submit a class diagram of your program, and a collaboration diagram for one use case of your choice.

You do not need to use a fancy tool. NEAT, hand drawn diagrams are fine. If you want to submit the diagrams electronically, they can be in postscript, pdf, gif, jpg, or MSWord.

These diagrams are DUE on or before the start of class Tuesday February 26th.


[Homepage] | [General Lab Info] | [TA's & Tutors] | [FAQ's] | [Homework] | [Excellent Programs] | [Exams]