2017年5月16日 星期二

Unit 13 小影像動畫

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 小影像動畫1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int x=300, y=300; //x,y為小影像中心
        int r=0, c=0; //小圖列數行數
        int w, h; //大影像寬高
     
        Image im1 = Properties.Resources.wGirl;
        Rectangle[,] im = new Rectangle[4, 4];
        //設定小影像,im二維陣列,型態先給,四層樓同一個門牌號碼,
        //陣列從0開始,第0,第一,第二,第三...
        //影像變數為屬性.資源.檔案名稱
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Width = 960; this.Height = 640;
            this.DesktopLocation = new Point(5, 10);
            //設定在桌面位置
           w = im1.Width / 4; h = im1.Height / 4;
            //小圖寬高為大圖寬高/4
            for (int i=0;i<4;i++)
            for(int j=0;j<4;j++)
                im[i,j]=new Rectangle(j*w,i*h,w,h);
            //i為列,j為行,跑兩個for迴圈
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.DoubleBuffered = true;
            e.Graphics.DrawImage(im1, 64, 64);
            //顯示影像局部矩形區域
            //e.Graphics.DrawImage(im, 左上x,左上y,矩形區域,GraphicsUnit.Pixel)
            e.Graphics.DrawImage(im1, x-r/2, y-c/2,im[r,c],GraphicsUnit.Pixel);
            //e表繪圖畫布變數的指令,
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            c++;
            c=c%4;
            //c為0,1,2,3,0,1,2,3
            this.Refresh();
        }

        private void label1_Click(object sender, EventArgs e)
        {
         
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
            { r = 1; x = x - 3; }
            else if (e.KeyCode == Keys.Right)
            { r = 2; x = x + 3; }
            if (e.KeyCode == Keys.Up)
            { r = 3; y = y - 3; }
            else if (e.KeyCode == Keys.Down)
            { r = 0; y = y + 3; }
            this.Refresh();
        }
    }
}

2017年5月2日 星期二

Unit 11 按鍵

Unit 11A 按鍵控制
作業
按左右鍵會增加或減少5
按上下可以增加或減少砲管r長度3
按空白鍵可以發射直徑為10的砲彈,發射口為砲管口(x,y),
0.1x位置為x+r*cos(a),y位置為y-r*sin(a)+0.5*10*t*t,其中t為累計時間

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;
using System.Drawing.Drawing2D; //擴增定義,線型
namespace 按鍵01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //宣告變數,畫筆顏色,亂數
        int x1 = 100, y1 = 600, x2, y2, a=90, r1=60, r2,p=0,q=0;
        int x3 = 300, y3 = 300, r3 = 100;
        double t = 0.1;
        Pen pe1, pe2; //線型顏色線寬樣式
        Random rd = new Random();

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Width = 1024; this.Height = 640;
            pe1 = new Pen(Color.Purple, 10);
            //最前面新增using System.Drawing.Drawing2D;
            //線結束端點.EndCap樣式,起始端點.StartCap
            pe1.EndCap = LineCap.SquareAnchor;//Drawing2D
            pe1.StartCap = LineCap.SquareAnchor;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.DoubleBuffered = true;
                        //.FillRectangle(Brushes.色彩, 左上x, 左上y, , )
            e.Graphics.FillRectangle(Brushes.Gold, x1 - 60, y1 - 20, 60 * 2, 30);
            e.Graphics.FillRectangle(Brushes.Goldenrod, x1 - 30, y1 - 40, 30 * 2, 20);
            //1=1*3.1416/180 弳度
            x2 = x1+(int)(r1 * Math.Cos(a * Math.PI / 180)); //用三角函數算,轉換成整數
            y2 = y1-50-(int)(r1 * Math.Sin(a * Math.PI / 180));
            e.Graphics.DrawLine(pe1, x1, y1 - 50, x2, y2);
            e.Graphics.FillEllipse(Brushes.Black,x2-5+p,y2-5-q,10,10);
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            //e.KeyCode:按鍵碼
            if (e.KeyCode == Keys.Left)
                a=a+10;
            else if(e.KeyCode==Keys.Right)
               a = a-10;
            if (e.KeyCode == Keys.Up)
            r1 = r1 + 3;
            else if (e.KeyCode == Keys.Down)
            r1 = r1 - 3;
            if (e.KeyCode == Keys.Space)
                timer1.Enabled = true;
            this.Refresh();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            p = x2 + (int)(r1 * Math.Cos(a * Math.PI / 180));
            q = y2 -50- (int)(r1 * Math.Sin(a * Math.PI / 180)+0.5*10*t*t) ;
            t += 0.1;
            this.Refresh();
        }
    }
}


