How to Create a C++ Graphics Application
This handout was originally written by Professor Wittman. Note that your version of Visual
studio might differ slightly from the one used in this handout. Any differences should be
minor.
Step 1 Download the graphics libraries.
The ccc files written by Cay Horstmann are available on our website:
http://www.math.ucla.edu/~virtanen/10a.2.13w/labs/lab2/ccc_graphics.zip
Download the ccc_graphics.zip file to a convenient place, like the Desktop.
Step 2 Unzip ccc_graphics.zip.
Right-click on the file you just downloaded and select “Extract to...” You can save them
in the same place (Desktop is a good place), we’ll move them later anyways. You should get 5
files:
ccc_win.h ccc_msw.cpp ccc_msw.h
ccc_shap.cpp ccc_shap.h
In our code, we only include the file “ccc_win.h”. But ccc_win uses the other files, so we
have to move all 5 files as a group.
Step 3 Create a new windows project.
• As usual, start up Visual Studio and under the "File" menu click “New Project”. On the
first screen that appears, select the category “Win 32” on the left and “Win32 Project”
on the right. (Not Console Project!)
• Give the project a name, like “graphics”. Select an easy location to save it, like the Z:
drive.
• Click “OK”.• On the next screen, click “Application Settings”. Then check the box “Empty Project”,
as we usually do. Select “Windows application”. (Not console application!) Click
“Finish”.Step 4 Set the character code.
In the Solution Explorer, right-click on the project icon (second from the top). In the pop-up
menu, select "Properties".
In the window that pops up, select "Configuration Properties => General". Select the
"Character Set" property by clicking on it and change the value to "Not Set".Step 5 Create a new source file.
In the Solution Explorer menu on the side, right-click on “Source Files” folder. In the pop-up
menu, select “Add => New Item...” Select “C++ file (.cpp)” and give your cpp file a good
name, such as graphics1. Click "Add".
Step 6 Move the ccc files to the same directory as your source file.
Step out of Visual Studio for a moment. Now locate the 5 ccc files you unzipped and
move (or copy) all 5 files to the same directory as the cpp file you just created. This
way, Visual Studio knows exactly where to look for the new library. Go back to Visual
Studio.
Step 7 Add the ccc files to your Header Files.
In the Solution Explorer on the side, right-click on “Header Files”. Select “Add => Add
Existing Item”. (The libraries are already written for us, so we’re not adding a new item.)
Select all 5 ccc files and click “Add”.
You are now ready to start typing in your C++ code. Compile and run your programs as
before. You should repeat these steps for every graphics application you create,
including HW3. In particular, you have to put copies of the 5 ccc files into each folder
that contains a graphics cpp file.Exercise 1: As a test, type the following text into your source file and compile.
#include "ccc_win.h"
int ccc_win_main ( )
{
cwin << Message( Point(-1,0), "Mean People");
cwin << Circle( Point(0,0) , 4.2 );
cwin << Line( Point(-3,3) , Point(3,-3) );
return 0;
}
Exercise 2: Draw a house. To help you plan, I’ve included some graph paper with the
default viewing window -10<x<10, -10<y<10. You can change any details of your house
(windows, doors, roof). The only requirement is that your house must have a chimney
with top at (5,5).Exercise 3: Modify your code for Exercise 2. Insert the following code right above the
final “return 0;” statement.
for (int i=1; i < 20; i++)
{
double r = (double) rand() / RAND_MAX - 0.5;
cwin << Circle ( Point(5+r , 5+0.2*i ), 0.2);
}
cwin.get_mouse("Click anywhere");
cwin.clear( );
ccc_win_main ( );
Click repeatedly in the screen to animate.