2017年3月28日 星期二

Unit 6

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Week6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Random rd = new Random();//宣告亂數種子
        int x, y, w, h;//(x,y)左上座標; w:寬
        Point[] po = new Point[20];
        Pen pe1; //畫筆顏色
        Brushes br; //填滿色彩
        Font fo; //文字
        Image im1; //影像
        Image[] im = new Image[4]; //影像陣列
        //外部變數
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Width = 960;//視窗寬高
            this.Height = 680;
            //在視窗中央畫十字交叉的線,中央畫綠色實心矩形寬240,高160
            //Color.FromArgb(透明度, 紅色(0-255), 綠色, 藍色)
            pe1 = new Pen(Color.FromArgb(100, 120, 0, 240), 4);
            fo = new Font("微軟正黑體", 36);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.DoubleBuffered = true;
            e.Graphics.DrawLine(pe1, new Point(0,this.Height/2),
                new Point(this.Width, this.Height / 2));
            e.Graphics.DrawLine(pe1, new Point(this.Width/2,0),
                new Point(this.Width/2, this.Height));

2017年3月21日 星期二

Unit 5 DrawLine, DrawLines, DrawArc,

Unit 5 DrawLine, DrawLines, DrawArc,  鍵盤事件 (畫線、畫弧線)


2017/03/22
X=0,y=0 (0,0)視窗最左上角的點


(this.Width,this.Height)最右下角的點

Form1_Load():程式執行第一個執行的事件,只執行一次,通常設定變數初值或環境設定(視窗寬高,最大化,計時器啟動,...)
this 表作用視窗
//宣告點的陣列含5個初值(存五個點) (Point表整數,Point F表浮點數可用小數)
Point[] p = new Point[5]; //Pen表線型,Brush塗色

設定線型pe1為紅色5點寬
Pen pe1 = new Pen(Color.Red, 5);
如何宣告一個含有5個點的陣列
Point[] p = new Point[5];
給定點陣列p,如何畫連續線(其中線型顏色線寬為pe1)
e.Graphics.DrawLines(pe1, p);
給定點陣列p,如何畫弧線(其中線型顏色線寬為pe2)
e.Graphics.DrawCurve(pe2, p);
填色
e.Graphics.FillClosedCurve(Brushes.LightSeaGreen, p); //Brushes表很多顏色
宣告亂數種子rd
Random rd = new Random();
亂數範圍100~800
rd.Next(100, 800)
5個點陣列初值亂數設定
for (int i = 0; i < p.Length; i++) //陣列長度個數
p[i] = new Point(rd.Next(100, 800), rd.Next(100, 600));//設定xy座標為亂數

根據點陣列p顯示填滿的封閉區域(色彩值自訂)3%/5%
根據點陣列p每隔0.5秒顯示填滿的封閉區域(色彩值自訂)5%/5%
根據點陣列p每隔0.5秒顯示填滿的封閉區域(色彩值亂數)7%/5%
(hint: 利用Brush型態的陣列)
宣告陣列 這些填滿的區域

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Week5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //宣告點的陣列含5個初值(存五個點)(Point表整數,Point F表浮點數可用小數)
        Point[] p = new Point[5];
        Pen pe1 = new Pen(Color.Red, 5); //設定圖刷紅色,5個像點,Pen表線型,Brush塗色
        Pen pe2 = new Pen(Color.Blue,10);
        Random rd = new Random();//宣告亂數種子rd
        Brush[] br = new Brush[3];
        private void Form1_Load(object sender, EventArgs e)
        //程式執行第一個執行的事件,只執行一次,通常設定變數初值或環境設定(視窗寬高,最大化,計時器啟動,...)
        {
            this.Width = 960; this.Height = 720; //表作用視窗
            br[0] = Brushes.RoyalBlue;
            br[1] = Brushes.Purple;
            br[2] = Brushes.Olive;
            p[0] = new Point(240, 310);
            p[1] = new Point(290, 180);
            p[2] = new Point(390, 120);
            p[3] = new Point(480, 200);
            p[4] = new Point(200, 510);
           
         
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.DoubleBuffered = true; //避免閃爍
            e.Graphics.DrawLines(pe1, p);//給定點陣列p,畫連續線(其中線型顏色線寬為pe1)
            e.Graphics.DrawCurve(pe2, p);//給定點陣列p,畫弧線(其中線型顏色線寬為pe2)
            e.Graphics.FillClosedCurve(br[rd.Next(0,3)], p); //Brushes表很多顏色
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < p.Length; i++) //陣列長度個數
                p[i] = new Point(rd.Next(100, 800), rd.Next(100, 600));//設定xy座標為亂數
            this.Refresh();
        }
        
    }

}

