Activity 12-2 - Working with Graphics - The use of a Graphics Library


 

Introduction

The purpose of this lab exercise is to practice with classes. To make the program more interesting graphics will be used. In order to write a program that uses graphics you must have access to a graphics library. An educational graphics library is available for Visual Studio, an IDE (Intergrated Development Environment) software application for software development. Many languages are supported in such environment. C++ is one of them. Therefore, the next program you will write will be created in Visual Studo and ran on your local machine instead of loki.

 

First things, first.

Let's become familiar with the graphics and the Visual Studio environment. An educational Graphics library, called EZwindows, has been chosen for this purpose.

In this part of the lab you will get acquainted to Visual Studio, you will take an existing code and you will modify it to improve it. In the post_lab you will write your brand new code. Before we start working with Visual Studio, download the Lab12Material on your machine. The folder contains contains the Lawn.cpp file that you will modify later and the folder EzwinvcFiles which contains the library Ezwinvc50.lib together all the *.h files that will be necessary for this lab. Unzip the file and keep track of the path where you have saved the folder.

 

Visual Studio

length of the lawn: 600
width of the lawn: 600
length of the house: 300
width of the house: 300
length of the garage: 150
width of the garage: 100

The display window will open that draws the lawn, the house and the garage. Move the display window if it covers the console window so that you can read the resulting cost of mowing the lawn. How much does it cost to cut this lawn?

Now go back to the code of  Lawn.cpp and start studying the code carefully and be sure you understand it well. The code is well commented. In order to follow the code, insert all the files *.h that are called. For example, Lawn.ccp includes rect.h, but rect.h includes shape.h and shape.h includes etc... For simplicity, if you prefer, you can include all the *.h files you have. You do not need to read in detail every *.h file but you need to understand the code in Lawn.cpp. For exampe, the class SimpleWindow used in the Lawn.cpp is defined in the file ezwin.h. The statement SimpleWindow Display("Lawn and House Plot", DisplayLength, DisplayHeight); creates an object called Display of the class SimpleWindow, which is the class that creates a display window. The name in the top corner of the window is in double quotes, the length and the width of the display window are given by the parameters DisplayLength, DisplayHeight. To open a display window you must call the method Open() in this way: Display.Open();. An object of the class SimpleWindow is used in the constructor of the RectangleShape class (the class of rectangles defined in rect.h).

When you feel familiar with the Lawn.cpp code you can start performing some modifications of the program by following the instructions below. Perform one step at a time and check periodically that your program keeps working appropriately.

Exercise 12.2

Open the rect.h file and look at the code that describes the class RectangleShape, the class of all the rectangles. Call the file ex122.txt. List all the member functions of the class and for each function describe in one line its functionality. Do not be concerned when you see some arguments in the argument list of the methods that are preceded by the symbol &. This means that the argument preceded by the & symbol is passed by "reference". What it means is that instead of passing the value of the variable, we are passing the address where the value of the variable is. In other words we are telling where to get the value of the variable that the method needs instead of giving directly the value. However if the method changes that variable, the value of the variable will be changed in the original location and the change will be visible when the function is terminated. I know that we have not studied this yet, but we will do that soon. In the meanwhile, it is not necessary that you understand everything at the moment. It is important that you look at the names of the methods (since you may want to use them), not at the way they are implemented. That is the good thing of the OO programming...we have separation of concerns!!

 

Exercise 12.3

Write a small paragraph in plain english that describes the code of Lawn.cpp. Note that in order to create a graphical program you must use the ApiMain( ) function instead of the main( ) function. Insert it as a comment in the file Lawn.cpp. Save the file as ex123.cpp.

 

Modifications to the Lawn

You are ready to make modification to the current Lawn.cpp file. If you make modification directly in to the file you have inserted in your project you will modify the original copy. So you may want to make a copy of the original file in YourNameLawn.cpp before you start any modification.

Start a new project (in the same way you did above) and call it YourNameLawnProject. Add to the current project the file YourNameLawn.cpp. Include the *.h files in the same way you just did and be ready to modify the code. Remember that Lawn.cpp program includes the rect.h file. In the modification you will perform, you may want to use other .h files that are available in the library folder. Therefore be ready to add any .h file that you need to include in your project. The .h files must be added to Header Files Folder. You also will need to use the library that is the Ezwinvc.zip file. All the .h files and the Ezwinvc50.lib library object file are in the folder EzwinvcFiles. Your project will not run if you do not set the path for the inclusion of the *.h files as well as for the library (unless all the .h files and the object file of the library is in the same folder of the project you have created.

Exercise 12.4

For full credit be sure you follow accurately the specifications of this problem. Undesired changes of file names, of color, of measure or of other details that are requested, will be cause of points detraction. Call the program ex124.cpp.
  1. Remove the lines of code containing the computation of the time and of the pay to cut the lawn. What you will do in the next step is to prepare for the creation of a nicer drawing than the one you have obtained with the Lawn program. The purpose of your NewLawn program is to draw a better house with an attached garage and a

  2. First ask the user for the size of the lawn.

  3. Check if the user provides a lawn bigger than the window. The lawn must be green.

  4. Inside the green lawn, draw a small house. You must ask the user for the size of the house. The house cannot be larger than the lawn.

  5. Draw the house. The house can be of any color except green, red, or blue. The house has a door, two windows and a red roof. The roof is made of several rectangular little bricks. (Hint: Use a loop to draw many rectangles one after the other. If you find difficult to draw the roof in this way, just use a single rectangle. Extra credits will be given for drawing the

  6. On the left hand side of the house, there is a blue garage. The garage must be attached to the left hand side of the house. The size of the garage depends on the number of cars that will fit in it. Ask the user for the number of cars will fit in the garage. The width of the garage should be 10 times the number of cars that the garage will hold (e.g. one car = 10 feet, two cars = 20 feet).  Remember that the rectangles are drawn in cm.

  7. In front of the garage, there is a driveway (the part of the road that allows the owner to drive from the garage to the street and sits inside the lawn). The driveway is black and is as wide as the garage.

  8. Even though you are free to draw the house and the garage in the way you like, as long as you do not skip the specifications that are requested by the program, a possible nice result may look like this. Your drawing can be simpler than the one shown below.

  9. When you have completed your work, Zip the folder "YourNameLawnProject" and attach it to the e-mail that you will send to the instructor.