How To Writing and Compiling a Basic C++ Graphics Program
Writing a graphics program in C++ is almost identical to writing any other type of program (see the C++ tutorial), but there are a few things you need to include in the program to load the graphics commands.
When you start writing a program including graphics, you must:
- Start the first line of the code with #include to load these commands.
- The next line of code should be#include. This provides functions specific to the X Windowing System.
- You must declare an integer variable to hold the "serial number" of your graphics window, such as int window; The various graphics commands use this number to know which window to write to.
- Create a graphics window with the line window = g2_open_X11(width, height); where width and height can be variables or numbers, and window is an integer variable.
- At the end of your program, close with window with g2_close(window);
#include <iostream.h>
#include <g2++.h>
#include <g2_X11++.h>
const int HEIGHT= 200;
const int WIDTH = 200;
int main(void)
{
int window;
/* Open a window on screen. Keep it's device ID */
window = g2_open_X11(WIDTH, HEIGHT);
/* Let's define some colors now */
int black_ink = g2_ink(window,0,0,0);
int blue_ink = g2_ink(window,0,0,1);
int yellow_ink = g2_ink(window, 0.7,0.7,0);
int white_ink = g2_ink(window,1,1,1);
/* clear screen */
g2_clear(window);
g2_set_background(window, black_ink);
/* Give it a face */
g2_pen(window,yellow_ink);
g2_filled_circle(window,100,100,50);
/* and eyes */
g2_pen(window,white_ink);
g2_filled_ellipse(window,85,115,9,6);
g2_filled_ellipse(window,115,115,9,6);
g2_pen(window,blue_ink);
g2_filled_circle(window,83,115,5);
g2_filled_circle(window, 113,115,5);
g2_pen(window,black_ink);
g2_filled_circle(window,83,115,3);
g2_filled_circle(window,113,115,3);
/* and a nose */
g2_pen(window,black_ink);
g2_line(window,102,105,95,88);
g2_line(window,95,88,102,88);
/* and a mouth */
g2_line(window,90,80,110,80);
cin.get();
g2_close(window);
return 0;
}
For a description of many of the graphics commands you can use, look at the g2 graphics library documentation
Compiling a Graphics Program
Now, to compile the program, open a terminal window. While you are in the directory containing your program, type at the command prompt:
g++ -o program1 program1.cpp -L/usr/X11R6/lib -lm -lX11 -lgd -lg2
This tells g++, the compiler, to look in the graphics library when compiling your program in order to find the commands. Running a graphics program is exactly like running any other kind of program; just type the program name at the command prompt.

- CS Mini Courses
- Compiling in Linux
- Writing Graphics
- Beep Program
- Makefiles
- Keyboard Interrupts
- Running programs in the background
- Scanner class
- Java Documentation
- Setting your Java CLASSPATH variable
- Installing Java 1.6 on your home computer
- Installing Python 2.5.x On Your Home Computer
- PIL (Python Imaging Library)
- Installing Python 2.6 and/or Python 3.0 on your home computer
- Installing Cygwin







