Kurdistan Region-Iraq
Cihan University-Duhok
Department of computer
3rd
Year
ComputerGraphics
Analog Clock
Prepare by :
Omid Mustefa
Supervised :
Dr.Salim
INTRODUCTION
This project is about how to
create an Analog clock
application for windows by
using computer graphics C#
language .
Source Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace clock
{
public partial class Form1 : Form
{
Point[,] hands_coord = new Point[3, 2]
{
{new Point(0,0),new Point (0,120)},
{new Point (0,0),new Point (0,140)},
{new Point (0,0),new Point (0,140)}
};
DateTime cur;
DateTime prev;
Boolean change;
Graphics g;
public Form1()
{
InitializeComponent();
}
private void RotatePoint(Point[] pt,
int iRotate, int iangle)
{
Point ptTemp = new Point(0, 0);
for (int i = 0; i < iRotate; i++)
{
ptTemp.X = (int)(pt[i].X *
Math.Cos(2 * Math.PI * iangle / 360) - pt[i].Y
* Math.Sin(2 * Math.PI * iangle / 360));
ptTemp.Y = (int)(pt[i].Y *
Math.Cos(2 * Math.PI * iangle / 360) + pt[i].X
* Math.Sin(2 * Math.PI * iangle / 360));
pt[i] = ptTemp;
}
}
private void Form1_Load(object sender,
EventArgs e)
{
}
private void Form1_Paint(object sender,
PaintEventArgs e)
{
e.Graphics.TranslateTransform(this.Size.Width /
2, this.Size.Height / 2);
e.Graphics.RotateTransform(150);
DrawClock(e.Graphics);
DrawHands(e.Graphics, prev, true,
Color.FromArgb(250, 0, 0, 0));
}
private void DrawClock(Graphics e)
{
Point[] pt = new Point[2];
for (int iangle = 0; iangle < 360;
iangle += 6)
{
pt[0].X = 0;
pt[0].Y = 150;
RotatePoint(pt, 1, iangle);
pt[1].X = pt[1].Y = (iangle % 5 ==
0 ? 10 : 5);
pt[0].X -= pt[1].X / 2;
pt[0].Y -= pt[1].Y / 2;
Pen p = new
Pen(Color.FromArgb(255, 0, 0, 0));
e.DrawEllipse(p, pt[0].X, pt[0].Y,
pt[1].X, pt[1].Y);
e.FillEllipse(new
SolidBrush(Color.FromArgb(255, 0, 0, 0)), pt[0].X,
pt[0].Y, pt[1].X, pt[1].Y);
}
}
private void DrawHands(Graphics e,
DateTime dt, bool change, Color c)
{
Point[,] temmppt = new Point[3, 2];
int[] iangle = new int[3];
iangle[0] = (int)((dt.Hour * 30) %
360 + dt.Minute / 2);
iangle[1] = (int)(dt.Minute * 6);
iangle[2] = (int)(dt.Second * 6);
temmppt =
(Point[,])hands_coord.Clone();
for (int i = change ? 0 : 2; i < 3;
i++)
{
Point[] tt = { temmppt[i, 0],
temmppt[i, 1] };
RotatePoint(tt, 2, iangle[i]);
Pen p = new Pen(c);
e.DrawLine(p, tt[0], tt[1]);
}
}
private void Form1_Resize(object
sender, EventArgs e)
{
Invalidate(); //position in center
}
private void timer1_Tick(object sender,
EventArgs e)
{
cur = DateTime.Now;
change = cur.Hour != prev.Hour ||
cur.Minute != prev.Minute;
DrawHands (this.CreateGraphics
(),cur ,change ,Color.FromArgb
(255,255,255,255));
DrawHands(this.CreateGraphics(),
cur, true, Color.FromArgb(255,0,0,0));
prev = cur;
Invalidate();
}
private void btnDisplay_Click(object
sender, EventArgs e)
{
Close();
}
}
}
OUTPUT

