using CourseWork_EredavkinRA; using System; using System.Collections.Generic; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using static System.Windows.Forms.AxHost; namespace CourseWorkEreda; public class Visualizator { private bool addRectangleUp = false; public void Visualize(ArrayDequeState state, FormMain form) { Pen penArrow = new(Color.Black, 2f); PictureBox? pictureBox = form.Controls["pictureBox1"] as PictureBox; if (pictureBox == null) return; Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height); using (Graphics graphics = Graphics.FromImage(bitmap)) { graphics.Clear(Color.White); int[] stackArray = state.Array; int stackSize = state.Top + 1; int rectangleWidth = 100; int rectangleHeight = 50; const int distanceBetweenElements = 10; int x = 300; int startY = 100; if (state.operation != Operation.None) { graphics.FillRectangle(Brushes.Turquoise, 50, 50, rectangleWidth, rectangleHeight); graphics.DrawRectangle(Pens.Black, 50, 50, rectangleWidth, rectangleHeight); graphics.FillRectangle(Brushes.Turquoise, pictureBox.Width - 200, 50, rectangleWidth, rectangleHeight); graphics.DrawRectangle(Pens.Black, pictureBox.Width - 200, 50, rectangleWidth, rectangleHeight); int lastRectangleY = startY + (stackSize - 1) * (rectangleHeight + distanceBetweenElements); graphics.FillRectangle(Brushes.Turquoise, 50, lastRectangleY + 100, rectangleWidth, rectangleHeight); graphics.DrawRectangle(Pens.Black, 50, lastRectangleY + 100, rectangleWidth, rectangleHeight); graphics.FillRectangle(Brushes.Turquoise, pictureBox.Width - 200, lastRectangleY + 100, rectangleWidth, rectangleHeight); graphics.DrawRectangle(Pens.Black, pictureBox.Width - 200, lastRectangleY + 100, rectangleWidth, rectangleHeight); for (int i = 0; i < stackSize; i++) { int y = startY + i * (rectangleHeight + distanceBetweenElements); graphics.FillRectangle(Brushes.Turquoise, x, y, rectangleWidth, rectangleHeight); graphics.DrawRectangle(Pens.Black, x, y, rectangleWidth, rectangleHeight); string text = stackArray[i].ToString(); using (Font font = new Font("Arial", 10)) { SizeF textSize = graphics.MeasureString(text, font); float textX = x + (rectangleWidth - textSize.Width) / 2; float textY = y + (rectangleHeight - textSize.Height) / 2; graphics.DrawString(text, font, Brushes.Black, textX, textY); } } } else { graphics.FillRectangle(Brushes.Turquoise, 50, 50, rectangleWidth, rectangleHeight); graphics.DrawRectangle(Pens.Black, 50, 50, rectangleWidth, rectangleHeight); graphics.FillRectangle(Brushes.Turquoise, pictureBox.Width - 200, 50, rectangleWidth, rectangleHeight); graphics.DrawRectangle(Pens.Black, pictureBox.Width - 200, 50, rectangleWidth, rectangleHeight); int lastRectangleY = startY + (stackSize - 1) * (rectangleHeight + distanceBetweenElements); graphics.FillRectangle(Brushes.Turquoise, 50, lastRectangleY + 100, rectangleWidth, rectangleHeight); graphics.DrawRectangle(Pens.Black, 50, lastRectangleY + 100, rectangleWidth, rectangleHeight); graphics.FillRectangle(Brushes.Turquoise, pictureBox.Width - 200, lastRectangleY + 100, rectangleWidth, rectangleHeight); graphics.DrawRectangle(Pens.Black, pictureBox.Width - 200, lastRectangleY + 100, rectangleWidth, rectangleHeight); for (int i = 0; i < stackSize; i++) { int y = startY + i * (rectangleHeight + distanceBetweenElements); graphics.FillRectangle(Brushes.Turquoise, x, y, rectangleWidth, rectangleHeight); graphics.DrawRectangle(Pens.Black, x, y, rectangleWidth, rectangleHeight); string text = stackArray[i].ToString(); using (Font font = new Font("Arial", 10)) { SizeF textSize = graphics.MeasureString(text, font); float textX = x + (rectangleWidth - textSize.Width) / 2; float textY = y + (rectangleHeight - textSize.Height) / 2; graphics.DrawString(text, font, Brushes.Black, textX, textY); } } } } pictureBox.Image = bitmap; } public void SetAddRectangleUp(bool value) { addRectangleUp = value; } }