INTRODUCTION TO
PROGRAMMING IN
C++
Kalamazoo
College
LAB:
ADDING
GRAPHICS TO THE
AQUARIUM
PROJECT
Create an Aquarium using graphics.*
- Copy the Graphics Demo project you created in the
Lab Entrance Assignment
and the AquaFish folder (from the
Creating Fish
in An Aquarium Mini-Lab)
from your floppy to the Temp folder on the C: drive. Open the
Graphics Demo project (graphics-prj).
- Double-click on ABhouse.cpp in the project window to open
it for editing.
Immediately do a Save As (under the File menu) to create a new file in
which you will draw an aquarium instead of a house. Make sure that the
name of your new file ends in ".cpp", for example "newfile.cpp".
Notice that the
project will now list your new file rather than ABhouse.cpp.
You can run the project, if you like, to see that CodeWarrior will now
compile the project with the new file. Since the new file is still
identical to ABhouse.cpp, you should get the same results as
before.
- Modify your new 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.
- You may wish to print your program at this point.
Remove the object code from the project (there is an option under the
Project Menu to do this), so that you will be able to copy the folder
to your floppy later.
Do NOT, however, remove the Graphics Demo folder from
the Temp folder on the C: drive yet, as you will be using it again.
Switch to a graphical display in the Aquarium
program.+
- Open the "AquaGraphics" project in your AquaFish folder in Temp.
- In the CodeWarrior project window, double-click on the
display.h file in the Aquarium folder. Modify the file to:
- Uncomment the #include statement for the CMU graphics
library.
- Move the comments around the constructors so that the
one-parameter constructor 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." We will
explore what these lines do in a later lab.
Save your changes to display.h.
- In the CodeWarrior project window, double-click on the file containing
the main function. Modify the file to:
- Add a #include statement to include waitnclear.h.
- Construct a graphics window object by copying and pasting the
appropriate line from the main function you created above
into your Aquarium main function
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.
- Copy and paste the lines that set the window title and adjust the
"WaitClose" property of a window
from the main function you created above
into your Aquarium main function.
- 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.)
- Note that the file called display.cpp in the project has
been replaced with a file called grdisplay.cpp. This file
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.)
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 the first part
of this lab to this function.
- Open the grdisplay.cpp file in the Aquarium folder. Scroll
down to the bottom, where you will find the skeleton implementation of the
TempShowAquarium function.
- Copy and paste the code you wrote in the first part 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 run the program (CodeWarrior will automatically do any necessary
compilation and linking) and make sure that it works.
Print and save your modifications.
- Print out your modified versions of grdisplay.cpp and the
main function.
Remove the object code from the project (there is an option under the
Project Menu to do this). You may also need to remove the executable
file(s) from both the Graphics Demo and AquaFish folders (named
GraphicsApp ..., unless you changed the name of the Target File) to fit
everything on your floppy disk. Copy the Graphics Demo and AquaFish
folders from the Temp folder to your floppy disk. You should then
remove the folders from the Temp folder on the C: drive.
- Be sure to turn in the printouts of the modified grdisplay.cpp
file and your modified main function.
*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.