How to Create Drawing Application by C Programing Language

drawing or painting program using C or C++ graphics programming language

Introduction :

Creating a drawing or painting application using the C or C++ graphics programming language is a fascinating, exciting and challenging task or project in the programming world. Using C or C++ graphics programming language, you can develop a drawing or painting program that enables users to unleash their artistic creativity on a digital canvas.

To design and manipulate graphical elements, you need to require handling graphics using graphics.h library of Turbo C++ IDE and implementing event handling using mouse pointer or cursor. By utilizing the event handling mechanism of mouse pointer, you have to capture mouse movements and button clicks performed by the user. This will allow users to interact with the application for updating the drawing area in real-time. In this article, I shall explore you how build a simple drawing or painting application by C or C++ graphics programming language. Here, I create a drawing or painting program, using C or C++ graphics programming language with source code.

About the program :

In this program, user can draw anything with different color on the drawing area as freehand drawing using the mouse pointer. Here, I create some basic drawing tools such as lines, circles, rectangles, triangles, eraser, different color box and fill color box in the drawing ground. Using these drawing tools, the user can draw lines, circles, rectangles, triangles with different color on the painting area of the digital canvas. You can also fill the circles, rectangles and triangles with different color.

After running the program, you see a drawing or a painting canvas with blue color. In the graphics window, there are some horizontal boxes in the upper most portion with different color and a box with “eraser”. To change the color of the drawing, you have to just click on any colored box as you like. After that, users click and drag the mouse pointer to draw anything like a pencil on the drawing area with selected color. If you want to erase some drawing, just click on “eraser” box and click and drag the mouse pointer for erase the drawing where you create a mistake.

There are some vertical boxes in the left portion of the graphics window with various shapes like line, circles, rectangles, triangles etc. and a box with “color”. If you want to draw different shapes, just click on any shaped box as you like. Now click and drag the mouse pointer on the painting area for draw selected shape on the canvas. There is a box with text “color” in the left portion which is very important and also have some problem. You have to use it with very carefully. It fills the color of a shape which you draw on the canvas.

To use it in the program, first click on any color then click any shape which you want to draw with the fill color. After that, draw the shape using mouse pointer on the screen. Now click on the color box and further click on the shape you just draw on the drawing place. Now you see the empty shape will be filled with your selected color. In this process, you can draw more than one shape with the fill color on the graphics screen otherwise you get an error with the fill color. So take care when you draw filled shapes. After drawing or painting artistic possibilities on the canvas, let your imagination to life.

Note that, The program has some problem when you fill the shape with color. You can solve the problem. Please forgive me for my mistake. Here, I do not implement the saving functionality so you can try to save the graphics screen after draw something.

Explanation of the program :

Initializing the graphics window and event handling mechanism of mouse pointer, you can create a drawing and painting application on your pc. At the beginning of the program, you have to include necessary library or header file such as graphics.h, stdlib.h, conio.h and dos.h in the program. The graphics.h library is used for various graphical functions. The abs() and kbhit() functions are defined in stdlib.h and conio.h library respectively. The dos.h library is used to handle the mouse pointer. The union type of REGS variables, delay() and int86() functions are defined in dos.h library.

After that, declare union type of REGS variables regs_in and regs_out. Then, declare necessary variables for the program. Using initgraph() function initializes the graphics mode. The setbkcolor() is used to set the background color of the graphics window and setcolor() for color of different shapes. To create various styles and color of the different shapes, you can use setfillstyle() function. Next, you can create drawing ground and horizontal and vertical boxes using bar() function.

The settextstyle() and outtextxy() use to write text in the drawing window such as “erase” and “color” in the boxes. In the vertical boxes, draw line, circle, rectangle etc. using the line(), circle() and rectangle() functions. Now draw a rectangle for drawing area where users can draw anything. After drawing place is completed, initialize the mouse pointer to set ax to 0 and call the int86() function and display mouse set ax to 1. Next, restrict the drawing area using ax to 7 and 8. The getmaxx() and getmaxy() functions are used for get maximum x and y coordinate of the graphics window.

