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:
- The ability to draw lines, rectangles, ovals, and polygons. The
type of shape to be drawn must be selectable using a menu named
"Shape". You may optionally provide a toolbar with buttons (in
addition to the menu options).
- A line is drawn by selecting two points on the screen using two
single clicks of the mouse.
- Rectangles and ovals are drawn by using two single clicks of the
mouse. These two clicks represent opposite corners of a bounding box.
- Polygons are drawn by repeated single clicks. Each click, after
the first, should draw the polygon as it would appear based upon the
points specified up to that point. E.g. after 2 clicks you will see a
line, after 3 clicks a triangle, and so on. The last point is specified by
either a double click or by a single click followed by selecting any
menu item.
- Shapes (except lines) can be drawn filled or in outline form. The
choice is specified on a "Fill" menu with items Filled and Unfilled.
- Shapes can be deleted by selecting a shape and then selecting
"Delete" from the "Edit" menu. A shape is selected by first selecting
"Select" from the edit menu, then clicking near the object to be
deleted. The object nearest the mouse click should be identified in
some way (e.g. draw in a different color). The user can select a
different object by simply clicking somewhere else. This deselects the
original and selects a new object, provided there is an object
sufficiently close to the newly clicked point. If not object is
nearby then no object is selected. You are free to make any reasonable
design decision about what it means to be nearby.
See below for help on finding the
nearest shape.
- The most recently deleted shape can be undeleted by selecting
"Undelete" from the "Edit" menu. If no shapes have been deleted, the
undelete menu item should be disabled.
- Include a "File" menu with the only required option being "Exit".
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]