C#でグラフを描く

 

 

@新規プロジェクトを作成

 

 

A自動生成されたフォームのサイズ関連ののプロパティをいじる

 (今回はすべて720,480にする)

 

 

A左の方にあるツールボックスから”ボタン”、”ピクチャーボックス”、”リストボックス”を選んで

フォームに貼り付ける

 

 

B ピクチャーボックスの”Size”プロパティを 640,480、”Location”(表示位置)を0,0にする

 

Cするとこんな画面ができるはずです

Dボタンをタブルクリックすると下のコードの赤い部分、ボタンが押されたときのイベントが自動生成されるので

その中にグラフを描くコード(今回は”y=0.075 x X^2”という2次関数)を記述します

 

 

Eグレーの部分は自動生成コード、色つきの部分か今回記述したコード

 

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

 

 

 

 

namespace C_ver

 

 

{

       /// <summary>

       /// Form1 の概要の説明です。

       /// </summary>

       public class Form1 : System.Windows.Forms.Form

       {

              private System.Windows.Forms.Button button1;

              private System.Windows.Forms.Label label1;

              private System.Windows.Forms.PictureBox pictureBox1;

              private System.Windows.Forms.ListBox listBox1;

              /// <summary>

              /// 必要なデザイナ変数です。

              /// </summary>

              private System.ComponentModel.Container components = null;

 

              public Form1()

 

              {     

                    

                    

                    

 

                    

                    

                     //

                     // Windows フォーム デザイナ サポートに必要です。

                     //

                     InitializeComponent();

 

                     //

                     // TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。

                     //

              }

 

              /// <summary>

              /// 使用されているリソースに後処理を実行します。

              /// </summary>

              protected override void Dispose( bool disposing )

              {

                     if( disposing )

                     {

                           if (components != null)

                           {

                                  components.Dispose();

                           }

                     }

                     base.Dispose( disposing );

              }

 

              #region Windows フォーム デザイナで生成されたコード

              /// <summary>

              /// デザイナ サポートに必要なメソッドです。このメソッドの内容を

              /// コード エディタで変更しないでください。

              /// </summary>

              private void InitializeComponent()

              {

                     this.button1 = new System.Windows.Forms.Button();

                     this.label1 = new System.Windows.Forms.Label();

                     this.pictureBox1 = new System.Windows.Forms.PictureBox();

                     this.listBox1 = new System.Windows.Forms.ListBox();

                     this.SuspendLayout();

                     //

                     // button1

                     //

                     this.button1.BackColor = System.Drawing.SystemColors.Control;

                     this.button1.Location = new System.Drawing.Point(651, 373);

                     this.button1.Name = "button1";

                     this.button1.Size = new System.Drawing.Size(56, 24);

                     this.button1.TabIndex = 0;

                     this.button1.Text = "button1";

                     this.button1.Click += new System.EventHandler(this.button1_Click);

                     //

                     // label1

                     //

                     this.label1.Location = new System.Drawing.Point(200, 59);

                     this.label1.Name = "label1";

                     this.label1.TabIndex = 1;

                     this.label1.Text = "label1";

                     //

                     // pictureBox1

                     //

                     this.pictureBox1.BackColor = System.Drawing.SystemColors.HighlightText;

                     this.pictureBox1.Location = new System.Drawing.Point(0, 0);

                     this.pictureBox1.Name = "pictureBox1";

                     this.pictureBox1.Size = new System.Drawing.Size(640, 480);

                     this.pictureBox1.TabIndex = 2;

                     this.pictureBox1.TabStop = false;

                     //

                     // listBox1

                     //

                     this.listBox1.ItemHeight = 12;

                     this.listBox1.Location = new System.Drawing.Point(644, 15);

                     this.listBox1.Name = "listBox1";

                     this.listBox1.ScrollAlwaysVisible = true;

                     this.listBox1.Size = new System.Drawing.Size(85, 340);

                     this.listBox1.TabIndex = 3;

                     this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);

                     //

                     // Form1

                     //

                     this.AccessibleRole = System.Windows.Forms.AccessibleRole.OutlineItem;

                     this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);

                     this.BackColor = System.Drawing.SystemColors.InactiveBorder;

                     this.ClientSize = new System.Drawing.Size(732, 456);

                     this.Controls.Add(this.listBox1);

                     this.Controls.Add(this.pictureBox1);

                     this.Controls.Add(this.label1);

                     this.Controls.Add(this.button1);

                     this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;

                     this.MaximumSize = new System.Drawing.Size(740, 480);

                     this.MinimumSize = new System.Drawing.Size(740, 480);

                     this.Name = "Form1";

                     this.Text = "Form1";

                     this.Load += new System.EventHandler(this.Form1_Load);

                     this.ResumeLayout(false);

 

              }

              #endregion

 

              /// <summary>

              /// アプリケーションのメイン エントリ ポイントです。

              /// </summary>

              [STAThread]

 

 

 

              //-----------------------------------------------------------

              public void DrawLineFloat(PaintEventArgs e)

              {

                     // Create pen.

                     Pen blackPen = new Pen(Color.Black, 3);

                     // Create coordinates of points that define line.

                     float x1 = 100.0F;

                     float y1 = 100.0F;

                     float x2 = 500.0F;

                     float y2 = 100.0F;

                     // Draw line to screen.

                     e.Graphics.DrawLine(blackPen, x1, y1, x2, y2);

              }

             

              //-----------------------------------------------------------

 

 

 

 

              static void Main()

              {

                     Application.Run(new Form1());

              }

 

              private void Form1_Load(object sender, System.EventArgs e)

              {

      

              }

 

 

 

 

              //-----------------------------------------------------------

              private void button1_Click(object sender, System.EventArgs e)

              {

 

 

                    

                     double[,] H = new double[640,100];//y座標の値をいれる配列を宣言

 

                     int iii;

 

 

                     //計算

                     for(iii=0;iii<640;iii++){int n=-320;

                                                              //H [iii,1]= iii*0.75;}

                                                              H [iii,1]= (iii+n)*(iii+n)*0.0075;}

 

                     //Graphicsオブジェクトの作成

                     Graphics g = pictureBox1.CreateGraphics();

 

                     //Penオブジェクトの作成(幅2の黒色)

                     Pen blackPen   = new Pen(Color.Black, 2);

 

 

                     //listBoxとy座標の値が入ってる配列を一体化

                     System.Object[] ItemObject = new System.Object[640];

                     for (int i = 0; i <= 639; i++)

                     {

                           ItemObject[i] = "H=" + H[i,1];

                     }

                     listBox1.Items.AddRange(ItemObject);

 

 

                     //描画

                     for(iii=1;iii<640;iii++)

                     {

                           //明示的な型変換 double → int

                           int   j = (int)H [iii-1,1];

                           int   k = (int)H [iii,1];

 

                           g.DrawLine(blackPen, iii-2,(480- j), iii,(480- k));

                     }

 

 

 

 

 

                     //リソースを開放する

                     blackPen.Dispose();

                     g.Dispose();

              }

 

              private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)

              {

             

              }

 

 

 

 

 

       }

}

 

 

Fコンパイルして実行し、ボタンを押すとグラフが表示され下の画面になります

  リストボックスにはy座標の値が表示されます。