Assignment 6. Java


Graphics with Swing                DUE: April 27, 2000 -- 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 Java 1.2 event model, employing delegation to separate the code handling input (mouse and keyboard) events from the code implementing the drawing routines.

On startup, your program 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 mode (which type object is being drawn, and other associated attributes like color, fill, font, etc.) must always be obvious, either by highlighting the currently selected button, or displaying or highlighting the current mode selected via a menu.

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, all objects 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 color of objects to be drawn, either via buttons or pulldown menus.

Several of the object types can also be filled when drawn (rectangles, ellipses and polygons). This means that there must be a way to select the current fill color for all objects that can be filled, again either via buttons or a pulldown menu. The fill color can be one of the four colors for objects (black, blue, green or red), or the user can specify using no fill.

Moving, copying and deleting objects

Your program must also allow the user to select a displayed object that can then either be moved, copied or deleted from the display. 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 and deleting objects.

Once the delete mode has been selected by the user, a left mouse click near an object selects the object to be deleted, and a right mouse click removes the object from the display (note that this allows selection of a different object if the wrong one is initially chosen). The simplest strategy for deciding which object is being selected is to find one whose bounding box contains the current mouse position. Also, changing to a different mode (e.g., to draw an object) must be allowed after selecting an
object with a left mouse click, but before deleting the object with a right mouse click.

Once the move mode has been selected by the user, a left mouse click near an object selects the object to be moved, just as in delete mode, and a right mouse click selects the new placement of the object. Again, multiple left mouse clicks are allowed, to change the object selected to move.

Once the copy mode has been selected by the user, a left mouse click near an object selects the object to be copied, just as in move mode, and a right mouse click selects the placement of the copy of the object. Multiple left mouse clicks are allowed, to change the object selected to copy.

While simply drawing objects on the display does not require storing all the information for the objects in your program, moving, copying and deleting 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 object that is selected for a move, copy or delete operation.

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.

Instructions for submitting your work

Your work may not be graded if these procedures are not followed exactly.