Introduction :
Clock is an essential aspect of many graphical user interface (GUI) applications. Building a clock using programing is a very interesting topic. Using C or C++ graphics program, creating an analog and digital clock is a very fun and challenging project for beginners. Creating a clock may seem like a difficult task, but with C or C++ graphics programming, it can be done easily.
In this post, I shall show you how to create an analog and digital clock in C or C++ graphics programming language. Here, I create both analog and digital clocks using graphics.h in Turbo C++ IDE which updating its time automatically.
What is clock :
Clocks are an essential part of our modern lives. In our daily life, we can’t survive without knowledge of time. We get the exact time by using of clock. A clock is a device used to measure and display the exact time. Clock has been used for thousands of years to help people keep track of time and organize their daily lives.
There are several types of clocks you can see in daily life. They are Analog Clock, Digital Clock, Mechanical Clock, Quartz Clock, Atomic Clock, Solar Clock, Radio Clock, Smart Clock etc. In this post, I discuss with you about Analog and Digital Clock.
Analog clock is traditional clocks with a circular dial or face with hour, minute and second hands that point to the numbers or symbols on the dial. Analog clock uses a mechanical mechanism that moves the hands at a constant rate to keep the current time. There are some examples of analog clock are wrist watch, wall clock and tower clock etc.
Digital clock typically displays the time using digits (numbers) on an LED or LCD display. They are used in electronic devices like computers, smartphones and alarm clock.
About the program :
In this program, I use the graphics.h library to draw a clock face by drawing a circle and dividing it into 12 parts to represent the hours and then draw the hour, minute and second hands on the clock face to create an analog clock. I also create a digital clock by draw a rectangle to represent the digits and then display the time by updating the digits every second.
Logic for analog and digital clock by C or C++ programming language :
The logic of this program is very simple. In the program, I use a while loop where the second hand of a clock moves around the circle fixed to the center of the circle. When the second hand completes the round means it moves 360 degrees, the minute hand moves 6 degrees and when the minute hand completes the round means it moves 360 degrees, the hour hand moves 0.5 degrees. This process is run continues.
At the same time the value of second, minute and hour is displayed in rectangular box for digital clock. Note that, clock is not showing current time in the program. Because I have not use time.h header file in my program.
Explanation of the program :
In this program I use the graphics.h, stdio.h, conio.h, dos.h header files to create both analog and digital clocks in C or C++ programing language. You have to declare integer variables (xmax, ymax, x, y, i, j, k, m, s, h, m1, graphic_driver and graphic_mode) and float variable (l) in the main() function. Before start drawing, you have to initialize the graphics mode using initgraph() function.
Then, get the x and y coordinate using getmaxx() and getmaxy() functions. Here, you can use setcolor() function to set the color of different shape and setbkcolor() to set background color. Using rectangle() and line() function, you can create a rectangular box to show the digital clock. To create face of the analog clock, use circle() function.
The setfillstyle() and floodfill() functions are used to fill the different shapes. Using settextstyle() and outtextxy() functions, write the hour from one (1) to twelve (12) at the border of circle (face of the analog clock). In the while loop, draw hour, minute and second hand using pieslice() function. You can use kbhit() function to get a key press from users.
Using sound() and nosound() functions produces sound when update position of the hands of the clock. You can use delay() function to maintain the time for updating. The gotoxy() function use for go to the digital clock. Here, use printf() function to print the digit of time on the digital clock. You have to use closegraph() function to stop the graphics mode.
How run the program :
Firstly, you have to install Turbo C++ IDE on your system so that you run the program. In this article, I shall use the graphics.h library which is included in the Turbo C++ IDE. The graphics.h library provides functions for creating and manipulating graphical objects on the screen. After that, create a C or C++ file in Turbo C++ IDE. Now, copy the following source code and paste in your C or C++ file.
You have to know how to copy paste in the Turbo C++ IDE. Then, save the file and run after compile. You will be seeing the analog and digital clock on your PC. If you do not know how use graphics.h in Turbo C++ follow the link.
Source code of the program :
You can copy the following source code of creating an analog and digital clock in C or C++ graphics programming language.
/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
int main(void)
{
int xmax, ymax, x, y, i = 0, j = 0, k = 0, m = 0, s = 0, h = 0, m1 = 0;
float l = 0.0;
int graphic_driver = DETECT, graphic_mode;
initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
xmax = getmaxx();
ymax = getmaxy();
x = xmax / 2;
y = ymax / 2;
setcolor(15);
setbkcolor(0);
rectangle(x + 150, y - 200, x + 300, y - 170);
line(x + 200, y - 200, x + 200, y - 170);
line(x + 250, y - 200, x + 250, y - 170);
circle(x, y, 179);
circle(x, y, 180);
setfillstyle(0, 0);
floodfill(x, y, 0);
settextstyle(1, 0, 4);
outtextxy(x - 200, y - 25, "9");
outtextxy(x + 186, y - 25, "3");
outtextxy(x - 20, y - 220, "12");
outtextxy(x - 10, y + 176, "6");
outtextxy(x + 90, y - 195, "1");
outtextxy(x + 165, y - 120, "2");
outtextxy(x + 155, y + 80, "4");
outtextxy(x + 86, y + 152, "5");
outtextxy(x - 109, y + 148, "7");
outtextxy(x - 180, y + 72, "8");
outtextxy(x - 195, y - 120, "10");
outtextxy(x - 120, y - 195, "11");
settextstyle(1, 0, 3);
outtextxy(0, ymax - 30, "PRESS ANY KEY FOR STOP");
outtextxy(x + 150, y + 210, "PUSKAR JASU");
settextstyle(1, 0, 6);
outtextxy(0, 0, "CLOCK");
while (!kbhit())
{
setcolor(14);
if (m == 60)
{
if (k <= 90)
{
setcolor(14);
pieslice(x, y, 91 - k, 90 - k, 175);
sound(440);
delay(10);
nosound();
setcolor(0);
pieslice(x, y, 97 - k, 96 - k, 175);
}
if (k > 90)
{
setcolor(14);
pieslice(x, y, 451 - k, 450 - k, 175);
sound(440);
delay(10);
nosound();
setcolor(0);
pieslice(x, y, 457 - k, 456 - k, 175);
}
if (k == 360)
k = 0;
k = k + 6;
}
else
{
if (k <= 90)
{
pieslice(x, y, 91 - k, 90 - k, 175);
setcolor(0);
pieslice(x, y, 97 - k, 96 - k, 175);
}
if (k > 90)
{
setcolor(14);
pieslice(x, y, 451 - k, 450 - k, 175);
setcolor(0);
pieslice(x, y, 457 - k, 456 - k, 175);
}
}
setcolor(4);
if (j == 60 * 5)
{
if (l <= 90)
{
setcolor(4);
pieslice(x, y, 91 - l, 90 - l, 175);
sound(880);
delay(20);
nosound();
setcolor(0);
pieslice(x, y, 93.5 - l, 92.5 - l, 175);
}
if (l > 90)
{
setcolor(4);
pieslice(x, y, 451 - l, 450 - l, 175);
sound(880);
delay(20);
nosound();
setcolor(0);
pieslice(x, y, 453.5 - l, 452.5 - l, 175);
}
if (l == 360)
l = 0;
l = l + 2.5;
}
else
{
if (l <= 90)
{
setcolor(4);
pieslice(x, y, 91 - l, 90 - l, 175);
setcolor(0);
pieslice(x, y, 93.5 - l, 92.5 - l, 175);
}
if (l > 90)
{
setcolor(4);
pieslice(x, y, 451 - l, 450 - l, 175);
setcolor(0);
pieslice(x, y, 453.5 - l, 452.5 - l, 175);
}
}
if (i <= 90)
{
setcolor(10);
pieslice(x, y, 91 - i, 90 - i, 175);
delay(1000);
sound(220);
delay(5);
nosound();
setcolor(0);
pieslice(x, y, 91 - i, 90 - i, 175);
}
if (i > 90)
{
setcolor(10);
pieslice(x, y, 451 - i, 450 - i, 175);
delay(1000);
sound(220);
delay(5);
nosound();
setcolor(0);
pieslice(x, y, 451 - i, 450 - i, 175);
}
if (i == 360)
i = 0;
i = i + 6;
s = i / 6;
if (j == 60 * 5)
j = 0;
j++;
if (m == 60)
m = 0;
m++;
if (s == 60)
{
m1++;
s = 0;
}
if (h == 24)
h = 0;
if (m1 == 60)
{
h++;
m1 = 0;
}
gotoxy(62, 4);
printf("%02d", h);
gotoxy(68, 4);
printf("%02d", m1);
gotoxy(74, 4);
printf("%02d", s);
s++;
}
closegraph();
return 0;
}
Output of the program :
After run the program on your PC, you can see the output like my YouTube channel.
Conclusion :
By the end of this article, I think you have gained knowledge on how to create two different types of clocks (analog and digital). Now, you have improved your skills in C or C++ programming language with graphics program. This article has provided a comprehensive guide to building both analog and digital clocks using C or C++ graphics programming language.
You will also be able to experiment with different designs to create unique and personalized clocks with the help of my code. Thank you for visiting my site.