Youtube RSS Feed
Learn with Vikas Suhag
Design an Analog Clock using C# Graphics features.
Nov 16, 2021   Vikas Suhag
Analog Clock using C# Graphics features.

AIM :  Design an Analog Clock using C# Graphics features.

Source Code :

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

namespace Analog_Clock

{

    public partial class Form1 : Form

    {

        Graphics g;

        float x1, y1,x2,y2,x3,y3,inc1,inc2,inc3,sx,sy,th4;

        float th1 = -90 + DateTime.Now.Second*6;

        float th2 = -90 + DateTime.Now.Minute * 6 + DateTime.Now.Second * 1.0f / 10;

        float th3 = -90 + DateTime.Now.Hour * 30 + DateTime.Now.Minute * 1.0f / 2 + DateTime.Now.Second * 1.0f / 120;

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            g = CreateGraphics();

            timer1.Interval = 1000;

            timer1.Start();

        }

 

        private void Form1_Paint(object sender, PaintEventArgs e)

        {

            int i=1;

            //g.DrawEllipse(Pens.Black, 250, 100, 500, 500);

            g.DrawEllipse(Pens.Black, 230, 80, 540, 540);

            Font f = new Font("Arial",10,FontStyle.Bold);

            for (th4 = -60; th4 <= 270; th4 += 30)

            {

                inc1 = (float)(3.14 / 180) * th4;

                sx = (float)(500 + 200 * Math.Cos(inc1))-0.4f;

                sy = (float)(350 + 200 * Math.Sin(inc1))+.4f;

                g.DrawString(i.ToString(), f, Brushes.Black, sx, sy);

                i++;

            }

        }

 

        private void timer1_Tick(object sender, EventArgs e)

        {

            Refresh();

            th1 += 6;

            th2 += 1.0f / 10;

            th3 += 1.0f / 120;

            inc1 = (float)(3.14 / 180) * th1;

            inc2 = (float)(3.14 / 180) * th2;

            inc3 = (float)(3.14 / 180) * th3;

            x1 = (float)(500 + 200 * Math.Cos(inc1));

            y1 = (float)(350 + 200 * Math.Sin(inc1));

            x2 = (float)(500 + 160 * Math.Cos(inc2));

            y2 = (float)(350 + 160 * Math.Sin(inc2));

            x3 = (float)(500 + 100 * Math.Cos(inc3));

            y3 = (float)(350 + 100 * Math.Sin(inc3));

            g.DrawLine(Pens.Black, 500, 350, x1, y1);

            g.DrawLine(Pens.Black, 500, 350, x2, y2);

            g.DrawLine(Pens.Black, 500, 350, x3, y3);

        }

    }

}

Output:


Leave a Comment

* Email will not be published.
All comments are only visible after approval.
Most Viewed articles
Search
Subscribe to Email Updates
Vikas Suhag

I am Engineer, Developer, Teacher, Blogger, Vlogger, Traveller, Photographer...

I have been developing websites, writing blogs and recording videos during my travel while being a full tim ...

Youtube Channel
Podcast
Subscribe to Email Updates
Connect With Us
Youtube RSS Feed
© 2021 Learn with Vikas Suhag. All Rights Reserved.