Analog Clock Design in C#

                                       Analog Clock

                       

                               Coding in C Sharp

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace AnalogClockCS
{
    public partial class MainForm : Form
    {
        Timer t = new Timer();

        int Width = 400, Height = 400; // Intialization Of Clock's Height and Width ^_^
        int secHAND = 160, minHAND = 110, hrHAND = 80;

        //center
        int cx, cy;

        Bitmap bmp;
        Graphics g; // Declaration Of Graphical Object or Instance ^_^

        public MainForm()
        {
            InitializeComponent();
        }


        private void MainForm_Load(object sender, EventArgs e)
        {
           
            //create bitmap
            bmp = new Bitmap(Width + 1 , Height + 1 );

            //This Code Tells The Position of Needles In Centre ^_^
            cx = Width/2  ;
            cy = Height/2 ;

            //MainForm BackGround Color
            this.BackColor = Color.DimGray;

            //Timer
            t.Interval = 1000;      //in millisecond
            t.Tick += new EventHandler(this.t_Tick);
            t.Start();
        }
        private void t_Tick(object sender, EventArgs e)
        {
            //create graphics
           g = Graphics.FromImage(bmp);

            //This Code Gets The Current Time On Clock ^_^
            int sec = DateTime.Now.Second;
            int min = DateTime.Now.Minute;
            int hour = DateTime.Now.Hour;

           int[] handCoord = new int[2];

            //This Code Changes The Inner-Color Of Clock
            g.Clear(Color.Black);

            //This Code Draw The Shape Of Clock ^_^
         
            g.DrawRectangle(new Pen(Color.DarkRed, 4f),0,0, Width, Height);

            //This Code Sets The Positons Of Numerals On Clock ^_^
            g.DrawString(hour + " : " + min + " : " + sec , new Font("Arial", 25), Brushes.Orange, new PointF(130, 280));
            g.DrawString("11", new Font("Arial", 23),Brushes.Silver, new PointF(95, 30));
            g.DrawString("12", new Font("Impact", 30), Brushes.OrangeRed, new PointF(178, 4));
            g.DrawString("1", new Font("Arial", 23), Brushes.Silver, new PointF(280, 30));
            g.DrawString("2", new Font("Arial", 23), Brushes.Silver, new PointF(350, 95));
            g.DrawString("3", new Font("Impact", 30), Brushes.YellowGreen, new PointF(370, 177));
            g.DrawString("4", new Font("Arial", 23), Brushes.Silver, new PointF(350, 275));
            g.DrawString("5", new Font("Arial", 23), Brushes.Silver, new PointF(280, 345));
            g.DrawString("6", new Font("Impact", 27), Brushes.MediumPurple, new PointF(180, 360));
            g.DrawString("7", new Font("Arial", 23), Brushes.Silver, new PointF(95, 345));
            g.DrawString("8", new Font("Arial", 23), Brushes.Silver, new PointF(28, 275));
            g.DrawString("9", new Font("Impact", 30), Brushes.RoyalBlue, new PointF(0, 177));
            g.DrawString("10", new Font("Arial", 23), Brushes.Silver, new PointF(30, 95));

            //second hand
            handCoord = msCoord(sec, secHAND);
            g.DrawLine(new Pen(Color.Red, 1f), new Point( cx,cy), new Point(handCoord[0], handCoord[1]));

            //minute hand
            handCoord = msCoord(min, minHAND);
            g.DrawLine(new Pen(Color.White, 2f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));

            //hour hand
            handCoord = hrCoord(hour % 12, min, hrHAND);
             g.DrawLine(new Pen(Color.Chartreuse, 3f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));
           

            //load bmp in picturebox1
           pictureBox1.Image = bmp;

            //disp time
            this.Text = ("Analog Clock -  " + hour + ":" + min + ":" + sec);

            //dispose
           // g.Dispose();
        }

        //coord for minute and second hand
        private int[] msCoord(int val, int hlen)
        {
            int[] coord = new int[2];
            val *= 6;   //each minute and second make 6 degree

            if (val >= 0 && val <= 180)
            {
                coord[0] = cx + (int)(hlen * Math.Sin(Math.PI * val / 180));
                coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));
            }
            else
            {
                coord[0] = cx - (int)(hlen * -Math.Sin(Math.PI * val / 180));
                coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));
            }
            return coord;
        }

        //coord for hour hand
        private int[] hrCoord(int hval, int mval, int hlen)
        {
            int[] coord = new int[2];

            //each hour makes 30 degree
            //each min makes 0.5 degree
            int val = (int)((hval * 30) + (mval * 0.5));

            if (val >= 0 && val <= 180)
            {
                coord[0] = cx + (int)(hlen * Math.Sin(Math.PI * val / 180));
                coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));
            }
            else
            {
                coord[0] = cx - (int)(hlen * -Math.Sin(Math.PI * val / 180));
                coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));
            }
            return coord;
        }
      
    }
}

                           Design  by Danish Shehzad

 Source code

Click here to download 

Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

Powered by Blogger.

Translate

 
 
.