INTRODUCTION TO
PROGRAMMING IN
C++
Lab:
Adding Graphics to the Aquarium Project
Alyce Brady
Kalamazoo College
Part I: Create an Aquarium using graphics.*
- Edit the ABhouse program you created in the
Pre-Lab Assignment.
- Modify the main function to draw an aquarium rather
than a house. The aquarium should have visible left and right walls and
a visible bottom, but no line across the top.
HINT: Modify the house body to become the water in the aquarium. You
may use DEEPSKYBLUE for the water color, or you can test various
colors from the colors.h file in the CMU graphics library.
Use the roof lines of the house as a template for drawing the three
walls of the aquarium. You can remove all references to the doors and
windows, but be sure NOT to remove the call to function
WaitNClear at the bottom of main.
You can also remove any references to the midpoint of the house or
aquarium, since you no longer need this.
You may wish to run the program after each substantive change you make
to see (and correct) the results, rather than wait until you have made
many changes to discover that the program won't even compile.
Part II: Switch to a graphical display in the Aquarium
program.+
- Edit the
display.h file in the Aquarium program. Modify the file to:
- Uncomment the #include statement for the CMU graphics
library.
- Move the comments around the constructors so that the default
constructor (the one with no parameters) is commented out and the
constructor that knows about a graphics window is not
commented out.
- Uncomment the lines that represent, according to the comment above
them, "private data members used for graphical display only." You will
explore what these lines do in a later lab.
Save your changes to display.h.
- Edit the file containing
the main function. Modify the file to:
- Add a #include statement to include waitnclear.h.
- Construct a graphics window object (just as you did in your program
in Part I) before the statements that construct the aquarium and the display.
- Review the change to the Display constructor in your
display.h file. Change the way you create the
Display variable in main to correspond to the
constructor declaration in the interface.
- Set the window title and adjust the "WaitClose" property of the window
(just as you did in your program in Part I).
- Add a call to WaitNClear after each set of display
statements in main.
(You no longer need to print an extra newline after displaying the initial
configuration of fish.)
- Compile and run your program using grdisplay.cpp rather than
display.cpp.
The file grdisplay.cpp.
contains the graphics-based implementation of the Display
class.
- Test your changes to the Aquarium demonstration program.
You should see the three fish move on an empty white
background. (You may sometimes see fewer fish if two or more fish were
created in the same location.)
Part III: Add a graphical aquarium to the Aquarium program.+
- The obvious place to add the graphics code for displaying the
aquarium is in the ShowAquarium function in the
Display class.
We have not yet seen how to implement class member functions, however,
so for now we will write the code to display the aquarium as client
code in a free function, one that is not tied to any
particular object or class. A skeleton version of this free function,
called TempShowAquarium (because we will remove it in a later
lab), can be found at the very bottom of grdisplay.cpp. You
will be adding a modified version of the code produced in Part I
of this lab to this function.
- Edit the grdisplay.cpp file. Scroll
down to the bottom, where you will find the skeleton implementation of the
TempShowAquarium function.
- Copy and paste the code you wrote in Part I of this lab for
drawing the aquarium water and walls (but not the code for creating the
window or calling WaitNClear) into TempShowAquarium.
- Note that the names of the values available to you are slightly
different. More importantly, you have not been given the pixel width and
height of the Aquarium as constants.
Research the interface for
Aquarium to discover how to
determine the height and width of an aquarium. These are not
the pixel width and height of an aquarium when displayed, however.
The pixel width of the
graphical display of the Aquarium should be proportional to the
width of the Aquarium, not exactly the same as it.
You should use local variables provided in
TempShowAquarium, in conjunction with the
width and height of the aquarium, to calculate aquaRight and
aquaBottom.
- Adjust the names of the arguments to DrawRectangle and
DrawLine to use the actual variables available to you in the
TempShowAquarium function.
- Research the Display interface to
determine how to display an aquarium. In your main function,
add a command that displays the aquarium at the beginning of each
sequence of fish display statements.
- Now compile and run the program and make sure that it works.
Print and save your modifications.
*The CMU Graphics Library was created by the Carnegie Mellon
University School of Computer Science and is used in their introductory
programming courses. It is available from
Mark Stehlik's
Advanced Placement page.
+The Aquarium series of labs is loosely based on the
Advanced Placement Computer Science Marine
Biology Simulation Case Study,
available from the College Board for face-to-face teaching purposes.
Copyright Alyce Faulstich Brady, 2000.