Homework 4

22C:22 Object-oriented Software Development
Due Monday, February 19, 2007

15 points


  1. Implement an interactive graphical program for creating and editing shapes. Use the Shape interface, and LineSegment, Rectangle, Square, Circle, and Polygon classes defined in Homework 3.
      Details requirements are:
      • your program should maintain a collection (implemented as a java List or other appropriate class/data structure) of all of the current Shapes and, at all times, the graphics window should display all of them.
      • your program must allow interface creation of new shapes, using mouse clicking and movement to indicate shape properties. (Though this might seem difficult if you've never done it before, it can be done quite simply. Some detailed hints are farther below).
      • your program must support the notion of a "currently selected" shape, and provide (1) specialized display for the selected shape, and (2) a way to change which object is the selected one. For example, your program could draw the the selected shape in red and all the others in black. And, your program could use the left and right arrow keys (or just one of them or a different key) to cycle through your objects, changing which one is the selected one.
      • your program must support graphically specified movement of the currently selected object. (Again, this is be done very easily. Hints are below, with the hints about shape creation.)
      • your program must make it possible to delete the currently selected object
      • when your program is called with a filename argument, it should create and display its initial sets of shapes using the specifications in that file. Use the same file format as Homework 3.
      • your program must be able to save all of the current shapes into a file, again using the file format of Homework 3. Thus, if you save your shapes and then quit the program and restart it with the saved file as argument, your window should display the same situation. You may "hardcode" the output file name into your program. For instance, you could simply use "savedshapes.text" as the name of the the output file. (Of course, this isn't too limiting, because if you want to save your favorite files of saved shapes, you can copy or change the file's name from outside the program.)


      SUBMISSION and PRIZES: In addition to the usual code and README file, include a savedshapes file that your program created. Your README should specify what each relevant keyboard key means and how mouse gestures are interpreted.

      One small prize (having nothing to do with homework grade) will be awarded to the best/most interesting set of saved shapes (students might get to vote among finalists)

      A second small prize (having nothing to do with homework grade) will be awarded to the best program in terms of implementated user features. The basic homework 4 requirements don't yield a really nice editing program (as my sample shows). But, with a little more effort, feature can be improved substantially. For instance, a temporary "under construction" version of the shape being created could be display while the mouse being held down and dragged. Please feel free to contact me for help on how to do this. Other features that could be supported: more kinds of shapes, objects could be displayed as filled shapes rather than outlines, menus could be used instead of keys for various things, you could make it so that each object instance can have its own color, instead of using keys to change the selected object, you implement mouse-based selection (again, feel free to ask for help on this one), etc.

      HINTS.With careful design/organization, the coding for this program can really be quite simple. Some hints:

      • I have provided a skeleton program that will create the editing window. Three java files and a README.text file are here: editorSkeletonSource.
      • For mouse and keyboard interaction, all requirements can be met by simple implementation of just three event methods: MousePressed, MouseReleased, and KeyPressed.
      • You will need to add at least a move(int x, int y) method to your Shape interface. You shouldalso add a toOutputForm() method that returns a String of the form needed in the output file. (It will be similar to the toString method but with different format.)
      • I suggest you use keyboard key presses to indicate what "mode" your program is currently in. For example, if a user presses the 'l' key, your program could go into "line segment creation" mode, meaning that it will interpret subsequent mouse gestures (until the mode is changed again) as relating to line segement creation. Things can be quite easy if you use a separate mode for creation of each type of object and for moving objects.
      • You don't need to get fancy with shape creation. Oncee in shape creation mode, you can use the next two (one up/one down) mouse clicks to completely define any of the shapes (except polygons, which take several clicks - it's still easy, but ignore polygon input until you have everything else in the program working). For instance, my program uses the same kind of mouse gesture for creation of all shapes (except polygons) and also for movement. The gesture is a mouse down-click, followed by a drag, followed by a mouse release/up-click. My program detects the down-click (in MousePressed) and saves the x and y coordinates of the mouse location, and then detects the up-click (in MouseReleased) and saves these x and y coordinates as well. These 4 values (x1, y1, x2, y2) can be use to create an appropriately sized Shape and add it to your list of shapes. For example, when in circle creation mode, x1 and x2 can represent the center of the circle and the distance between (x1,y1) and (x2,y2) can define the radius.
      • For moving the currently selected object, you can use the differences (x2-x1) and (y2-y1) as amounts to move the object in x and y. Thus, if you represent a circle using a point, (xc,yc), and a radius, the move(int x, int y) method for the Circle class simply needs to change xc to xc+x and yc to yc+y.
      • For polygons, you'll need several clicks to define each point of the polygon and you'll need some way to indicate when you're finished adding points (a common way is to do a right-mouse click instead of left, but whatever you'd like is fine).


      Sample executable program: you can try my program by copying the files in the sampleEditor folder. To use if from the command line, execute 'java -cp . Editor'
      Basic documentation for the sample:
      
             KEY            RESULT
      
              w             save current shapes in "savedshapes.text"              
              SPACE         toggle whether currently selected shape is
                              shown in red or not (initially, it is "on")
              left-arrow    change selected item to "previous" item in 
                              shapes list
              right-arrow   change selected item to "next" item in 
                              shapes list
              d             delete the currently selected item 
      
              l             enter "create line segment" mode
              q             enter "create square" mode
              r             enter "create rectangle" mode
              c             enter "create circle" mode
              p             enter "create polygon" mode
      
              m             enter "move" mode
         
        Note that when in one of the "create" modes, the program remains in that
        same create mode until another is chosen.  That means you can 
        create multiple objects of the same kind without needing to 
        hit any keys.
      
        In "create polygon mode" each left down-click specifies another point of the
        polygon. A right-click indicates that you are finished creating that polygon.
        (A subsequent left-click would begin another polygon.)