Amaliy mashg’ulot. Windows form application da animatsiya yaratish. Buning uchun formaga pictureBox, button va timer uskunalarini qo’yamiz.
Formaning ko’rinishi quyidagicha:
Dastur kodi quyidagilardan iborat:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication36
{
public partial class Form1 : Form
{
Graphics chizish; //biz obyekt chizadigan grafika ni e'lon qilamiz
Pen qalam; //biz konturni chizishimiz uchun qalam obyektini e'lon qilamiz
SolidBrush fon; //fonni va chizilgan figuraning ichki tomonlari mos ravishda
SolidBrush soha; // to'ldirish uchun obyekt e'lon qilamiz
int rad; // chizilgan doiralar radiusini saqlash uchun o'zgaruvchi
Random rand;
public Form1()
{
InitializeComponent();
}
void DoiraChizish(int x, int y)
{
int xc, yc;
xc = x - rad;
yc = y - rad;
chizish.FillEllipse(soha, xc, yc, rad, rad);
chizish.DrawEllipse(qalam, xc, yc, rad, rad);
}
private void button1_Click(object sender, EventArgs e)
{
chizish = pictureBox1.CreateGraphics();
fon = new SolidBrush(Color.Aqua);
rad = 40;
chizish.FillRectangle(fon, 0, 0, pictureBox1.Width, pictureBox1.Height); // chizilgan maydonimizni
int x, y;
rand = new Random();
for (int i = 0; i < 15; i++)
{
Color rang = Color.FromArgb(rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255));
soha = new SolidBrush(rang);
qalam = new Pen(Color.FromArgb(rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255)), 2);
x = rand.Next(pictureBox1.Width);
y = rand.Next(pictureBox1.Height);
DoiraChizish(x, y);
}
timer1.Enabled = true; //endi, taymerimizni yoqaylik,
// ya'ni endi Tick hodisasi sodir bo'ladi va On_Tick funksiyasi uni boshqaradi (sukut bo'yicha)
}
private void timer1_Tick(object sender, EventArgs e)
{
//avval chizilgan maydonni fon rangi bilan tozalaymiz
chizish.FillRectangle(fon, 0, 0, pictureBox1.Width, pictureBox1.Height);
// keyin yana tasodifiy doiralar markazlarining koordinatalarini tanlaymiz
// va biz tasvirlab bergan funksiya yordamida ularni chizamiz
int x, y;
for (int i = 0; i < 15; i++)
{
Color rang = Color.FromArgb(rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255));
soha = new SolidBrush(rang);
x = rand.Next(pictureBox1.Width);
y = rand.Next(pictureBox1.Height);
DoiraChizish(x, y);
}
}
}
}
Dastur natijasi:
Joriy nazorat3
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication45
{
public partial class Form1 : Form
{
Graphics chizish;
Pen qalam;
SolidBrush fon;
SolidBrush soha;
Random rand;
int rad,x,y;
bool kattaKichik = true;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
void DoiraChizish(int x, int y, int rad)
{
int xc, yc;
xc = x - rad / 2;
yc = y - rad / 2;
chizish.FillEllipse(soha, xc, yc, rad, rad);
chizish.DrawEllipse(qalam, xc, yc, rad, rad);
}
private void button1_Click(object sender, EventArgs e)
{
chizish = pictureBox1.CreateGraphics();
fon = new SolidBrush(Color.Aqua);
chizish.FillRectangle(fon, 0, 0, pictureBox1.Width, pictureBox1.Height);
int x, y;
rand = new Random();
Color rang = Color.FromArgb(rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255));
soha = new SolidBrush(rang);
qalam = new Pen(Color.FromArgb(rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255)), 2);
x = pictureBox1.Width / 2;
y = pictureBox1.Height / 2;
rad = pictureBox1.Height;
DoiraChizish(x, y, rad);
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
chizish.FillRectangle(fon, 0, 0, pictureBox1.Width, pictureBox1.Height);
Color rang = Color.FromArgb(rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255));
soha = new SolidBrush(rang);
DoiraChizish(x, y, rad);
rand = new Random();
if (kattaKichik)
{
rad -= 30;
if (rad <= 0)
{
kattaKichik = false;
x = rand.Next(0, pictureBox1.Width);
y = rand.Next(0, pictureBox1.Height);
}
}
else
{
rad += 30;
if (rad >= pictureBox1.Height)
{
kattaKichik = true;
x = rand.Next(0, pictureBox1.Width);
y = rand.Next(0, pictureBox1.Height);
}
}
timer1.Enabled = true;
}
}
}
Do'stlaringiz bilan baham: |