您的位置:首页 > 编程语言 > C#

____88__Bitmap_Graphics_GDI绘制动态的圆

2016-02-04 15:41 591 查看
Bitmap亦称为点阵图像或绘制图像,是由称作像素(图片元素)的单个点组成的

Graphics 类是所有图形上下文的抽象基类,允许应用程序可以在组件(已经在各种设备上实现),以及闭屏图像上,进行绘制

定义变量

public int[,] ballarray = new int[20,20];
public string[] colours = new string[16];
public int g1=1;
public int g2=200;
public Bitmap bmp;

private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem AnimateStart;
private System.Windows.Forms.MenuItem AnimateStop;
private System.ComponentModel.IContainer components;

定义颜色

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//colours[0]="Red";
colours[1]="Red";
colours[2]="Blue";
colours[3]="Black";
colours[4]="Yellow";
colours[5]="Crimson";
colours[6]="Gold";
colours[7]="Green";
colours[8]="Magenta";
colours[9]="Aquamarine";
colours[10]="Brown";
colours[11]="Red";
colours[12]="DarkBlue";
colours[13]="Brown";
colours[14]="Red";
colours[15]="DarkBlue";

//
// TODO: Add any constructor code after InitializeComponent call
//
}

timer1事件初始化

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.AnimateStart = new System.Windows.Forms.MenuItem();
this.AnimateStop = new System.Windows.Forms.MenuItem();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.timer1 = new System.Windows.Forms.Timer(this.components);
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.AnimateStart,
this.AnimateStop});
this.menuItem1.Text = "运动的球";
//
// AnimateStart
//
this.AnimateStart.Index = 0;
this.AnimateStart.Text = "开始";
this.AnimateStart.Click += new System.EventHandler(this.menuItem2_Click);
//
// AnimateStop
//
this.AnimateStop.Index = 1;
this.AnimateStop.Text = "停止";
this.AnimateStop.Click += new System.EventHandler(this.menuItem3_Click);
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// timer1 绑定点击事件
//
this.timer1.Interval = 25;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(373, 315);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "运动的球";

}
#endregion

timer1绑定的触发方法

private void timer1_Tick(object sender, System.EventArgs e)
{
for (int i=1;i<=13;i++)
{
//add direction vectors to coordinates
ballarray[i,1] = ballarray[i,1] + ballarray[i,3];
ballarray[i,2] = ballarray[i,2] + ballarray[i,4];
//if ball goes of to right
if ((ballarray[i,1]+50)>=ClientSize.Width)
{
ballarray[i,1]=ballarray[i,1]-ballarray[i,3];
ballarray[i,3]=-ballarray[i,3];
}
//if ball goes off bottom
else if ((ballarray[i,2]+50)>=ClientSize.Height)
{
ballarray[i,2]=ballarray[i,2]-ballarray[i,4];
ballarray[i,4]=-ballarray[i,4];
}
//if ball goes off to left
else if (ballarray[i,1]<=1)
{
ballarray[i,1]=ballarray[i,1]-ballarray[i,3];
ballarray[i,3]=-ballarray[i,3];
}
//if ball goes over top
else if (ballarray[i,2]<=1)
{
ballarray[i,2]=ballarray[i,2]-ballarray[i,4];
ballarray[i,4]=-ballarray[i,4];
}
}
this.Refresh(); //force repaint of window
}


绘制圆的主方法,两个按钮,开始和停止

//Called from timer event when window needs redrawing
protected override void OnPaint(PaintEventArgs e)
{
bmp = new Bitmap(ClientSize.Width,ClientSize.Height, e.Graphics);
Graphics bmpGraphics = Graphics.FromImage(bmp);
// draw here
for (int i=1;i<=13;i++)
{
bmpGraphics.DrawEllipse(new Pen(Color.FromName(colours[i]),2),
ballarray[i,1],ballarray[i,2],50,50);
}
e.Graphics.DrawImageUnscaled(bmp, 0, 0);
//Draw ellipse acording to mouse coords.
e.Graphics.DrawEllipse(new Pen(Color.Red), g1,g2,50,50);
bmpGraphics.Dispose();
bmp.Dispose();
}
private void menuItem2_Click(object sender, System.EventArgs e)
{ //开始按钮
Random r = new Random();
//set ball coords and vectors x,y,xv,yv
for (int i=1;i<=13;i++)
{
ballarray[i,1]=+r.Next(10)+1; //+1 means i lose zero values
ballarray[i,2]=+r.Next(10)+1;

ballarray[i,3]=+r.Next(10)+1;
ballarray[i,4]=+r.Next(10)+1;
}
timer1.Start();
}
//Called when user moves the mouse
protected override void OnMouseMove(MouseEventArgs m)
{
//Read the mouse x and y coords and use them to draw a circle in OnPaint method
g1=m.X;
g2=m.Y;
}
private void menuItem3_Click(object sender, System.EventArgs e)
{  //停止按钮
timer1.Stop();
}


运行效果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C#基础 gdi 图形