Click here to Skip to main content
15,434,055 members
Articles / Programming Languages / C#
Article
Posted 12 Aug 2009

Tagged as

Stats

26.6K views
517 downloads
12 bookmarked

Simple Binary Clock using Graphics

Rate me:
Please Sign up or sign in to vote.
4.42/5 (10 votes)
13 Aug 2009CPOL1 min read
A simple binary clock snippet

Introduction

“There are only 10 people in this world, those who understand binary – and those who don’t”
This little snippet is the result of me playing about with C# drawing. It basicly takes a DateTime, converts each number to a binary string and then draws each binary value as a whole or empty circle to a given pictureBox.

If you're interested in learning more about how it works, check out the attached sample source code which is fully documented

Using the Code

Ensure your windows form makes reference to the following namespaces:

C#
using System.Drawing;
using System.Windows.Forms;

The main BinaryClock function:

C#
/*For a fully documented version of this snippet please see the attached sample code */
private void BinaryClock(PictureBox PicBox, DateTime dtTime)
        {
            int bcSize = 10;   
            int bcSpacing = 15;           
            int bcY = 50;                       
            int bcX = 5;                            

            Color nodeColor = Color.Gray;           
            PicBox.BackColor = Color.White;              

            Graphics gClock = PicBox.CreateGraphics();   
            Pen myPen = new Pen(nodeColor);              
            Brush brsh = new SolidBrush(nodeColor);
            string strTime = dtTime.ToString("hhmmss");

            gClock.Clear(PicBox.BackColor);

            for (int x = 0; x <= strTime.Length - 1; x++)
            {
                int bcAmount = 3; 

                if (x == 2 || x == 4) 
                {
                    bcX += 5;      
                    bcAmount = 2; 
                }
                else if (x == 0)   
                {
                    bcAmount = 1;   
                }



                for (int i = 0; i <= bcAmount; i++)
                {
                    string BinaryString = Convert.ToString(strTime[x], 2);
                    if (BinaryString[BinaryString.Length - (i + 1)] == '1')
                        gClock.FillEllipse(brsh, new Rectangle(bcX + 1 +
                            (x * bcSpacing), bcY - (i * bcSpacing), bcSize, bcSize));
                    else
                        gClock.DrawEllipse(myPen, new Rectangle(bcX + 1 +
                           (x * bcSpacing), bcY - (i * bcSpacing), bcSize, bcSize));

                }
            }
        }

To draw the clock, you simply call the BinaryClock method with the PictureBox you wish to draw to and the DateTime to show:

C#
BinaryClock(pictureBox1, DateTime.Now);

This will then draw the given DateTime to the pictureBox in binary! To have it update in real time, add the above code to a timers Tick event and set the timers interval to 1000 (1 second).

Points of Interest

Play about with the variables at the top of the method to get your desired effect.

  • bcSize - Size of each binary dot
  • bcSpacing – Space between each binary string
  • bcY – pixels from the picture box top
  • bcX – Pixels from the picture boxes left edge
  • nodeColor - self explanatory
  • PicBox.BackColor – self explanatory

History

  • 13/AUG/09: Updated with documented sample project
  • 12/AUG/09: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior) Klickware
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGood work! Pin
alrsds19-Sep-09 19:20
Memberalrsds19-Sep-09 19:20 
Generali&lt;=length-1 Pin
fkhg113-Aug-09 3:11
Memberfkhg113-Aug-09 3:11 
GeneralRe: i&lt;=length-1 Pin
Tommy Pickersgill13-Aug-09 6:40
MemberTommy Pickersgill13-Aug-09 6:40 
GeneralMy vote of 1 Pin
Michael E. Jones12-Aug-09 21:43
MemberMichael E. Jones12-Aug-09 21:43 
GeneralRe: My vote of 1 Pin
Tommy Pickersgill12-Aug-09 21:47
MemberTommy Pickersgill12-Aug-09 21:47 
GeneralRe: My vote of 1 Pin
Jason Barry13-Aug-09 2:52
professionalJason Barry13-Aug-09 2:52 
Is it really that hard to copy and paste the small code snippet into a new project? I agree that it should probably have a sample project included with the article, but I wouldn't rate it a 1 for that.
GeneralMy vote of 1 Pin
getter@dotnet12-Aug-09 21:24
Membergetter@dotnet12-Aug-09 21:24 
GeneralCode Pin
Amarnath S12-Aug-09 16:33
professionalAmarnath S12-Aug-09 16:33 

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.