Project 6 - Design Patterns and Swing/AWT GUI Components
 Due May 13, 6:00PM

You are going to implement a drawing package using the Java Swing library, which is built on top of the Abstract Windowing Toolkit (AWT). The program will manage a GUI (graphical user interface) consisting of a single window on a user's workstation/PC, taking input from a mouse to add and delete graphical objects from the window.

The handling of mouse events should use the Observer design pattern (also called the JavaBeans event model in the Swing tutorial), employing delegation to separate the code handling input (mouse) events from the code implementing the drawing routines.

On startup, your program will be in Student visual mode (explained below). It should display a window containing a blank drawing area, and buttons or menus to select the various objects the user may draw.  The window should be half the size of the user's display. You get to choose whether to use buttons or pulldown menus to allow selection of the various types of objects to draw. If you use buttons, use text to label each button with the mode that is selected by that button. With either buttons or a menu, only one type of object can be selected at a time, and objects of that type are drawn until a different object type is selected by the user. The current operational mode (which type object is being drawn, and other associated attributes like border color, visual mode, etc.) must always be obvious, either by highlighting the currently selected button, or displaying or highlighting the current mode selected via a menu. In Student visual mode all text should be in 12pt font.

The objects that can be drawn are:

For all these objects, selecting points with the mouse is done via mouse clicks (no dragging with the mouse button depressed). In addition, the border for an object can be one of four colors: black, blue, green, or red. As for the object types, you must allow the user to select the current border color of objects to be drawn, either via buttons or pulldown menus.

Moving, copying, deleting and grouping objects

Your program must also allow the user to select one or more displayed objects, that can then be moved, copied, deleted or grouped. This means that in addition to buttons or menu entries for drawing the various object types, you must also have buttons or menu entries for moving, copying, deleting and grouping/ungrouping objects.

For all of these operations, selection of objects is done with left mouse clicks, sometimes combined with the Control (Ctrl) key for the grouping operation.  A left mouse click on an object selects that object, deselecting any previously selected objects.  A left mouse click on an unoccupied portion of the display window should deselect all objects, so that no object is selected.  To select more than one object, the Ctrl key should be pressed while performing a left mouse click.  Such an action toggles the selection of the object clicked on, meaning that if the object is not currently selected it becomes part of the currently selected set of objects, and if the object was already selected it becomes deselected.  The strategy you should implement for deciding which object is selected with each mouse click is to:

  1. find all objects whose bounding box contains the mouse location, then  
  2. find the object whose center point is closer to the mouse location than any other object satisfying condition (a).  

The bounding box of a line is the rectangle formed using the endpoints of the line as opposite corners of the rectangle, while the bounding box of a circle is the square circumscribed on the circle (the square with side length equal to the diameter of the circle, with the same center point as the circle).  Finally, changing to a different operational mode (e.g., to draw an object) deselects all currently selected objects.

When the delete mode is selected by the user, left mouse clicks are used (ignoring the Ctrl key) to select the object to be deleted. A right mouse click then removes the selected object from the display. The delete operation can only be applied to a single object.

When the move mode is selected by the user, left mouse clicks are used to select the object to be moved, as in delete mode. A right mouse click then determines the new placement of the object.  The move operation can only be applied to a single object.

When the copy mode is selected by the user, left mouse clicks are used to select the object to be copied. A right mouse click then determines the placement of the copy of the object. The copy operation can only be applied to a single object.

When the group mode is selected by the user, left mouse clicks are used (including with the Ctrl key, as described above) to select the objects to be grouped.  A right mouse click then groups the selected objects into a single new object that can then be deleted, moved, copied or grouped again with other objects.  Note that a straightforward implementation of operations on a group of objects passes the operations through to each object in the group (recursively if groups are nested).  See the design patterns discussion below for more details.

When the ungroup mode is selected by the user, left mouse clicks are used to select the object to be ungrouped.  A right mouse click then ungroups the selected object, effectively undoing the group operation that created it. Applying the ungroup operation to a non-grouped object has no effect.  The ungroup operation can only be applied to a single object.

While simply drawing objects on the display does not require storing all the information for the objects in your program, moving, copying, deleting and grouping/ungrouping objects does require storing this information. So you will need some data structure to keep track of all the objects that are currently displayed, and the data structure will need to be searched to find the objects that are being selected for a move, copy, delete or group/ungroup operation.  The data structure will also be needed for switching between visual modes.

Design Patterns

You will have to support two visual modes, one called Professor mode and the other called Student mode, switching between them via either a button or pulldown menu. Student mode is the default at startup. Professor mode is meant to make it easier for near-sighted and color-blind professors to work with your system. In Professor mode components are bigger than when in Student mode. Size is specified as a magnification factor. In particular, windows are bigger (1.5x), graphical objects are bigger (1.5x), borders are always black and text for buttons/menus is larger (1.5x, or 18 point). In Professor mode you can also select an object more easily (the object's bounding box is is visible, colored gray and 1.25x larger than the smallest bounding box that contains the now larger object). Note: the bounding box is not visible in Student mode. When you switch from one visual mode to another you should recreate the drawing window, including all buttons, menus and graphical objects. Make sure that the object's centers do not change position when switching visual modes. Use the State pattern to make the visual mode transparent to your system. 

You will need to perform certain operations, such as group move, group delete, and object recreation after a visual mode change, on multiple objects. Use the Visitor pattern for these operations. That is, write groupMoveVisitor, groupCopyVisitor, groupDeleteVisitor, and groupRecreateVisitor classes that work together with your graphical object classes. You will also have to create the data structure(s) needed to use this pattern.

Exiting the program

Finally, one additional button or menu entry is required to allow the user to exit your drawing program. Providing a confirmation message before exiting (e.g., "Do you really want to exit? (y or n)''), to allow the user to change his/her mind, is required.

Swing/AWT Notes

A short tutorial for Swing is on the Sun developers web site, and a more extensive tutorial is on the main Sun Java web site.
A short tutorial for the Abstract Windowing Toolkit (AWT), which underlies Swing and which you may also need to use directly, is also on the Sun developers web site.

The APIs for Swing and AWT are available with the Java 1.3 API, under javax.swing and java.awt, respectively

Instructions for submitting your work

 

Web Accessibility