analog clock C#

  • 1.
    Kurdistan Region-Iraq Cihan University-Duhok Departmentof computer 3rd Year ComputerGraphics Analog Clock Prepare by : Omid Mustefa Supervised : Dr.Salim
  • 2.
    INTRODUCTION This project isabout how to create an Analog clock application for windows by using computer graphics C# language .
  • 3.
    Source Code using System; usingSystem.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace clock { public partial class Form1 : Form { Point[,] hands_coord = new Point[3, 2] { {new Point(0,0),new Point (0,120)}, {new Point (0,0),new Point (0,140)}, {new Point (0,0),new Point (0,140)} }; DateTime cur; DateTime prev; Boolean change; Graphics g;
  • 4.
    public Form1() { InitializeComponent(); } private voidRotatePoint(Point[] pt, int iRotate, int iangle) { Point ptTemp = new Point(0, 0); for (int i = 0; i < iRotate; i++) { ptTemp.X = (int)(pt[i].X * Math.Cos(2 * Math.PI * iangle / 360) - pt[i].Y * Math.Sin(2 * Math.PI * iangle / 360)); ptTemp.Y = (int)(pt[i].Y * Math.Cos(2 * Math.PI * iangle / 360) + pt[i].X * Math.Sin(2 * Math.PI * iangle / 360)); pt[i] = ptTemp; } } private void Form1_Load(object sender, EventArgs e) { }
  • 5.
    private void Form1_Paint(objectsender, PaintEventArgs e) { e.Graphics.TranslateTransform(this.Size.Width / 2, this.Size.Height / 2); e.Graphics.RotateTransform(150); DrawClock(e.Graphics); DrawHands(e.Graphics, prev, true, Color.FromArgb(250, 0, 0, 0)); } private void DrawClock(Graphics e) { Point[] pt = new Point[2]; for (int iangle = 0; iangle < 360; iangle += 6) { pt[0].X = 0; pt[0].Y = 150; RotatePoint(pt, 1, iangle); pt[1].X = pt[1].Y = (iangle % 5 == 0 ? 10 : 5); pt[0].X -= pt[1].X / 2; pt[0].Y -= pt[1].Y / 2; Pen p = new Pen(Color.FromArgb(255, 0, 0, 0)); e.DrawEllipse(p, pt[0].X, pt[0].Y, pt[1].X, pt[1].Y); e.FillEllipse(new SolidBrush(Color.FromArgb(255, 0, 0, 0)), pt[0].X, pt[0].Y, pt[1].X, pt[1].Y); } }
  • 6.
    private void DrawHands(Graphicse, DateTime dt, bool change, Color c) { Point[,] temmppt = new Point[3, 2]; int[] iangle = new int[3]; iangle[0] = (int)((dt.Hour * 30) % 360 + dt.Minute / 2); iangle[1] = (int)(dt.Minute * 6); iangle[2] = (int)(dt.Second * 6); temmppt = (Point[,])hands_coord.Clone(); for (int i = change ? 0 : 2; i < 3; i++) { Point[] tt = { temmppt[i, 0], temmppt[i, 1] }; RotatePoint(tt, 2, iangle[i]); Pen p = new Pen(c); e.DrawLine(p, tt[0], tt[1]); } } private void Form1_Resize(object sender, EventArgs e) { Invalidate(); //position in center }
  • 7.
    private void timer1_Tick(objectsender, EventArgs e) { cur = DateTime.Now; change = cur.Hour != prev.Hour || cur.Minute != prev.Minute; DrawHands (this.CreateGraphics (),cur ,change ,Color.FromArgb (255,255,255,255)); DrawHands(this.CreateGraphics(), cur, true, Color.FromArgb(255,0,0,0)); prev = cur; Invalidate(); } private void btnDisplay_Click(object sender, EventArgs e) { Close(); } } }
  • 8.