Click here to Skip to main content
15,886,788 members
Articles / Multimedia / GDI+
Article

Analog Clock

Rate me:
Please Sign up or sign in to vote.
4.35/5 (13 votes)
7 Dec 20061 min read 60.3K   1.7K   26   4
An article on creating a simple analog clock using drawing and math.

Sample Image

Introduction

This is your basic analog clock using Cos, Sin, Theta, and the form's aspect ratio to draw the circles and gather the placement of the markers and hands. This is my first submission on here, and I hope it is useful to someone out there. The code is heavily documented, and there is also a demo download.

Background

I first wrote the code for this way back in the QB 4.5 days, and later converted it to Flash MX. I was bored on one of my days off, and really didn't feel like working on the other projects I have going, so I did this for old time sakes. I hope you enjoy it.

Using the code

I separated the main drawings into different Subs to make the code easier to follow and understand.

The main sub that is called is the Draw_Graphics() and it calls the other Subs to do the drawing. I am using a Bitmap object and a Graphics object to paint the pictures, and then I push the picture to the form to prevent flickering.

VB
Private Sub Draw_Graphics()
    '******************************************************************
    '******** The drawing sub which has the individual         ******** 
    '******** subs in it for easier understand-                ********
    '******** ing. First we will draw the clock base,          ********
    '******** then the face, on to the markers,                ********
    '******** then the date and time on the face,              ********
    '******** and finally the hands of the clock.              ********
    '******************************************************************
    Draw_Clock_Base()
    Draw_Clock_Face()
    Draw_Clock_Markers()
    Draw_Clock_DateTime()
    Draw_Clock_Hands()
End Sub

These objects and variables are used in the full scope of the form. Other variables are used within the Subs.

VB
Dim bm As Bitmap
Dim g As Graphics
Dim PI As Double = 3.14159
Dim XC, YC, X, Y, R, OldSec As Integer
Dim Theta, AspectRatio As Double

Points of Interest

I don't do a lot of graphics programming, so when working with the DrawPolygon command, it kept wanting to default the last location to (0,0) and I couldn't figure out why, so I ended up drawing the lines manually. If anyone has any suggestions, please let me know.

License

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


Written By
Web Developer
United States United States
What would life be without code?

Comments and Discussions

 
Generalgood work yeah Pin
vivekc514-Jun-11 3:38
vivekc514-Jun-11 3:38 
GeneralWho is gonna use this Pin
Nirosh7-Dec-06 17:15
professionalNirosh7-Dec-06 17:15 
Hi,

When you developed a module like this, which has bundled with a windows form, you are limiting the area of its usage. I do not think that no one wants to click a button and wait for a new window popup just to see the time. It is less practical and less probable..

What you could do to improve is,, make it a user control, ideally a separated DLL and then demonstrate its usage inside a form (placing it at the top right corner probably) like a widget.. Also shoult support several form like, analog clock, digital clock and also expanded clock, which diaplay a long date time.. etc... that will make this highlight among others and also it will increase the value of this control..

I also suggest you to take advantage of skin feature and region property of the control, which will ensure a better UI and also will minimally hurt its container.

I hope this comment will help you in some way..

L.W.C. Nirosh.
Colombo,
Sri Lanka.

GeneralRe: Who is gonna use this Pin
Uber17-Dec-06 19:35
Uber17-Dec-06 19:35 
AnswerRe: Who is gonna use this Pin
Hanzie5319-Dec-06 10:15
Hanzie5319-Dec-06 10:15 

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.