Apache web server tutorial - 534 Object-Oriented Programming Chapter 9 Class MyShape in
534 Object-Oriented Programming Chapter 9 Class MyShape in Fig. 9.45 must be abstract. The only data representing the coordinates of the shapes in the hierarchy should be defined in class MyShape. Lines, rectangles and ovals can all be drawn if you know two points in space. Lines require x1, y1, x2 and y2 coordinates. The drawLine method of the Graphics class will connect the two points supplied with a line. If you have the same four coordinate values (x1, y1, x2 and y2) for ovals and rectangles, you can calculate the four arguments needed to draw them. Each requires an upper-left x-coordinate value (minimum of the two x-coordinate values), an upper-left y-coordinate value (minimum of the two y coordinate values), a width (difference between the two x-coordinate values; must be nonnegative) and a height (difference between the two y-coordinate values; must be nonnegative). [Note: In Chapter 12, each x,y pair will be captured by using mouse events from mouse interactions between the user and the program s background. These coordinates will be stored in an appropriate shape object as selected by the user. As you begin the exercise, you will use random coordinate values as arguments to the constructor.] In addition to the data for the hierarchy, class MyShape should define at least the following methods: a) A constructor with no arguments that sets the coordinates to 0. b) A constructor with arguments that sets the coordinates to the supplied values. c) Set methods for each individual piece of data that allow the programmer to independently set any piece of data for a shape in the hierarchy (e.g., if you have an instance variable x1, you should have a method setX1). d) Get methods for each individual piece of data that allow the programmer to independent ly retrieve any piece of data for a shape in the hierarchy (e.g., if you have an instance vari able x1, you should have a method getX1). e) The abstractmethod public abstract void draw( Graphics g ); This method will be called from the program s paintmethod to draw a shape onto the screen. The preceding methods are required. If you would like to provide more methods for flexibility, please do so. However, be sure that any method you define in this class is a method that would be used by all shapes in the hierarchy. All data must be private to class MyShape in this exercise (this forces you to use proper encapsulation of the data and provide proper set/get methods to manipulate the data). You are not allowed to define new data that can be derived from existing information. As explained previously, the upper-left x, upper-left y, width and height needed to draw an oval or rectangle can be calculated if you already know two points in space. All subclasses of MyShape should provide two constructors that mimic those provided by class MyShape. Objects of the MyOval and MyRectclasses should not calculate their upper-left x-coordinate, upper-left y-coordinate, width and height until they are about to draw. Never modify the x1, y1, x2 and y2 coordinates of a MyOvalor MyRect object to prepare to draw them. Instead, use the temporary results of the calculations described above. This will help us enhance the program in Chapter 12 by allowing the user to select each shape s coordinates with the mouse. There should be no MyLine, MyOvalor MyRect references in the program only MyShape references that refer to MyLine, MyOval and MyRect objects are allowed. The program should keep an array of MyShape references containing all shapes. The program s paint method should walk through the array of MyShape references and draw every shape (i.e., call every shape s draw method). Begin by defining class MyShape, class MyLine and an application to test your classes. The application should have a MyShape instance variable that can refer to one MyLine object (created Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01