In the while loop check any key press or not by kbhit() function. Here, set ax to 3 for get the mouse position and mouse click. Using mouse click and movement, initialize the different value of various variables in the “if” statement. According the different value and movement of the mouse pointer, you can draw as you like in the drawing area. In the program, I also use setfillstyle() and floodfill() function for fill the shapes with color. The delay(), ellipse() and putpixel() are also used in the program. Finally, close the graphics mode using closegraph() function.

If you want to know how to control mouse pointer or cursor using C or C++ programming language, just follow my link.

How run the program :

At first, you have to install the Turbo C++ IDE on your pc. Then, open the Turbo C++ IDE on your pc and create a C or C++ file with .c or .cpp extension. After that, copy the below source code of a drawing or painting program by C or C++ graphics programming language and paste in your C or C++ file in the Turbo C++ IDE. If you do not know how to copy paste in the Turbo C++ IDE, just follow my link. You have to also know how to use graphics.h in Turbo C++ IDE.

Source code :

The following code is the source code of a drawing or painting program using the C or C++ graphics programming language.

/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
int main(void)
{
    union REGS regs_in, regs_out;
    int graphic_driver = DETECT, graphic_mode;
    int x = 0, y = 0, i = 1, j = 4, click = 0;
    int k = 0, l = 0, x1, x2, y1, y2, m = 1, x3, y3, p = 4, x4, y4, a = 0;
    initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
    setbkcolor(0);
    setcolor(4);
    setfillstyle(1, 1);
    bar(100, 100, getmaxx(), getmaxy());
    setfillstyle(1, 15);
    bar(600, 110, 638, 140);
    settextstyle(1, 0, 1);
    outtextxy(600, 110, "erase");
    for (i = 2; i <= 15; i++)
    {
        setfillstyle(1, i);
        bar(110 + k, 110, 140 + k, 140);
        k += 35;
    }
    for (i = 0; i <= 6; i++)
    {
        setfillstyle(1, 15);
        bar(100, 150 + l, 140, 190 + l);
        l += 45;
    }
    settextstyle(1, 0, 1);
    outtextxy(100, 420, "color");
    line(100, 190, 140, 150);
    circle(120, 215, 15);
    rectangle(105, 245, 135, 275);
    line(105, 320, 135, 320);
    line(105, 320, 120, 290);
    line(120, 290, 135, 320);
    line(105, 365, 135, 365);
    line(105, 335, 105, 365);
    line(105, 335, 135, 365);
    line(105, 405, 130, 405);
    line(110, 380, 135, 380);
    line(110, 380, 105, 405);
    line(130, 405, 135, 380);
    rectangle(150, 150, getmaxx() - 1, getmaxy() - 1);
    regs_in.x.ax = 0;
    int86(0x33, &regs_in, &regs_out);
    int86(0x33, &regs_in, &regs_out);
    regs_in.x.ax = 1;
    int86(0x33, &regs_in, &regs_out);
    regs_in.x.ax = 7;
    regs_in.x.cx = 100;
    regs_in.x.dx = getmaxx();
    int86(0x33, &regs_in, &regs_out);
    regs_in.x.ax = 8;
    regs_in.x.cx = 100;
    regs_in.x.dx = getmaxy();
    int86(0x33, &regs_in, &regs_out);
    while (!kbhit())
    {
        regs_in.x.ax = 3;
        int86(0x33, &regs_in, &regs_out);
        click = regs_out.x.bx;
        x = regs_out.x.cx;
        y = regs_out.x.dx;
        if ((x >= 110 && x <= 140) && (y >= 110 && y <= 140) && click == 1)
        {
            m = 1;
            j = 2;
            p = 2;
            a = 1;
        }
        if ((x >= 145 && x <= 175) && (y >= 110 && y <= 140) && click == 1)
        {
            j = 3;
            m = 1;
            p = 3;
            a = 1;
        }
        if ((x >= 180 && x <= 210) && (y >= 110 && y <= 140) && click == 1)
        {
            j = 4;
            m = 1;
            p = 4;
            a = 1;
        }
        if ((x >= 215 && x <= 245) && (y >= 110 && y <= 140) && click == 1)
        {
            m = 1;
            j = 5;
            p = 5;
            a = 1;
        }
        if ((x >= 250 && x <= 280) && (y >= 110 && y <= 140) && click == 1)
        {
            j = 6;
            m = 1;
            p = 6;
            a = 1;
        }
        if ((x >= 285 && x <= 315) && (y >= 110 && y <= 140) && click == 1)
        {
            m = 1;
            j = 7;
            p = 7;
            a = 1;
        }
        if ((x >= 320 && x <= 350) && (y >= 110 && y <= 140) && click == 1)
        {
            m = 1;
            j = 8;
            p = 8;
            a = 1;
        }
        if ((x >= 355 && x <= 385) && (y >= 110 && y <= 140) && click == 1)
        {
            j = 9;
            m = 1;
            p = 9;
            a = 1;
        }
        if ((x >= 390 && x <= 420) && (y >= 110 && y <= 140) && click == 1)
        {
            m = 1;
            j = 10;
            p = 10;
            a = 1;
        }
        if ((x >= 425 && x <= 455) && (y >= 110 && y <= 140) && click == 1)
        {
            m = 1;
            j = 11;
            p = 11;
            a = 1;
        }
        if ((x >= 460 && x <= 490) && (y >= 110 && y <= 140) && click == 1)
        {
            m = 1;
            j = 12;
            p = 12;
            a = 1;
        }
        if ((x >= 495 && x <= 525) && (y >= 110 && y <= 140) && click == 1)
        {
            m = 1;
            j = 13;
            p = 13;
            a = 1;
        }
        if ((x >= 530 && x <= 560) && (y >= 110 && y <= 140) && click == 1)
        {
            m = 1;
            j = 14;
            p = 14;
            a = 1;
        }
        if ((x >= 565 && x <= 595) && (y >= 110 && y <= 140) && click == 1)
        {
            m = 1;
            j = 15;
            p = 15;
            a = 1;
        }
        if ((x >= 600 && x <= 638) && (y >= 110 && y <= 140) && click == 1)
        {
            m = 1;
            j = 1;
            a = 1;
        }
        if ((x >= 100 && x <= 140) && (y >= 150 && y <= 190) && click == 1)
        {
            m = 0;
            click = 0;
            a = 0;
        }
        if ((x >= 100 && x <= 140) && (y >= 195 && y <= 235) && click == 1)
        {
            m = 2;
            click = 0;
            a = 0;
        }
        if ((x >= 100 && x <= 140) && (y >= 240 && y <= 280) && click == 1)
        {
            m = 3;
            click = 0;
            a = 0;
        }
        if ((x >= 100 && x <= 140) && (y >= 285 && y <= 325) && click == 1)
        {
            m = 4;
            click = 0;
            a = 0;
        }
        if ((x >= 100 && x <= 140) && (y >= 330 && y <= 370) && click == 1)
        {
            m = 5;
            click = 0;
            a = 0;
        }
        if ((x >= 100 && x <= 140) && (y >= 375 && y <= 415) && click == 1)
        {
            m = 6;
            click = 0;
            a = 0;
        }
        if ((x >= 100 && x <= 140) && (y >= 420 && y <= 460) && click == 1)
        {
            m = 7;
            click = 0;
        }
        if (m == 6)
        {
            if (click == 0)
            {
                x1 = x;
                y1 = y;
                x2 = x;
                y2 = y;
                x3 = x;
                y3 = y;
            }
            if ((x >= 150 && x <= getmaxx()) && (y >= 150 && y <= getmaxy()) && click == 1)
            {
                setcolor(1);
                line(x1, y3, x3, y1);
                line(x3, y1, x2, y3);
                line(x1, y3, x3, y2);
                line(x3, y2, x2, y3);
                x2 = x;
                y2 = y;
                x3 = x1 + (x2 - x1) / 2;
                y3 = y1 + (y2 - y1) / 2;
                setcolor(p);
                line(x1, y3, x3, y1);
                line(x3, y1, x2, y3);
                line(x1, y3, x3, y2);
                line(x3, y2, x2, y3);
                delay(200);
            }
        }
        if (m == 7 && a == 0)
        {
            if ((x >= 150 && x <= getmaxx()) && (y >= 150 && y <= getmaxy()) && click == 1)
            {
                regs_in.x.ax = 3;
                int86(0x33, &regs_in, &regs_out);
                x4 = regs_out.x.cx;
                y4 = regs_out.x.dx;
                regs_in.x.ax = 2;
                int86(0x33, &regs_in, &regs_out);
                setcolor(p);
                setfillstyle(1, p);
                floodfill(x4, y4, p);
                delay(200);
                m = 100;
            }
            regs_in.x.ax = 1;
            int86(0x33, &regs_in, &regs_out);
        }
        if (m == 5)
        {
            if (click == 0)
            {
                x1 = x;
                y1 = y;
                x2 = x;
                y2 = y;
                x3 = x;
            }
            if ((x >= 150 && x <= getmaxx()) && (y >= 150 && y <= getmaxy()) && click == 1)
            {
                setcolor(1);
                line(x1, y2, x1, y1);
                line(x1, y1, x2, y2);
                line(x1, y2, x2, y2);
                x2 = x;
                y2 = y;
                x3 = x1 + (x2 - x1) / 2;
                setcolor(p);
                line(x1, y2, x1, y1);
                line(x1, y1, x2, y2);
                line(x1, y2, x2, y2);
                delay(200);
            }
        }
        if (m == 4)
        {
            if (click == 0)
            {
                x1 = x;
                y1 = y;
                x2 = x;
                y2 = y;
                x3 = x;
            }
            if ((x >= 150 && x <= getmaxx()) && (y >= 150 && y <= getmaxy()) && click == 1)
            {
                setcolor(1);
                line(x1, y2, x3, y1);
                line(x3, y1, x2, y2);
                line(x1, y2, x2, y2);
                x2 = x;
                y2 = y;
                x3 = x1 + (x2 - x1) / 2;
                setcolor(p);
                line(x1, y2, x3, y1);
                line(x3, y1, x2, y2);
                line(x1, y2, x2, y2);
                delay(200);
            }
        }
        if (m == 3)
        {
            if (click == 0)
            {
                x1 = x;
                y1 = y;
                x2 = x;
                y2 = y;
            }
            if ((x >= 150 && x <= getmaxx()) && (y >= 150 && y <= getmaxy()) && click == 1)
            {
                setcolor(1);
                rectangle(x1, y1, x2, y2);
                x2 = x;
                y2 = y;
                setcolor(p);
                rectangle(x1, y1, x2, y2);
                delay(200);
            }
        }
        if (m == 2)
        {
            if (click == 0)
            {
                x1 = x;
                y1 = y;
                x2 = x;
                y2 = y;
                x3 = x;
                y3 = y;
            }
            if ((x >= 150 && x <= getmaxx()) && (y >= 150 && y <= getmaxy()) && click == 1)
            {
                setcolor(1);
                ellipse(x3, y3, 0, 360, abs(x2 - x1) / 2, abs(y2 - y1) / 2);
                x2 = x;
                y2 = y;
                x3 = x1 + (x2 - x1) / 2;
                y3 = y1 + (y2 - y1) / 2;
                setcolor(p);
                ellipse(x3, y3, 0, 360, abs(x2 - x1) / 2, abs(y2 - y1) / 2);
                delay(200);
            }
        }
        if (m == 0)
        {
            if (click == 0)
            {
                x1 = x;
                y1 = y;
                x2 = x;
                y2 = y;
            }
            if ((x >= 150 && x <= getmaxx()) && (y >= 150 && y <= getmaxy()) && click == 1)
            {
                setcolor(1);
                line(x1, y1, x2, y2);
                x2 = x;
                y2 = y;
                setcolor(p);
                line(x1, y1, x2, y2);
                delay(200);
            }
        }
        if (m == 1)
        {
            if ((x >= 150 && x <= getmaxx()) && (y >= 150 && y <= getmaxy()) && click == 1)
            {
                for (i = 0; i <= 2; i++)
                {
                    putpixel(x + i, y, j);
                    putpixel(x, y + i, j);
                    putpixel(x - i, y, j);
                    putpixel(x, y - i, j);
                }
            }
        }
    }
    closegraph();
    return 0;
}

Output :

After running the above program successfully, you can draw anything on the drawing ground and also create different shapes with color. You can also see the output of drawing program in my YouTube channel.

Conclusion :

By following this article, you will gain a fundamental understanding of how to create a drawing or painting application using the C or C++ graphics programming language. Thanks for visiting my site.

Scroll to Top