2017年3月14日 星期二

Unit 4 C# Pen & ContextMenu

Unit 4 C# Pen & ContextMenu
結構=資料巨集


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Paint1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Pen pe0, pe1; //3-1.要先宣告pe0,pe1
        //5-1.宣告5000個元素的整數陣列(表訊號強度)(靜態陣列)
        int[] s = new int[5000]; //5-1. 型態(配置一個大樓有5000個房間)
        Random rd = new Random(); //5-2.設定亂數物件
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Width = 960; this.Height = 720; //1.視窗寬高
            //2.定義線型物件(色彩粗細)pe0
            pe0 = new Pen(Color.Blue, 2);
            //3.指定線型物件屬性(如起始端點,結束端點樣式,...)
            pe0.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
            pe1 = new Pen(Color.BlueViolet, 1);
            for (int i = 0; i < 5000; i++)//5for迴圈
                s[i] = rd.Next(100, 350); //5-3. 100~350之間的整數亂數
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.DoubleBuffered = true;//4.避免閃爍
            //3-2畫線DrawLine(線型, x0,y0,x1,y1);
            //畫線DrawLine(線型,new Point (x0,y0), new Point(x1,y1));
            e.Graphics.DrawLine(pe0, 50, 400, 600, 400);//第一條線
            e.Graphics.DrawLine(pe0, new Point(50, 400),
                new Point(50, 50));
            Point[] pt1 = new Point[500];//5-4. 500個點
            for (int i = 0; i < 500; i++)
            {
                pt1[i].X = i + 100;
                pt1[i].Y = s[i];
            }
            e.Graphics.DrawLines(pe1, pt1);
        }
    }
}


課堂練習: 讓畫布每秒往左邊移動

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Paint1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Pen pe0, pe1; //3-1.要先宣告pe0,pe1
        //5-1.宣告5000個元素的整數陣列(表訊號強度)(靜態陣列)
        int[] s = new int[5000]; //5-1. 型態(配置一個大樓有5000個房間)
        Random rd = new Random(); //5-2.設定亂數物件
        Point[] pt1 = new Point[500];//5-4. 500個點
        int k = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Width = 960; this.Height = 720; //1.視窗寬高
            //2.定義線型物件(色彩粗細)pe0
            pe0 = new Pen(Color.Blue, 2);
            //3.指定線型物件屬性(如起始端點,結束端點樣式,...)
            pe0.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
            pe1 = new Pen(Color.BlueViolet, 1);
            for (int i = 0; i < 5000; i++)//5.for迴圈
                s[i] = rd.Next(100, 350); //5-3. 100~350之間的整數亂數
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.DoubleBuffered = true;//4.避免閃爍
            //3-2.畫線DrawLine(線型, x0,y0,x1,y1);
            //畫線DrawLine(線型,new Point (x0,y0), new Point(x1,y1));
            e.Graphics.DrawLine(pe0, 50, 400, 600, 400);//第一條線
            e.Graphics.DrawLine(pe0, new Point(50, 400),
                new Point(50, 50));
            e.Graphics.DrawLines(pe1, pt1);
        }
       
        private void timer1_Tick(object sender, EventArgs e)
        {
             for (int i = 0; i < 500; i++)
            {
                pt1[i].X = i + 100;
                pt1[i].Y = s[i+k];//讓畫布每秒往左邊移動
               
            }
             k++;
             this.Refresh();//6-1要求畫布重畫
        }
    }
}