Lab 7: Due November 6

In this lab we will work on some of the basics of window management, as well as extending the capabilities of your earlier labs. A window is basically a rectangle on the screen in which one can place text, draw, etc. It is the job of the window manager to determine when a window has been "selected" and how to place windows on top of each other, etc. Your program should be able to do rudimentary window (rectangle) management within a Java Frame.

Your program will have three basic modes. In "drawing" mode you should be able to create new rectangles and draw them in the frame. You should also have the facility to select colors while in this mode. At a minimum the user should be able to pick from blue, green, red, black, and white. You can either do the selection from buttons or menus, your choice. In "move" mode you should be able to select a rectangle by clicking on it and then move it around by dragging the mouse. At mouse release the rectangle should be in a new location (unless the mouse is released outside the frame). While you drag the mouse a "ghost" of the rectangle should be continously updated as the mouse moves. This ghost will be a simple framed rectangle of the dimensions corresponding to the movement. In this mode you should also be able to change a rectangle's color if it has been selected. In "resize" mode a selected rectangle should be resizable. This should happen when the mouse is clicked very near to any of the four corners (but not so near that it is very hard to do) on the rectangle. As with moving, when the mouse is dragged you should draw a ghost rectangle that continuously shows the new dimensions the rectangle would have. In any mode a mouse click on a rectangle should make it the selected rectangle, thus moving it to the top.

In previous labs it was enough to use a MouseListener. In this lab you will need the extended functionality of a MouseInputListener. A MouseInputListener has the same methods as a MouseListener, but also has mouseDragged and mouseMoved methods (which it gets from the MouseMotionListener class). Although you will now be implementing the MouseInputListener, in your constructor you will still addMouseListener(this) as you did before, but you will also addMouseMotionListener(this).

All drawing in this lab should take place within the paint() method of your JFrame class. Remember, calls to repaint() from other methods can force paint() to execute.

To keep track of what rectangles are on "top" of other rectangles you must use a List. You can use whatever implementation of a List you desire, but you must stick to methods from the List interface (you can find it in the Course Materials folder). So, for example, the list might hold the rectangles from bottom to top, then paint() would simply draw them in the order in the list. The list should not have redundant copies of the rectangles.

You must define some type of Rectangle class for this lab. It should store information on a rectangles coordinates, size, color, etc. You'll find it is also useful/necessary to use it to track whether or not a rectangle has been selected, keep an alternate set of coordinates during move and size operations, etc. The major challenge of the lab will be identifying when a rectangle has been selected, and then tracking changes in its dimensions.

A good (but not required) extension of this lab is to include the same functionality for Ovals and Lines as well as for rectangles.