Chapter 9 (Tomcat web server) Object-Oriented Programming 535 in the application s
Chapter 9 Object-Oriented Programming 535 in the application s constructor). The paint method (for your subclass of JFrame) should draw the shape with a statement like currentShape.draw( g ); where currentShape is the MyShape reference and g is the Graphics object that the shape will use to draw itself on the background of the window. 9.29 Next, change the single MyShapereference into an array of MyShapereferences and hard code several MyLineobjects into the program for drawing. The application s paintmethod should walk through the array of shapes and draw every shape. After the preceding part is working, you should define the MyOval and MyRect classes and add objects of these classes into the existing array. For now, all the shape objects should be created in the constructor for your subclass of JFrame. In Chapter 12, we will create the objects when the user chooses a shape and begins drawing it with the mouse. In Exercise 9.28, you defined a MyShape hierarchy in which classes MyLine, MyOval and MyRectsubclass MyShape directly. If the hierarchy was properly designed, you should be able to see the tremendous similarities between the MyOval and MyRect classes. Redesign and reimplement the code for the MyOval and MyRect classes to factor out the common features into the abstractclass MyBoundedShapeto produce the hierarchy in Fig. 9.46. Class MyBoundedShape should define two constructors that mimic the constructors of class MyShapeand methods that calculate the upper-left x-coordinate, upper-left y-coordinate, width and height. No new data pertaining to the dimensions of the shapes should be defined in this class. Remember, the values needed to draw an oval or rectangle can be calculated from two (x,y) coordinates. If designed properly, the new MyOval and MyRect classes should each have two constructors and a drawmethod. java.lang.Object MyShapeMyLine MyBoundedShapeMyRectMyOval Fig. 9.46 The MyShapehierarchy. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01