Complete Guide to Using graphics.h in C Programming

Complete Guide to Using graphics.h in C Programming

In this tutorial, we will learn how to use the graphics.h library in C programming to create graphics and drawings. graphics.h is a part of Turbo C++ and allows you to create simple 2D graphics in C programs.

Table of Contents

1. Introduction to graphics.h

The graphics.h library is used for drawing graphics on the screen. It contains a set of functions to create graphics, handle mouse events, and work with graphical modes. Although the library was originally available in Turbo C++ (an older IDE), it is still useful for learning graphics programming, especially for beginners.

Key Features of graphics.h:

  • Draw lines, circles, rectangles, and other shapes
  • Use different colors and styles
  • Handle mouse events and user input
  • Change the resolution of the graphical window

2. Setting Up the Development Environment

To use graphics.h in C programming, we need to set up an appropriate environment.

Option 1: Using Turbo C++

If you are using Turbo C++:

  1. Install Turbo C++ (older versions).
  2. Create a new C project and start coding with graphics.h.

Option 2: Using Modern IDEs

If you are using a modern IDE (like Code::Blocks or Dev-C++), you will need to set up the graphics library manually:

  1. Download the WinBGI graphics library (available on GitHub or other sources).
  2. Place the graphics.h and winbgim.h files in the include folder.
  3. Copy libbgi.a to the lib folder.
  4. Link the graphics library to your project:
    • Open your project in Code::Blocks.
    • Go to Project > Build options > Linker settings.
    • Add libbgi.a in the Link Libraries section.
    • Add the path to your graphics library in Search directories > Linker.

3. Basic Graphics Functions

Here are some basic functions that you will need to start drawing graphics with graphics.h.

a. Initializing Graphics Mode:

#include <graphics.h>
#include <conio.h>

int main() {
    // Initialize the graphics system
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\TurboC3\\BGI");

    // Your graphics code goes here...

    // Close the graphics window
    getch();
    closegraph();
    return 0;
}

initgraph(&gd, &gm, "path") initializes the graphics mode. The DETECT driver auto-detects the best graphics mode.

"C:\\TurboC3\\BGI" is the path to the BGI folder, which contains the driver files. Modify this path as needed.

4. Drawing Simple Shapes

a. Drawing a Line

line(100, 100, 300, 300);

This draws a line from point (100, 100) to (300, 300).

b. Drawing a Circle

circle(200, 200, 50);

This draws a circle with center (200, 200) and a radius of 50.

c. Drawing a Rectangle

rectangle(100, 100, 300, 200);

This draws a rectangle with top-left corner (100, 100) and bottom-right corner (300, 200).

5. Changing Colors and Styles

a. Changing Line Color

setcolor(RED);
line(100, 100, 300, 300);  // Red line

b. Changing Line Style

setlinestyle(SolidLine, 0, 3);  // Solid line with width 3

Available styles:

  • SolidLine
  • DottedLine
  • DashedLine
  • DoubleDashLine

c. Filling Shapes

setfillstyle(SolidFill, BLUE);
floodfill(200, 200, RED);  // Fill the shape with blue color

6. Handling the Graphics Window

a. Clearing the Screen

cleardevice();

b. Closing the Graphics Window

closegraph();

7. Conclusion

In this tutorial, we covered the basics of using the graphics.h library in C. While graphics.h is an old library, it is still a great starting point for learning graphics programming. With functions like line(), circle(), and rectangle(), you can create a variety of 2D shapes, manipulate their colors, and even fill them with patterns.

You can experiment with different shapes, colors, and styles to create more complex graphics. Happy coding!

References

SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment