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



沒有留言:

張貼留言