2017年4月25日 星期二

Unit 10 影像/檔案讀取

Unit 10A
檔案讀取視窗
//開啟檔案讀取視窗,成功則將檔案指定給字串fn
            string fn = ""; //""為空字串
            if (openFileDialog1.ShowDialog() == DialogResult.OK)//顯示對話方塊
                fn = openFileDialog1.FileName;

Unit 10B
Image影像檔案讀取
            //宣告影像變數讀取檔案
            Image im1= Image.FromFile(fn); 
Unit 10C
指定繪圖畫布,繪出影像
//建立f2視窗的繪圖畫布(指定畫布在左上角0,0的位置)
            Graphics g = f2.CreateGraphics();
            g.DrawImage(im1, 0, 0);
Unit 10D
新設視窗
Form f2 = new Form();
//顯示視窗
f2.Show();

練習題:
//button開啟檔案後顯示在新視窗,視窗大小為影像大小
//定義30Point型態的座標資料,按button後在新視窗顯示這些點,這些點以小圓點表示,圓心為Point座標,直徑為4

            int rdX, rdY;
            Form f3 = new Form();
            f3.Width = 800;
            f3.Height = 480;
            f3.Show();
            Graphics g= f3.CreateGraphics();
            for (int i = 0; i < 30; i++)
            {
                rdX = rd.Next(50, 750);
                rdY = rd.Next(50, 430);
                g.FillEllipse(Brushes.Red, rdX, rdY, 4, 4);
                String str = "(" + rdX + "," + rdY + ")";
                g.DrawString(str, new System.Drawing.Font("Arial", 8), Brushes.Black, (float)rdX, (float)rdY);
               
            }
++++++++++++++++++++++++++++++++++++++++++


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 影像讀取
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //開啟檔案讀取視窗,成功則將檔案指定給字串fn
            string fn = ""; //""為空字串
            if (openFileDialog1.ShowDialog() == DialogResult.OK)//顯示對話方塊
                fn = openFileDialog1.FileName;
            label1.Text = fn;

            //宣告影像變數讀取檔案
            Image im1= Image.FromFile(fn);

            //新設視窗(做自己的繪圖畫布,建立視窗)
            //練習:讓選取的影像大小等於顯示視窗的大小
            Form f2 = new Form();
            f2.Width = im1.Width;
            f2.Height = im1.Height;
            f2.BackColor = Color.FromArgb(126, 60, 255);
            //顯示視窗
            f2.Show();

            //建立f2視窗的繪圖畫布(指定畫布在左上角0,0的位置)
            Graphics g = f2.CreateGraphics();
            g.DrawImage(im1, 0, 0);


         
        }
Random rd = new Random();
       Point[] p = new Point[30];

        private void button2_Click(object sender, EventArgs e)
        {
            //定義30個Point型態的座標資料,按button後在新視窗顯示這些點,
            //這些點以小圓點表示,圓心為Point座標,直徑為4
            //g.FillEllipse(Brushes.Red, 50, 100, 4, 4);
            int rdX, rdY;
            Form f3 = new Form();
            f3.Width = 800;
            f3.Height = 480;
            f3.Show();
            Graphics g= f3.CreateGraphics();
            for (int i = 0; i < 30; i++)
            {
                rdX = rd.Next(50, 750);
                rdY = rd.Next(50, 430);
                g.FillEllipse(Brushes.Red, rdX, rdY, 4, 4);
                String str = "(" + rdX + "," + rdY + ")";
                g.DrawString(str, new System.Drawing.Font("Arial", 8), Brushes.Black, (float)rdX, (float)rdY);
             
            }
                   private void button3_Click(object sender, EventArgs e)
        {
            int rd1X, rd1Y;
            Form f4 = new Form();
            f4.Width = 800;
            f4.Height = 480;
            f4.Show();
            Graphics g = f4.CreateGraphics();
            for (int i = 0; i < 10; i++)
            {
                rd1X = rd.Next(0, 800);
                rd1Y = rd.Next(50, 430);
                g.FillRectangle(Brushes.BlueViolet, rd1X, rd1Y, 3, 480);

            }


        }
    }
}

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();
        }
        
    }

}