CS127
Due 9:50AM Monday, 1/19/04, via HSP

Shapes

For this assignment, you will write a program that will read a text file containing descriptions of a bunch of shapes, and then open a window displaying those shapes.

For example, suppose your text file contains the following:


circle
255 0 0
100 200 60
triangle
0 255 0
300 450 300 200 500 450
rectangle
180 0 180
100 400 80 120

For each shape, the first line identifies the type of shape, the second line describes the RGB (red/green/blue) color of the shape, and the third line contains shape-specific information. If your program encountered this example, it would need to display a red circle with radius 60 and centered at (100,200), a green triangle with vertices (300,450), (300,200), and (500,450), and a purple rectangle with upper left corner (100,400), width 80, and height 120. In addition, each shape's area should be displayed in the center (more or less) of the shape.

There are many ways to write such a program, of course. For this assignment, you're going to use an existing Shape class in Shape127.java, and create three subclasses of Shape127: Circle127, Triangle127, and Rectangle127. (There are already Shape and Rectangle classes in the standard Java library, so we'll just stick "127" onto the class names to avoid conflict.) Your three classes will need to implement the load, draw, and getArea < methods of the Shape127 class.

You will also need to create a ShapeWindow class. Make it a subclass of JFrame containing a Vector of references to Shape127 objects. You will need to implement a constructor, the paint method, and some sort of method for adding new shapes to the vector. You should also implement a main method, which will:

  1. Instantiate a ShapeWindow.

  2. Open the file named as the first command-line argument.

  3. Read shapes from the file until the file is empty, like so:

You may assume that the text file is correctly formatted.

Advice

In many ways, this is a very simple program. On the other hand, the devil will definitely be in the details. As you work on this assignment, you will confront Java's somewhat baroque file-reading system, exceptions, number-to-string conversion, string-to-number conversion, and so on.

To manage the complexity of the task, you should start by making an incremental development plan. For example, the first version of your program might simply open the specified file and print its contents to standard output. The second version could include a loop that runs through the shapes in the file, printing only the shape-name line and terminating at the end of input. The third could parse the color integers into separate int variables, and then print those out. This kind of planned sequence of small steps, executed with discipline, can make a big job manageable. After each step, make sure to compile, test, and make backup copies of your code.

One more thing: get started before Friday so you can bring questions to class.

Have fun.