Skip to content
Related Articles

Related Articles

Improve Article

Program To Create Keypad Mobile Using Computer Graphics

  • Last Updated : 20 Aug, 2021

In Turbo C graphics, the graphics.h functions are used to draw different shapes like a circle, rectangle, etc, display text(any message) in different formats (different fonts and colors). By using graphics.h programs, animations, and also games can be designed. These can be useful for beginners.

Function Used:

  • rectangle(l, t, r, b): A function from graphics.h header file which draws a rectangle from left(l) to right(r) and from top(t) to bottom(b).
  • line(a1, b1, a2, b2): A function from graphics.h header file which draws a line from (a1, b1) point to (a2, b2) point.
  • Circle(a, b, r): A function from graphics.h header file which draws a circle with (a, b) as the center and r is the radius.
  • setfillstyle( pattern, color): A function from graphics.h header file by which one can give a pattern of drawing and also a specific color.
  • floodfill( a, b, c): A function from graphics.h header file by which one can color a specific bounded area with (a, b) as the center and c as the border color.
  • outtextxy(int x, int y, char *string): A function from graphics.h header file by which one can print any statements where, x, y are coordinates of the point and, the third argument contains the address of the string to be displayed.
  • settextstyle(int font, int direction, int font_size): A function from graphics.h header file by which one can create the style of the printable text where the font argument specifies the font of the text. Direction can be HORIZ_DIR (Left to right) or VERT_DIR (Bottom to top).

Approach:

  • Four functions are defined menu_key(), others(), key(), screen().
  • The background color is made dark-gray using setfillstyle() and floodfill() functions.
  • The next step is to implement a rectangle using the rectangle() function, which will act as the outline of the mobile.
  • Color inside the outline as Black using setfillstyle() and floodfill() functions. That will be the color of the mobile.
  • Invoke the screen() function and make the screen outline using the rectangle() function. Implement the Indian Flag as the screen-saver of the mobile. The Indian Flag is also implemented by Computer Graphics.
  • Call others() function. Some mobile accessories are implemented in this function, like Speaker, Selfie Camera, Microphone & Mobile Brand Name.
  • Speaker will be implemented by using the rectangle() function and for coloring white, the setfillstyle() and floodfill() functions are used. The selfie Camera is implemented by using two concatenated circles implemented by the circle() function. Color it white using setfillstyle() and floodfill() functions.
  • The microphone is implemented by a single circle implemented by a circle() function. Also, it is colored with white using setfillstyle() and floodfill() functions. Mobile Brand Name is implemented by outtextxy() and settextstyle() functions.
  • Call menu_key() function to implement the Menu key. The main menu key is implemented by two concatenated rectangles implemented by the rectangle() function. Color this rectangle white using setfillstyle() and floodfill() function. Implement four keys using the rectangle() function. Among them, two will be decorated with white color using setfillstyle() and floodfill() functions, and the other two are call receiving and rejecting keys. The call receiving key is colored green and the call rejecting key is colored red using setfillstyle() and floodfill() functions.
  • Call the key() function to implement other keys. On mobile phones, there are 12 keys present. Each of them has a different name and work. So, here two nested while loops are implemented that will create a total of 12 keys, 3 keys in every 4 rows.
  • If statements are implemented to print the number of the keys. The numbers are printed on the screen using outtextxy() and settextstyle().

Below is the implementation of the above approach:

C




// C program for the
// above approach
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
 
// Used Function Declaration
void menu_key();
void others();
void key();
void screen();
 
// Driver Code
void main()
{
    int gd = DETECT, gm;
 
    // Initialize of gdriver with
    // DETECT macros
    initgraph(&gd, &gm,
              "C:\\turboc3\\bgi");
 
    // Set Background Color As
    // Darkgray
    setfillstyle(SOLID_FILL,
                 DARKGRAY);
    floodfill(1, 1, 15);
 
    // Main Outline
    rectangle(800, 100, 1100, 730);
 
    // Set Phone Color AS Black
    setfillstyle(SOLID_FILL, BLACK);
    floodfill(805, 105, 15);
 
    // Calling screen() Function
    screen();
 
    // Calling others() Function
    others();
 
    // Calling menu_key() Function
    menu_key();
 
    // Calling key() Function
    key();
 
    // Holding The Screen For
    // A While
    getch();
 
    // Close the initialized
    // gdriver
    closegraph();
}
 
