Click here to Skip to main content
12,953,230 members (50,536 online)
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 2 Aug 2004

Analog Clock

, 23 Jun 2005
Rate this:
Please Sign up or sign in to vote.
An analog clock control that can raise alarm at a given time and that can also set a count down alarm.

Sample Image - analogclock.jpg


I am a software developer working from home. This means that I have to attend to a number of chores while I am working on projects. Very often, when I am busy doing some absorbing piece of programming, I tend to forget some important chores. I therefore decided to develop an analog alarm clock control that could set alarms at specific times as well as alarms that count down time. I decided to implement this control in C#.

When I started working on the application, I was faced with a peculiar problem. You know that in C#, Cosine and Sine functions take their parameters in radians and not in degrees. The question was how to draw a clock face using radians? That meant drawing the 12 digits of the clock each at an angle of 30 degrees from each other, and the clock showing the minute hand, second hand, and the hour hand. I give below the method I used.

The Code

Drawing the Clock Face

To draw the clock, we write the strings 1 through 12 in the appropriate locations. We specify the location as x, y coordinates, imagining them to lie on an imaginary circle. Draw each number on the clock face with the overloaded DrawString method of the Graphics object.

void DrawString(string, Font, Brush, float, float, StringFormat);

How to compute x, y coordinates

The x-coordinate can be found by multiplying the cosine of the angle by the radius. A circle is of 360 degrees. To place 12 numbers on the circle, each number should be placed 30 degrees from the previous number. The C# Cosine and Sine functions take their parameters in radians and not degrees. To convert degrees to radians:

radians = (degrees * PI) / 180
x = GetCos( i * deg + 90) * FaceRadius;
y = GetSin( i * deg + 90) * FaceRadius;

The GetCos and GetSin methods convert the degrees to radians.

private static float GetSin(float degAngle)
   return (float) Math.Sin(Math.PI * degAngle / 180);
private static float GetCos(float degAngle)
   return (float) Math.Cos(Math.PI * degAngle / 180);

Drawing the hands (hour, minute, second)

Draw the hands using the Pen object. The EndCap property is of type LineCap. The ArrowAnchor is used in this example (view the code in the Zip file).

Pen hourPen = new (Color.Red);
hourPen.EndCap = LineCap.ArrowAnchor;

Create methods GetSecondRotation() and GetMinuteRotation() to return a floating point number indicating how much to rotate.

private float GetSecondRotation()
   return(360f * currentTime.Second / 60f);

private float GetMinuteRotation()
   return(360f * currentTime.Minute / 60f);

The GetHourRotation() method is difficult as we have to set the face to 24 hour mode, and the angle for the hour hand will be different if there are 24 hours around the clock face rather than 12.

private float GetHourRotation()
   float deg = b24Hours ? 15 : 30; 
   //30 degrees if 12 hour clock face or 15 degrees 
   //if 24 hr clock face.
   float numHours = b24Hours ? 24 : 12;
   return(360f *currentTime.Hour / numHours + deg*currentTime.Minute / 60f);

Drawing the new time

Once the seconds hand, minutes hand and the hours hand is drawn, set the currentTime to new time.

currentTime = newTime;

Finally, I have also declared two properties: AlarmTime which allows you to raise alarm, and countDownTime which allows you to set the count down timer to milliseconds.

Code References

The implementation for the clock face is home grown, but the code for 'How to use the Graphics object' is taken from the article 'Programming .NET Application' by O' Reilly. Complements to O' Reilly, as the classes he developed for drawing the clock face were exceptionally easy to apply and reuse.


This control demonstrates a simple use of the Graphics object in a real world example, which can be combined with other applications requiring alarms. I have included some more functionalities in my control and also included the demo project of how to use it and set the alarm time.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Nidhi S
Web Developer
India India
Nidhi Shrivastava
B.Sc (Maths), M.Sc(Maths), PGDCA(NIIT)
Working as a freelance software developer.

Experienced in C# .NET, VB .NET, VB 6.0, SQL Server 2000, Java 2 SDK.

Worked with PMPCertify (home based) (
Currently working with .NET and its technology for last 1 and half years. Previously worked with VB 6.0 and have done many projects using it.

Hobbies include reading, music and travelling.

You may also be interested in...

Comments and Discussions

GeneralThanks Pin
Oner Dikdere (TR)4-Nov-07 5:23
memberOner Dikdere (TR)4-Nov-07 5:23 
GeneralAlarm clock using java Pin
Wafiq9-Oct-07 9:55
memberWafiq9-Oct-07 9:55 
GeneralSimplify the math Pin
rvdt30-Jun-05 20:26
memberrvdt30-Jun-05 20:26 
GeneralHello Nidhi Pin
Anonymous29-Aug-05 6:49
sussAnonymous29-Aug-05 6:49 
GeneralUsing Pin
Esam Salah12-May-05 1:15
memberEsam Salah12-May-05 1:15 
GeneralRe: Using Pin
Nidhi S17-Jun-05 21:37
memberNidhi S17-Jun-05 21:37 
Generalgood work Pin
Srinivas Varukala3-Aug-04 7:45
memberSrinivas Varukala3-Aug-04 7:45 
GeneralVector Graphics Pin
Marc Clifton3-Aug-04 5:11
protectorMarc Clifton3-Aug-04 5:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170525.1 | Last Updated 24 Jun 2005
Article Copyright 2004 by Nidhi S
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid