BCA Sem 1 | Unit 3 | Computer Concepts & C Programming
Overview of graphics.h Header File in C — BKNMU Junagadh
Introduction to Computer Graphics, Initialization, Drawing Shapes (Line, Circle, Rectangle), Colors, and Legacy GCC Issues
graphics.h header file is used to introduce **Computer Graphics** in C programming. It is a legacy header, primarily part of older Borland C++ compilers (like Turbo C), containing declarations for functions required to draw shapes, lines, and manage colors on a graphical window. It allows transforming the console's text-only display into a simple graphical output screen. This post provides a clear overview for **BKNMU BCA Semester 1 (Unit 3)**.
The graphics.h library is widely recognized for four main areas of visual interaction routines:
1. System Management
Functions required to switch the terminal from text mode to graphics mode (`initgraph`) and properly shut it down (`closegraph`).
2. Drawing Primitive
The building blocks: putpixel draws a single colored dot; line connects two (X, Y) coordinates with a straight line.
3. Closed Shapes
Functions that accept center points and radii/coordinates to instantly draw predefined geometric structures.
4. Color & Styling
Used to set the current drawing color (foreground) or the background color of the graphics window using standard color codes.
Here is a detailed breakdown of the primary functions supplied by the non-standard graphics.h library, focusing on their input requirements.
| Function Prototype | Functional Description | Standard Parameters / Coordinate System |
|---|---|---|
void initgraph(int*, int*, char*) | Initializes the graphics system and loads graphics drivers (often set to 'DETECT'). | GD driver, GM mode, Path to drivers. (X increases Right, Y increases Down from 0,0). |
void line(int, int, int, int) | Draws a straight line from the starting coordinate to the ending coordinate. | (X1, Y1, X2, Y2) Integers. |
void circle(int, int, int) | Draws a circle with the specified center and radius. | (CenterX, CenterY, Radius) Integers. |
void rectangle(int, int, int, int) | Draws a rectangle defined by its top-left and bottom-right corner coordinates. | (TopLeftX, TopLeftY, BotRightX, BotRightY) Integers. |
void putpixel(int, int, int) | Plots a single pixel at the specified coordinate in the designated color. | (X, Y, ColorCode) Integers. |
void setcolor(int) | Sets the current foreground color for all subsequent drawing functions. | Color Code (0-15 or Constants). Example: RED, BLUE, WHITE. |
1. Initializing and Drawing a Basic Circle and Line:
#include <stdio.h>
#include <graphics.h> // Required for graphics functions
#include <conio.h> // Required for getch() pause
int main() {
int gd = DETECT, gm; // Autodetect driver and mode
// 1. Initialize the system
initgraph(&gd, &gm, "C:\\TurboC3\\BGI"); // Path often required in TC
// 2. Set current color to YELLOW
setcolor(YELLOW);
// 3. Draw a circle: center (200, 200), radius 50
circle(200, 200, 50);
// 4. Draw a line: from (100, 100) to (300, 300)
line(100, 100, 300, 300);
// 5. Pause output
getch();
// 6. Close system
closegraph();
return 0;
}
Step 1: Driver Detection & Initialization
The preprocessor loads declarations. `initgraph` loads BGI (Borland Graphics Interface) drivers to physically switch the console monitor from character mode to pixel addressing.
Step 2: Attribute Setup & Primitive Calls
Attribute functions (`setcolor`) determine styling. Drawing functions (`line`, `circle`) provide mathematical instructions for specific pixel plots.
Step 3: Rendering & Closure
The screen instantly renders based on pixel instructions. You *must* call `closegraph()` to release graphics drivers and return the monitor to standard text console mode when finished.
"C:\\TurboC3\\BGI") or using incorrect backslashes often results in the error "BGI Error: Graphics not initialized." during BCA lab exams.
graphics.h header file discussed here is specific to ancient Borland Turbo C compilers and is NOT supported by modern compilers like GCC (VS Code, Code::Blocks) on Windows/Linux. Students wishing to learn modern graphics must use advanced libraries like SDL, OpenGL, or special legacy GCC emulation headers (WinBGIm).
- Full Header Name: Computer Graphics Header (
graphics.h). - Header Type: Non-standard / Legacy header file (specific to Borland Turbo C).
- Core Function Sequence: Mandatory `initgraph()` start, mandatory `closegraph()` end.
- Key Primitive Functions: `putpixel()`, `line()`, `circle()`, `rectangle()`.
- Coordinate System Origin (0,0): Strictly at the Top-Left corner of the screen.
- Primary Parameters: Integers (Coordinates X, Coordinates Y, Radius, ColorCode).
- Portability fallback: Modern GCC requires emulation via WinBGIm to run these legacy examples.