2017年6月13日 星期二

Unit 17A 類別定義與動畫&影像局部放大

Unit 17A 類別定義與動畫
1.   設定視窗為(720,480),載入大地影像(至少1024*1024)
2.   將人物影像擺在視窗正中央
3.   以畫布平移方式,e.Graphics.TranslateTransform(x,y);按上下左右可以移動背景,人物永遠在視窗中央

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Image im1 = Properties.Resources.f1;
        int x, y;
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Width = 720; this.Height = 480;
            this.DesktopLocation = new Point(10, 10);
            x=(720-im1.Width)/2;y=(480-im1.Height)/2;

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.DoubleBuffered=true;
            e.Graphics.TranslateTransform(x, y);
            e.Graphics.DrawImage(im1,x,y);
            e.Graphics.ResetTransform(); //繪圖畫布還原
            e.Graphics.FillEllipse(Brushes.Red, (720 - 64) / 2, (480 - 64) / 2, 64, 64);

        }

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


Unit 17B
影像局部放大

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 Unit_17B
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int x1, y1, x2, y2;
        int w = 96, h = 80;
        int x3=300, y3=150;
        int r = 3;
        Image im1 = Properties.Resources.wattch2;
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Width = 960; this.Height = 640;
            this.DesktopLocation = new Point(10, 10);

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.DoubleBuffered=true;
            e.Graphics.DrawImage(im1, 0, 0);
            //DrawRectangle(線色彩寬度,左上x,左上y,,)
            e.Graphics.DrawRectangle(new Pen(Color.Red, 3),
                x1 - 84 / 2, y1 - 64 / 2, w, h);
            //re1: 影像上的某一矩形位置左上角(300,150),矩形寬高(80,50)
            Rectangle re1 =new Rectangle(x1-w/2,y1-h/2,w,h);
            Rectangle re2= new Rectangle(600,150,w*r,h*r);
            e.Graphics.DrawImage(im1,re2,re1,GraphicsUnit.Pixel);

        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            Point p = e.Location;
            x1 = p.X; y1 = p.Y;
            this.Refresh();
        }
    }
}




2017年6月6日 星期二

Unit 16A 類別定義與動畫

Unit 16A 類別定義與動畫

1. 定義一個函數line(p)可以在新視窗顯示折線圖,
其中新視窗寬640,高480, p為整數陣列或
2.定義p為整數陣列,畫出折線圖
3.定義p為整數陣列,畫出直條圖

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 Unit_16A
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //int[] p = { 30, 90, 60, 45, 48, 54 }; //為y陣列的值
        int[] y = { 30, 90, 60, 45, 48, 54 };
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Width = 960; this.Height = 640;
            this.DesktopLocation = new Point(8, 8);
            line(y);
        }
        void line(int[] p) //僅形式,與上方的p無關
        {
            Form f2 = new Form();
            f2.Width = 640;
            f2.Height = 480;
            f2.Show();
            Graphics g = f2.CreateGraphics(); //新視窗上建立繪圖畫布
         
            //用for迴圈指定p的值
            Point[] ps = new Point[p.Length];
            for (int i = 0; i < p.Length; i++)
                ps[i] = new Point(i * 30 + 200, p[i]); //(x值,y座標)
            g.DrawLines(new Pen(Color.Red, 6), ps);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            line(y);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.DoubleBuffered = true;
            //你可以定義一個point陣列p,每個元素為x軸位置及
            //y陣列的值元素的值
           Point[] p = new Point[y.Length]; //空的陣列
            //用for迴圈指定p的值
           for (int i = 0; i < p.Length; i++)
               p[i] = new Point(i * 30+200, y[i]); //(x值,y座標)
            e.Graphics.DrawLines(new Pen(Color.Red, 6), p);
        }

        private void label1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
旋轉
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();
        }
        Image Im1 = Properties.Resources.earth;
        int x, y;
        float a; //旋轉角度
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Width = 960; this.Height = 720;
            this.DesktopLocation = new Point(8, 8);
            x = 960 / 2 - 96 / 2;
            y = 720 / 2 - 96 / 2;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            a += 3;
            this.Refresh();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.DoubleBuffered = true;
            //繪圖畫布左上角(0,0)平移到(x,y)
            e.Graphics.TranslateTransform(x,y);
            //繪圖畫布以角度a旋轉,看起來像星球在轉,其實是畫布在轉        
            e.Graphics.RotateTransform(a);
            //影像左上角(0,0)
            e.Graphics.DrawImage(Im1,0-144/2,0-144/2,144,144);//繞圓心旋轉

            //依照上次的繪圖畫布(0,0)位置平移(150,0)
            e.Graphics.RotateTransform(a);
            e.Graphics.TranslateTransform(150, 0);
            e.Graphics.DrawImage(Im1, 0-84/2, 0-84/2, 84, 84);

            e.Graphics.TranslateTransform(80, 80);
            e.Graphics.RotateTransform(a);
            e.Graphics.DrawImage(Im1, 0-64/2, 0-64/2, 64, 64);
        }
    }
}