void screen()
{
    // Screen Outline
    rectangle(830, 130, 1070, 370);
 
    // Screen Saver Indian Flag
    rectangle(900, 170, 1000, 230);
    line(900, 190, 1000, 190);
    setfillstyle(SOLID_FILL,
                 LIGHTRED);
    floodfill(905, 175, 15);
    circle(950, 200, 10);
    setfillstyle(SOLID_FILL, BLUE);
    floodfill(955, 205, 15);
    line(900, 190, 1000, 190);
    line(900, 210, 1000, 210);
    setfillstyle(SOLID_FILL, WHITE);
    floodfill(905, 195, 15);
    floodfill(995, 195, 15);
    line(900, 210, 1000, 210);
    setfillstyle(SOLID_FILL, GREEN);
    floodfill(905, 215, 15);
    line(900, 170, 900, 320);
    rectangle(880, 320, 920, 330);
    setfillstyle(SOLID_FILL, WHITE);
    floodfill(885, 325, 15);
}
 
void key()
{
    int l = 820, t = 500, i = 1;
 
    // Implementing 12 Others Key
    // and
    // Numbering Them
    while (t <= 650) {
        while (l <= 1020) {
            rectangle(l, t, l + 70,
                      t + 40);
            if (i == 1) {
                settextstyle(8, 0, 2);
                outtextxy(l + 5, t + 5,
                          "1 ., ?");
            }
            else if (i == 2) {
                settextstyle(8, 0, 2);
                outtextxy(l + 5, t + 5,
                          "2 ABC");
            }
            else if (i == 3) {
                settextstyle(8, 0, 2);
                outtextxy(l + 5, t + 5,
                          "3 DEF");
            }
            else if (i == 4) {
                settextstyle(8, 0, 2);
                outtextxy(l + 5, t + 5,
                          "4 GHI");
            }
            else if (i == 5) {
                settextstyle(8, 0, 2);
                outtextxy(l + 5, t + 5,
                          "5 JKL");
            }
            else if (i == 6) {
                settextstyle(8, 0, 2);
                outtextxy(l + 5, t + 5,
                          "6 MNO");
            }
            else if (i == 7) {
                settextstyle(8, 0, 2);
                outtextxy(l + 5, t + 5,
                          "7PQRS");
            }
            else if (i == 8) {
                settextstyle(8, 0, 2);
                outtextxy(l + 5, t + 5,
                          "8 TUV");
            }
            else if (i == 9) {
                settextstyle(8, 0, 2);
                outtextxy(l + 5, t + 5,
                          "9WXYZ");
            }
            else if (i == 10) {
                settextstyle(8, 0, 2);
                outtextxy(l + 5, t + 5,
                          "* +");
            }
            else if (i == 11) {
                settextstyle(8, 0, 2);
                outtextxy(l + 5, t + 5,
                          "0 _");
            }
            else if (i == 12) {
                settextstyle(8, 0, 2);
                outtextxy(l + 5, t + 5,
                          "# x");
            }
            i++;
            l = l + 100;
        }
        t = t + 50;
        l = 820;
    }
}
 
void others()
{
    setfillstyle(SOLID_FILL,
                 WHITE);
 
    // Implement Microphone
    circle(900, 710, 5);
    floodfill(902, 712, 15);
 
    // Implement Selfie Camera
    circle(1030, 115, 8);
    circle(1030, 115, 4);
    floodfill(1031, 116, 15);
 
    // Implement Speaker
    rectangle(900, 110, 1000, 120);
    setfillstyle(XHATCH_FILL, WHITE);
    floodfill(905, 115, 15);
 
    // Implement Mobile Brand Name
    settextstyle(8, 0, 3);
    outtextxy(895, 375, "SOUNETRA");
 
    // Division Between
    // Screen and Keys
    line(800, 400, 1100, 400);
}
 
void menu_key()
{
    setfillstyle(SOLID_FILL, WHITE);
 
    // Menu Key
    rectangle(930, 420, 980, 470);
    rectangle(920, 410, 990, 480);
    settextstyle(10, 0, 3);
    outtextxy(940, 430, "OK");
    floodfill(925, 415, 15);
 
    // Left Upper Bottom
    rectangle(820, 410, 890, 440);
    rectangle(830, 420, 880, 430);
    floodfill(835, 425, 15);
 
    // Right Upper Bottom
    rectangle(1020, 410, 1090, 440);
    rectangle(1030, 420, 1080, 430);
    floodfill(1035, 425, 15);
 
    // Left Lower Bottom
    rectangle(820, 450, 890, 480);
    setfillstyle(SOLID_FILL, GREEN);
    floodfill(825, 455, 15);
 
    // Right Lower Bottom
    rectangle(1020, 450, 1090, 480);
    setfillstyle(SOLID_FILL, RED);
    floodfill(1025, 455, 15);
}

Output:

KEYPAD MOBILE

 

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.




My Personal Notes arrow_drop_up
Recommended Articles
Page :