Faculty of Science, Engineering and Technology Object Oriented Programming Credit Task 5.2: Drawing Program - Saving Drawings Overview Drawing programs have a natural affinity with object oriented design and programming, with easy to see roles and functionality. In this task you will start to create an object oriented draw- ing program. Purpose: See how to use inheritance to create families of related objects and interact with them as a group (polymorphism) Task: Extend the shape drawing program to provide multiple different kinds of shapes. Time: Aim to complete this task by the start of week 5 Resources: Submission Details You must submit the following files to Doubtfire: · Program source code · Screenshot of program execution SWIN BUR NE SWINBURNE UNIVERSITY OF TECHNOLOGY
Credit Task 5.2 - Drawing Program: Saving Drawings Object Oriented Programming Instructions This task continues the Shape Drawer from the previous topic, adding the ability to save and load the drawings. Text-based File IO is similar to reading and writing values from the Terminal, but involves the extra steps for opening and closing the file. The strategy for saving a Drawing will involve a Save method in the Drawing class, which will then use a SaveTo method from the Shape class to allow the different shapes to write their values to the file. 1. Open your Shape Drawing solution. 2. Switch to the Drawing class. 3. Add a new Save method using the following pseudocode. Method Save Parameters : - filename: String path to the file to save! Local Variables - writer: StreamWriter - s: Shape 1: Assign writer, a new StreamWriter passing in filename ! 2: Tell writer to WriteLine, passing in Background. ToArgb () ! 3: Tell writer to WriteLine, passing in ShapeCount! ! 4: For each Shape s in _shapes! 5 : Tell s to SaveTo writer! 6: Tell writer to Close This code will output the background color as an integer, which can then be used to recreate the color using Color's FromArgb function when the file is loaded. Following this it outputs the number of shapes, so that when the file is read back in we can use that number to determine how many shapes need to be read in. Note: You will need to use the System.IO namespace. Page [ 2of DS
Object Oriented Programming Credit Task 5.2 - Drawing Program: Saving Drawings 4. Switch to the Shape class, and add a virtual SaveTo method. This method will save the details from Shape to file, but will be overridden by child classes to add extra details. Virtual Method: SaveTo Parameters : - writer: The StreamWriter to save to! 1: Tell writer to WriteLine, passing in Color. ToArgb () ! 2: Tell writer to WriteLine, passing in X! 3: Tell writer to WriteLine, passing in Y Note: You could also save the selected property. In this case we have left it out so it will default to false when shapes are loaded. 5. Switch to the Rectangle class, and override the SaveTo