2017年5月30日 星期二

Unit 15A 類別定義與動畫

Unit 15A 類別定義與動畫(5%~10%)
1.      點按滑鼠,在滑鼠點按位置(圓中心)畫圓(3%)
2.      產生連續爆炸圖(3%)
3.      產生爆炸圖兩次(2%)
4.      點按滑鼠,在滑鼠點按位置產生爆炸圖兩次(3%)
5.      兩物體碰撞後產生爆炸圖(3%)
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 爆炸01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Image im = Properties.Resources.fire;
        int x, y; //滑鼠點按位置
        Random rd = new Random();
        int w, h; //小圖寬高
        //宣告放小圖的二維陣列
        Rectangle[,] re = new Rectangle[3, 5];
        int r = 0, c = 0; //小圖陣列註標位置
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Width = 960; this.Height = 640;
            this.DesktopLocation = new Point(8, 8);
            w = im.Width / 5; h = im.Height / 3;
            //切割影像放在二維矩形
            for (int i= 0;i<3;i++)
                for (int j = 0; j < 5; j++)
                    re[i, j] = new Rectangle(j * w, i * h, w, h);

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.DoubleBuffered = true;
            e.Graphics.DrawImage(im, 300, 200, re[r,c],
                GraphicsUnit.Pixel);
        }
    }
}



2017年5月23日 星期二

Unit 14A 類別定義與動畫
「類別」可以有很多屬性,有「類別」比較好管理
Ex:球具有圓心、顏色、穿透性(反彈係數)…靜態特性
專案>加入類別 bomb.cs
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
{
    class bomb
    {
        public int x, y, r,mx,my, w, h; //類別成員變數: 圓心、直徑、移動方向、視窗寬高
        public Brush br; //顏色非整數
        public bomb() { } //宣告類別變數,空的建構元constructor
        public bomb(int x1, int y1, int r1, int mx1, int my1,Brush br1, int w1, int h1) //指定初值
        {
            x = x1; y = y1; r = r1; mx=mx1 ; my=my1; br = br1; w = w1; h = h1;//右邊指定給左邊
        }
        public void drawC(Graphics g)//畫圓要有畫布
        {
            g.FillEllipse(br,x-r/2,y-r/2,r,r); //(x,y)圓心
        }
        public void move()
        {
            x=x+mx; y=y+my;//mx:移動的x方向距離
            if (x+r/2>w-3) //碰右牆
                mx=-mx;
            else if (x-r/2<0) //碰左牆
                mx=-mx;
            else if (y+r/2>h) //碰視窗下緣
                my=-my;
            else if (y-r/2<0) //碰視窗上緣
                my=-my;
        }
    }
}

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();
        }
            Random rd = new Random(); //定義亂數
           
            bomb[] bo = new bomb[5]; //很多個球用中括號
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Width = 960; this.Height = 640;
            this.DesktopLocation = new Point(5, 10);
            for (int i=0;i<bo.Length; i++)    //陣列初值for迴圈
                bo[i]=new bomb(rd.Next(50,900), rd.Next(50,550),
                    rd.Next(48,120), rd.Next(-25,25),rd.Next(-25,25),
                    new SolidBrush(Color.FromArgb(rd.Next(99,255),
                        rd.Next(99,255),rd.Next(99,255))),960,640);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.DoubleBuffered = true;
            for (int i = 0; i < bo.Length; i++)
                bo[i].drawC(e.Graphics);
                    }

        private void timer1_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < bo.Length; i++)
                bo[i].move();
            for (int i = 0; i < bo.Length; i++)
                for (int j = i + 1; j < bo.Length;j++ )
                    if (collide(bo[i], bo[j]))
                    {
                        bomb t = new bomb();
                        t.mx = bo[i].mx;
                        t.my = bo[i].my;
                        bo[i].mx = bo[j].my;
                        bo[i].my = bo[j].my;
                        bo[j].mx = t.mx;
                        bo[j].my = t.my;
                    }

                    this.Refresh();
        }
        bool collide(bomb b1, bomb b2) //布林值
        {
            if((b1.x - b1.r/2 < b2.x + b2.r/2 &&
                b1.x + b1.r/2 > b2.x - b2.r/2 &&
                b1.y - b1.r/2 < b2.y + b2.r/2 &&
                b1.y + b1.r/2 > b2.y - b2.r/2))
                return true;
            else
                return false;

        }
    }
}