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