Создать класс очередь. Графическая реализация очереди на основе класса - C#
Формулировка задачи:
Помогите пожалуйста, полнейша безысходность в простых прораммах работа с очередью понятно, но с графической реализацией...
Решение задачи: «Создать класс очередь. Графическая реализация очереди на основе класса»
textual
Листинг программы
- public partial class Form1 : Form
- {
- public class MyQueue
- {
- List<MyRect> list;
- public MyQueue()
- {
- list = new List<MyRect>();
- }
- public void Enqueue(MyRect rect)
- {
- rect.Number = list.Count;
- list.Add(rect);
- }
- public MyRect Get()
- {
- MyRect tmp = list[0];
- list.RemoveAt(0);
- return tmp;
- }
- public void DrawQueue(Graphics g)
- {
- foreach (MyRect r in list)
- {
- r.Draw(g);
- }
- }
- }
- public class MyRect
- {
- Rectangle rect;
- int number;
- public MyRect(int x, int y, int width, int height)
- {
- rect.X = x;
- rect.Y = y;
- rect.Width = width;
- rect.Height = height;
- }
- public int Number
- {
- get { return number; }
- set { this.number = value; }
- }
- public void Draw(Graphics g)
- {
- g.DrawRectangle(new Pen(new SolidBrush(Color.Black)), rect);
- g.DrawString(number.ToString(), new Font("Arial", 10), new SolidBrush(Color.Black), rect.X + 5, rect.Y + 5);
- }
- }
- MyQueue queue;
- Random rand;
- Panel panel1, panel2;
- List<MyRect> l; //список для выборки из очереди
- public Form1()
- {
- queue = new MyQueue();
- rand = new Random();
- l = new List<MyRect>();
- Button add, del;
- add = new Button();
- del = new Button();
- add.SetBounds(10, 10, 70, 30);
- del.SetBounds(100, 10, 70, 30);
- add.Text = "Добавить";
- del.Text = "Вытащить";
- this.Controls.Add(add);
- this.Controls.Add(del);
- add.Click += add_Click;
- del.Click += del_Click;
- panel1 = new Panel();
- panel2 = new Panel();
- panel1.SetBounds(10, 50, 200, 200);
- panel2.SetBounds(220, 50, 200, 200);
- panel1.BorderStyle = BorderStyle.FixedSingle;
- panel2.BorderStyle = BorderStyle.FixedSingle;
- this.Controls.Add(panel1);
- this.Controls.Add(panel2);
- panel1.Paint += panel1_Paint;
- panel2.Paint += panel2_Paint;
- InitializeComponent();
- this.Width = 440;
- this.Height = 300;
- }
- void panel2_Paint(object sender, PaintEventArgs e)
- {
- Graphics g = Graphics.FromHwnd(panel2.Handle);
- foreach (MyRect r in l)
- {
- r.Draw(g);
- }
- }
- void panel1_Paint(object sender, PaintEventArgs e)
- {
- queue.DrawQueue(Graphics.FromHwnd(panel1.Handle));
- }
- void del_Click(object sender, EventArgs e)
- {
- try
- {
- l.Add(queue.Get());
- }
- catch (ArgumentOutOfRangeException)
- { MessageBox.Show("Очередь пуста"); return; }
- panel1.Refresh();
- panel2.Refresh();
- }
- void add_Click(object sender, EventArgs e)
- {
- queue.Enqueue(new MyRect(rand.Next(0, panel1.Width-30), rand.Next(panel2.Height-30), 20, 20));
- panel1.Refresh();
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д