Chapter 5 Sample Development DrawShapes
Sample Development: Drawing Shapes Write an application that simulates a screensaver by drawing various geometric shapes in different colors. The user has an option of choosing a type (ellipse or rectangle), color, and movement (stationary, smooth, or random).
Predefined Classes Until now, we've used predefined classes by calling their methods.
Predefined Classes, cont. –There is another kind of predefined class requires us to define helper classes for it to use. –Since the given class is already written, the helper classes need to have methods that match those the given class calls
Functionality Specification How do we know what the helper class needs to do? –Writer of the class has provided documentation specifying the necessary functionality. –There could be an abstract class with the desired methods and extend that to make DrawableShape. –There could be an interface with the desired methods.
DrawingBoard Supports the drawing of DrawableShape objects –fixed position –smooth trajectory –random positions
DrawingBoard DrawingBoard methods take DrawableShape objects as parameters. DrawingBoard expects to be able to draw the following set of methods for a DrawableShape: –public void draw (java.awt.Graphics) –public java.awt.Point getCenterPoint() –public java.awt.Dimension getDimension () –public void setCenterPoint (java.awt.Point)
Drawing Shapes Program flow: 1.Get the shape the user wants to draw. 2.Get the color of the chosen shape. 3.Get the type of movement the user wants to use. 4.Start the drawing.
Drawing Shapes Program implementation: 1.Start with a program skeleton. Explore the DrawingBoard class. 2.Define an experimental DrawableShape class that draws a dummy shape. 3.Add code to allow the user to select a shape. Extend the DrawableShape and other classes as necessary. 4.Add code to allow the user to specify the color. Extend the DrawableShape and other classes as necessary. 5.Add code to allow the user to specify the motion type. Extend the DrawableShape and other classes as necessary. 6.Finalize the code by tying up loose ends.
Class Diagram for Ch5DrawShape
Drawing Shapes Step 1 Development: Program Skeleton –Use the DrawingBoard class to establish a launch pad for the development. –Keep the code simple at this stage: Make the DrawingBoard visible on the screen.
Drawing Shapes Step 2 Development: Draw a Shape Define the DrawableShape class with the specified set of methods: –public void draw (java.awt.Graphics) –public java.awt.Point getCenterPoint() –public java.awt.Dimension getDimension () –public void setCenterPoint (java.awt.Point)
Minimal DrawableShape Create a minimal class for DrawableShape Single instance variable is Point representing center of shape Shape drawn will be a circle of radius 100
Drawing Shapes Step 3 Development: Allow the User to Select a Shape –Use JOptionPane to allow the user to input a choice numerically, and to establish the height and width of the shape. –Modify the DrawableShape class so it can draw three different geometric shapes.
Drawing Shapes Step 4 Development: Allow the User to Select a Color –Adopt the same input style for accepting the shape in Step 3. –Add a method named inputColor to the Ch5DrawShape class.
Drawing Shapes Step 5 Development: Allow the user to select a motion type. –Adopt the same design as used in Steps 3 and 4 for the motion selection.
Drawing Shapes No changes are required for the DrawableShape class, as the DrawingBoard class is the one responsible for shape movement.
Drawing Shapes Step 6 Development: Finalize –Perform a critical program review, looking for unfinished methods, inconsistency or error in the methods, missing comments, etc. –Make extensions to the program if desired.