Homework 5 - Inheritance: A Drawing Program


[Homepage] | [General Lab Info] | [TA's & Tutors] | [FAQ's] | [Homework] | [Excellent Programs] | [Exams]

Reading: Chapter 8.

Due: At 8:00am, February 9, 1999, submit is turned off automatically. Work turned in after that time will not be accepted for grading.

Program Description

Most computer painting/drawing programs today have two modes of operation. In one mode, usually called "drawing," you draw and manipulate objects on the screen. Once placed, an object maintains its identity. That is, it can be modified, deleted etc. In the other mode, usually called "painting," you put down paint on the screen. Although there may be tools to allow you to place a particular shape, such as a rectangle or oval, once placed, you cannot edit the object. It is as if you have put down paint. The object loses its identity.

The SimpleDraw example from Chapter 6 is the beginnings of a "painting" style program. For this assignment you will explore some of the concepts needed for a "drawing" style program. For this assignment you will not actually do any drawing, instead you will implement the underlying structure needed to keep track of all of the objects in a particular drawing.

This will be a standard, command line program. The user will be allowed to add objects to the drawing by specifying the object type and any relevant parameters. Your program should allow the user to place at least four different types of objects, text, rectangles, lines, and ovals. Once placed, an object/shape can be modified according to its type. For example, you might allow moving the location of the object, or changing its parameters such as height or width. The accessor methods used to modify an object will be different for different types of objects. For example, text doesn't have a width, but it has the text. Likewise a rectangle doesn't have any text.

In addition to adding and modifying objects/shapes, you should be able to delete shapes.

For this assignment, I have given you something to start with. This is a simple driver that allows addition and deletion of rectangles and ovals. After each command the list of current objects is printed. Your program should behave in a similar fashion. Of course in a real drawing program, the paint() method would take a Graphics object as a parameter instead of the PrintStream object passed in this test driver (System.out is of type java.io.PrintStream). Also in the real drawing program, the paint method would make appropriate drawing calls to the Graphics object, instead of just some print() method calls.

Summary

  1. Create classes MyRectangle, MyOval, MyText, MyLine all of which extend Shape, which you also create.
  2. All of the new classes have a method like:
    // You can use out just like you have been using System.out
    public void paint(java.io.PrintStream out)
    {
      String s;
      // code here to give s a value
      out.print(s);
    }
    
  3. Modify DrawingDriver.java to allow for the additional commands specified above (add text, add line, modify any object etc.).

Sample Run

Here is a sample run of the provided program DrawingDriver, with some Shape classes that are not provided to you.
[Homepage] | [General Lab Info] | [TA's & Tutors] | [FAQ's] | [Homework] | [Excellent Programs] | [Exams]

This page maintained by Charlie McDowell. Email regarding this site.