Lab 3: Due Wednesday, October 2, 2002

 

Improving your drawing program

The drawing program we wrote for last week's lab had some limitations. One of the critical ones was that when the window was moved around whatever we drew was lost. Our first goal in the new lab is to fix that problem. To do this we will use the Vector class.

As we have discussed, in java drawing is normally done through the paint method in a JFrame. This method is called whenever a window needs to be refreshed, such as when it is resized or another window is selected. The trouble is, when we interact with a user we want to draw on the fly as we did in Lab 2. We can solve this problem by keeping track of what we have drawn and then simply redrawing everything everytime paint has been called. Essentially everytime we draw something we will store what we did in a Vector. When paint is called it can simply traverse the Vector and redraw each thing as necessary. When the user wants to empty the screen, the Vector can in turn be emptied as well.

The major challenge in this implementation is figuring out how to store a representation of what you have just drawn in a Vector. Since Vectors are used to store Objects, you will want to create a class for this task. The class you create should hold all of the information necessary to redraw whatever the user just drew. For example if the user just drew a rectangle, you'll need to store the coordinates, the height and width of the rectange and the fact that it is in fact a rectangle.

There are many ways to do this and we will choose one to make the second half of the lab (and subsequent labs) simpler. Instead of a single class, we are going to define two (optionally you can do four).

The first class will be very simple. It will simply contain and integer and an object. Call this class "Association". The methods for the class should be a constructor (of course), a method to return the integer (which we will call the "key") and one to return the object (which we will call the "value"). This class will prove useful throughout the term. In our implementation the key to a given association will be a number that tells you what the object is. For example, 1 might mean a line, 2 a rectangle, and 3 an oval. The object will then be a representation of what was drawn.

Your other class (or three classes) should store what is necessary to recreate the figures. Again, you should have constructors and "getters" for these classes.

With the classes in place you can now, in addition to drawing, make an Association with the proper contents and add it to the Vector you use to keep track of things. Later when you need to pull things out of the Vector, you first examine the key, then you can pull out the value based upon the key.

Note: If you are ambitious and want to include things like filled vs. unfilled, color, etc. you can add those to the classes.

Now implement an "Undo" menu command that erases the most recent thing drawn. Then get rid of the original "hello world" from the default program and make a drawing window pop up at the start. Finally change the resetting behavior, such that the program resets either by using a button, or by pulling down an appropriate menu selection. Mouse clicking should no longer do anything.

As for any program, I will expect hard copy of any code you have written or modified, as well as a working version in the drop Box with your name and Lab2 on the folder.