Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace majcher_rynski_projekt
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         Timer timer = new Timer();
  16.         int width = 300, height = 300, secHand = 140, minHand = 110, hrHand = 80;
  17.  
  18.         int cx, cy;
  19.  
  20.         Bitmap bitmap;
  21.         Graphics g;
  22.         public Form1()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.  
  27.         private void Form1_Load(object sender, EventArgs e)
  28.         {
  29.             bitmap = new Bitmap(width + 1, height + 1);
  30.  
  31.             cx = width / 2;
  32.             cy = height / 2;
  33.  
  34.             this.BackColor = Color.White;
  35.  
  36.             timer.Interval = 1000;
  37.             timer.Tick += new EventHandler(this.timer_tick);
  38.             timer.Start();
  39.         }
  40.  
  41.         private void timer_tick(object sender, EventArgs e)
  42.         {
  43.             g = Graphics.FromImage(bitmap);
  44.  
  45.             int ss = DateTime.Now.Second;
  46.             int mm = DateTime.Now.Minute;
  47.             int hh = DateTime.Now.Hour;
  48.  
  49.             int[] handCoord = new int[2];
  50.  
  51.  
  52.             g.Clear(Color.White);
  53.  
  54.             g.DrawEllipse(new Pen(Color.Black, 1f), 0, 0, width, height);
  55.  
  56.             g.DrawString("12", new Font("Arial", 12), Brushes.Black, new Point(140, 2));
  57.             g.DrawString("3", new Font("Arial", 12), Brushes.Black, new Point(286, 140));
  58.             g.DrawString("6", new Font("Arial", 12), Brushes.Black, new Point(142, 282));
  59.             g.DrawString("9", new Font("Arial", 12), Brushes.Black, new Point(0, 140));
  60.  
  61.             handCoord = ms_coord(ss, secHand);
  62.             g.DrawLine(new Pen(Color.Red, 1f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));
  63.  
  64.             handCoord = ms_coord(mm, minHand);
  65.             g.DrawLine(new Pen(Color.Black, 2f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));
  66.  
  67.             handCoord = hr_coord(hh%12, mm, hrHand);
  68.             g.DrawLine(new Pen(Color.Gray, 3f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));
  69.  
  70.             pictureBox1.Image = bitmap;
  71.  
  72.             this.Text = "Obecny czas: " + hh + ":" + mm + ":" + ss;
  73.  
  74.             g.Dispose();
  75.         }
  76.  
  77.         private int[] ms_coord(int val, int hlen)  //KOORDYNATY DLA SEKUND I MINUT
  78.         {
  79.             int[] coord = new int[2];
  80.             val *= 6;     //kazda minuta i sekunda to 6 stopni
  81.  
  82.             if (val >= 0 && val <= 180)
  83.             {
  84.                 coord[0] = cx + (int)(hlen * Math.Sin(Math.PI * val / 180));
  85.                 coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));
  86.             }
  87.             else
  88.             {
  89.                 coord[0] = cx - (int)(hlen * -Math.Sin(Math.PI * val / 180));
  90.                 coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));
  91.             }
  92.             return coord;
  93.         }
  94.         private int[] hr_coord(int hval, int mval, int hlen)
  95.         {
  96.             int[] coord = new int[2];
  97.  
  98.             int val = (int)((hval * 30) + (mval * 0.5));   //Kazda godzina to 30 stopni
  99.  
  100.             if (val >= 0 && val <= 180)
  101.             {
  102.                 coord[0] = cx + (int)(hlen * Math.Sin(Math.PI * val / 180));
  103.                 coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));
  104.             }
  105.             else
  106.             {
  107.                 coord[0] = cx - (int)(hlen * -Math.Sin(Math.PI * val / 180));
  108.                 coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));
  109.             }
  110.             return coord;
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement