Posted by : Izumikawa Fukumi 2013年11月17日日曜日

今回JavaScriptに続き、C#でも線を描画してみました。

描画方法は至って簡単です。
もう既にJavaScriptバージョンをお読みになった方は直ぐに分かると思います。

JavaScriptで描画してみた。





ではまずソースをご覧下さい。

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 描画
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //オブジェクトの作成
            Graphics g = this.CreateGraphics();

            //pen作成
            Pen blackPen = new Pen(Color.Red, 5);

            //lineの始点と終点
            Point Start_point1 = new Point(50, 40);
            Point End_point1 = new Point(250, 40);

            //lineを描画
            g.DrawLine(blackPen, Start_point1, End_point1);

            //penを解放
            blackPen.Dispose();
        }
    }
}





ここでは描画の基礎はJavaScript編だとして引数について教えます。

Pen blackPen = new Pen(Color.Red, 5);」では線の色と太さを設定しています。
Pen(Color.線の色, 太さ);」の様にします。

Point Start_point1 = new Point(50, 40);」は線の開始点です。JavaScriptで言う「moveTo()」です。
Point End_point1 = new Point(250, 40);」は線の終了点です。JavaScriptで言う「lineTo()」です。

これらの情報を全てまとめるのが、


g.DrawLine(blackPen, Start_point1, End_point1);


です。

g.DrawLine(色や太さ, 開始点, 終了点);


今回の方法はとてもシンプルでただformに線を描画すると言う物です。

他にも点線などの線も描けるようです。






■追記

この記事の投稿後ちょっといじったらやはりアニメーションの様な効果も出来ました。


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 描画
{
    public partial class Form1 : Form
    {
        private int sec = 1;
        private int scv = 1;
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (sec < 90)
            {
                sec++;
                //オブジェクト作成
                Graphics g = this.CreateGraphics();

                Pen black = new Pen(Color.Red, 5);
                Point Stato = new Point(10, 10);
                Point Endo = new Point(10, sec + 0);

                g.DrawLine(black, Stato, Endo);
                black.Dispose();
            }
            else if (scv < 90)
            {
                scv++;
                //オブジェクト作成
                Graphics g = this.CreateGraphics();

                Pen black = new Pen(Color.Red, 5);
                Point Stato = new Point(10, 10);
                Point Endo = new Point(scv + 0, 10);

                g.DrawLine(black, Stato, Endo);
                black.Dispose();
            }
            else
            {
                timer1.Enabled = false;
            }
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            timer1.Interval = 10;
            timer1.Enabled = true;
        }
    }
}



- Copyright © I aim to creator. - Hatsune Miku - Powered by Blogger - Designed by Johanes Djogan -