9cubed
ブログ | Tailwind | Vite | Python | Node.js | Linux | PowerShell | その他 | 将棋ウォーズ | 歌の練習
プログラミング講座

第20回 アナログ時計の作成

公開日:2014-10-14
更新日:2019-05-11

1. 概要

独り言によるプログラミング講座「第20回 アナログ時計の作成」です。
アナログ時計を作成しながら説明しています。

2. 動画



3. 動画中に書いたソース

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime d = DateTime.Now;
            label1.Text = d.ToLongTimeString();

            //画面を消す
            Graphics g = this.CreateGraphics();
            g.Clear(Color.Black);
            g.Dispose();

            //針を描く
            draw(6, Pens.Aqua, d.Second, 100); //秒
            draw(6, Pens.Yellow, d.Minute, 80);  //分
            draw(30, Pens.Pink, d.Hour, 50);    //時
        }

        private void draw(int 倍率, Pen pen, int time, int 針の長さ) {

            int cx = 150;
            int cy = 150;

            //原点
            int x1 = cx;
            int y1 = cy;

            //針
            double 角度1度あたり = Math.PI / 180;
            double 角度 = 角度1度あたり * 倍率 * time - Math.PI / 2;

            
            int x2 = cx + (int)(Math.Cos(角度) * 針の長さ);
            int y2 = cy + (int)(Math.Sin(角度) * 針の長さ);

            Graphics g = this.CreateGraphics();
            g.DrawLine(pen, x1, y1, x2, y2);
            g.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            //g.Clear(Color.Black);
            g.DrawLine(Pens.Black, 0, 0, 100, 200);
            g.Dispose();
        }
    }
}


YouTube X

新着一覧

  • SCSS のインストールVite
  • Tailwind CSS のプロジェクトの作成Tailwind
  • TypeScriptのプロジェクトの作成Vite
  • Flask のインストールと動作確認Python
  • 簡易Webサーバーの作成Python
  • pipeline で文章の生成Python
  • pipeline で文章の要約Python
  • 音声から文字起こしPython
  • Node.js のインストールNode.js
  • .ps1(PowerShellスクリプト)を実行可能にするPowerShell

アーカイブ

  • 2025/12
  • 2025/11
  • 2025/10
  • 2025/09
  • 2025/08

以前のカテゴリー一覧

  • CakePHP3
  • CentOS7
  • HTML・CSS・JavaScript
  • Haskell
  • JavaScript
  • Kotlin
  • Laravel5
  • PHP
  • Python
  • Ruby
  • RubyOnRails5
  • TypeScript
  • Vue.js
  • Webサーバ講座
  • Webプログラミング講座
  • jQuery
  • linux
  • パソコン講座
  • ブログ
  • プログラミング講座
  • メモ帳作成講座
  • 数学

Copyright © 9cubed. All Rights Reserved.

プライバシーポリシー 利用規約
▲