diff --git a/Lab/CarsGenericCollection.cs b/Lab/CarsGenericCollection.cs new file mode 100644 index 0000000..57a4e5d --- /dev/null +++ b/Lab/CarsGenericCollection.cs @@ -0,0 +1,91 @@ +using Lab.Generics; +using Lab.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Lab.DrawningObjects; + +namespace Lab.Generics +{ + internal class CarsGenericCollection + where T : DrawTanker + where U : IMoveableObject + { + private readonly int _pictureWidth; + private readonly int _pictureHeight; + private readonly int _placeSizeWidth = 110; + private readonly int _placeSizeHeight = 80; + private readonly SetGeneric _collection; + public CarsGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + public static int? operator +(CarsGenericCollection collect, T? + obj) + { + if (obj == null) + { + return -1; + } + return collect?._collection.Insert(obj); + } + public static bool operator -(CarsGenericCollection collect, int + pos) + { + T? obj = collect._collection.Get(pos); + if (obj == null) + { + return false; + } + return collect._collection.Remove(pos); + } + + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + public Bitmap ShowCars() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawObjects(gr); + return bmp; + } + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Black, 3); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + + 1; ++j) + {//линия рамзетки места + g.DrawLine(pen, i * _placeSizeWidth, j * + _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * + _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * + _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + private void DrawObjects(Graphics g) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + for (int i = 0; i < _collection.Count; i++) + { + DrawTanker? tank = _collection.Get(i); + if (tank == null) + continue; + tank.SetPosition(i % height * _placeSizeWidth, (height - i / height - 1) * _placeSizeHeight); + tank.DrawTransport(g); + + } + } + }} diff --git a/Lab/CollectionsFrame.Designer.cs b/Lab/CollectionsFrame.Designer.cs new file mode 100644 index 0000000..0ac9f15 --- /dev/null +++ b/Lab/CollectionsFrame.Designer.cs @@ -0,0 +1,135 @@ +namespace Lab +{ + partial class CollectionsFrame + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + panel1 = new Panel(); + UpdateButton = new Button(); + DeleteButton = new Button(); + AddButton = new Button(); + InputNum = new TextBox(); + label1 = new Label(); + DrawTank = new PictureBox(); + panel1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)DrawTank).BeginInit(); + SuspendLayout(); + // + // panel1 + // + panel1.Controls.Add(UpdateButton); + panel1.Controls.Add(DeleteButton); + panel1.Controls.Add(AddButton); + panel1.Controls.Add(InputNum); + panel1.Controls.Add(label1); + panel1.Dock = DockStyle.Right; + panel1.Location = new Point(550, 0); + panel1.Name = "panel1"; + panel1.Size = new Size(250, 450); + panel1.TabIndex = 0; + // + // UpdateButton + // + UpdateButton.Location = new Point(10, 241); + UpdateButton.Name = "UpdateButton"; + UpdateButton.Size = new Size(228, 37); + UpdateButton.TabIndex = 4; + UpdateButton.Text = "Обновить коллекцию"; + UpdateButton.UseVisualStyleBackColor = true; + UpdateButton.Click += ButtonRefreshCollection_Click; + // + // DeleteButton + // + DeleteButton.Location = new Point(10, 176); + DeleteButton.Name = "DeleteButton"; + DeleteButton.Size = new Size(228, 37); + DeleteButton.TabIndex = 3; + DeleteButton.Text = "Удалить автомобиль"; + DeleteButton.UseVisualStyleBackColor = true; + DeleteButton.Click += ButtonRemoveCar_Click; + // + // AddButton + // + AddButton.Location = new Point(10, 53); + AddButton.Name = "AddButton"; + AddButton.Size = new Size(228, 37); + AddButton.TabIndex = 2; + AddButton.Text = "Добавить автомобиль"; + AddButton.UseVisualStyleBackColor = true; + AddButton.Click += ButtonAddTank_Click; + // + // InputNum + // + InputNum.Location = new Point(10, 133); + InputNum.Name = "InputNum"; + InputNum.Size = new Size(228, 27); + InputNum.TabIndex = 1; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(2, 2); + label1.Name = "label1"; + label1.Size = new Size(103, 20); + label1.TabIndex = 0; + label1.Text = "Инструменты"; + // + // DrawTank + // + DrawTank.Dock = DockStyle.Fill; + DrawTank.Location = new Point(0, 0); + DrawTank.Name = "DrawTank"; + DrawTank.Size = new Size(550, 450); + DrawTank.TabIndex = 1; + DrawTank.TabStop = false; + // + // CollectionsFrame + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(DrawTank); + Controls.Add(panel1); + Name = "CollectionsFrame"; + Text = "Form1"; + panel1.ResumeLayout(false); + panel1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)DrawTank).EndInit(); + ResumeLayout(false); + } + + #endregion + + private Panel panel1; + private Button UpdateButton; + private Button DeleteButton; + private Button AddButton; + private TextBox InputNum; + private Label label1; + private PictureBox DrawTank; + } +} \ No newline at end of file diff --git a/Lab/CollectionsFrame.cs b/Lab/CollectionsFrame.cs new file mode 100644 index 0000000..5d9e7fd --- /dev/null +++ b/Lab/CollectionsFrame.cs @@ -0,0 +1,71 @@ +using Lab.DrawningObjects; +using Lab.Generics; +using Lab.MovementStrategy; +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 Lab +{ + public partial class CollectionsFrame : Form + { + private readonly CarsGenericCollection _cars; + + public CollectionsFrame() + { + InitializeComponent(); + _cars = new CarsGenericCollection(DrawTank.Width, DrawTank.Height); + } + + private void ButtonAddTank_Click(object sender, EventArgs e) + { + Frame form = new Frame(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_cars + form.SelectedCar != -1) + { + MessageBox.Show("Объект добавлен"); + DrawTank.Image = _cars.ShowCars(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + private void ButtonRemoveCar_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = -1; + try + { + pos = Convert.ToInt32(InputNum.Text); + } + catch (Exception ex) { } + if (_cars - pos) + { + MessageBox.Show("Объект удален"); + DrawTank.Image = _cars.ShowCars(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + DrawTank.Image = _cars.ShowCars(); + } + } +} diff --git a/Lab/CollectionsFrame.resx b/Lab/CollectionsFrame.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/Lab/CollectionsFrame.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Lab/DrawTanker.cs b/Lab/DrawTanker.cs index b39f1e4..14a2981 100644 --- a/Lab/DrawTanker.cs +++ b/Lab/DrawTanker.cs @@ -5,6 +5,7 @@ using System.Security.Cryptography.Pkcs; using System.Text; using System.Threading.Tasks; using Lab.Entities; +using Lab.MovementStrategy; namespace Lab.DrawningObjects { @@ -119,5 +120,7 @@ namespace Lab.DrawningObjects } + + public IMoveableObject GetMoveableObject => new DrawingObjectTanker(this); } } diff --git a/Lab/Frame.Designer.cs b/Lab/Frame.Designer.cs index df08d3e..970fe1e 100644 --- a/Lab/Frame.Designer.cs +++ b/Lab/Frame.Designer.cs @@ -29,14 +29,15 @@ private void InitializeComponent() { DrawCar = new PictureBox(); - CreateBaseCarButton = new Button(); - Up = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStep = new Button(); + Right = new Button(); Left = new Button(); Down = new Button(); - Right = new Button(); + Up = new Button(); buttonCreateGasolineTanker = new Button(); - buttonStep = new Button(); - comboBoxStrategy = new ComboBox(); + CreateBaseCarButton = new Button(); + ChooseCar = new Button(); ((System.ComponentModel.ISupportInitialize)DrawCar).BeginInit(); SuspendLayout(); // @@ -49,28 +50,37 @@ DrawCar.TabIndex = 1; DrawCar.TabStop = false; // - // CreateBaseCarButton + // comboBoxStrategy // - CreateBaseCarButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - CreateBaseCarButton.Location = new Point(12, 441); - CreateBaseCarButton.Name = "CreateBaseCarButton"; - CreateBaseCarButton.Size = new Size(200, 100); - CreateBaseCarButton.TabIndex = 2; - CreateBaseCarButton.Text = "Создать простой бензовоз"; - CreateBaseCarButton.UseVisualStyleBackColor = true; - CreateBaseCarButton.Click += CreateCarButton_Click; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "0", "1" }); + comboBoxStrategy.Location = new Point(719, 27); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(151, 28); + comboBoxStrategy.TabIndex = 9; // - // Up + // buttonStep // - Up.BackgroundImage = Properties.Resources.Up; - Up.BackgroundImageLayout = ImageLayout.Zoom; - Up.Location = new Point(804, 476); - Up.Name = "Up"; - Up.Size = new Size(30, 30); - Up.TabIndex = 3; - Up.Text = "↑"; - Up.UseVisualStyleBackColor = true; - Up.Click += ButtonMove_Click; + buttonStep.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonStep.Location = new Point(776, 72); + buttonStep.Name = "buttonStep"; + buttonStep.Size = new Size(94, 29); + buttonStep.TabIndex = 8; + buttonStep.Text = "Шаг"; + buttonStep.UseVisualStyleBackColor = true; + buttonStep.Click += ButtonStep_Click; + // + // Right + // + Right.BackgroundImage = Properties.Resources.Right; + Right.BackgroundImageLayout = ImageLayout.Zoom; + Right.Location = new Point(840, 512); + Right.Name = "Right"; + Right.Size = new Size(30, 30); + Right.TabIndex = 6; + Right.Text = "→"; + Right.UseVisualStyleBackColor = true; + Right.Click += ButtonMove_Click; // // Left // @@ -96,17 +106,17 @@ Down.UseVisualStyleBackColor = true; Down.Click += ButtonMove_Click; // - // Right + // Up // - Right.BackgroundImage = Properties.Resources.Right; - Right.BackgroundImageLayout = ImageLayout.Zoom; - Right.Location = new Point(840, 512); - Right.Name = "Right"; - Right.Size = new Size(30, 30); - Right.TabIndex = 6; - Right.Text = "→"; - Right.UseVisualStyleBackColor = true; - Right.Click += ButtonMove_Click; + Up.BackgroundImage = Properties.Resources.Up; + Up.BackgroundImageLayout = ImageLayout.Zoom; + Up.Location = new Point(804, 476); + Up.Name = "Up"; + Up.Size = new Size(30, 30); + Up.TabIndex = 3; + Up.Text = "↑"; + Up.UseVisualStyleBackColor = true; + Up.Click += ButtonMove_Click; // // buttonCreateGasolineTanker // @@ -119,31 +129,33 @@ buttonCreateGasolineTanker.UseVisualStyleBackColor = true; buttonCreateGasolineTanker.Click += CreateGasolineTankerButton_Click; // - // buttonStep + // CreateBaseCarButton // - buttonStep.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonStep.Location = new Point(776, 72); - buttonStep.Name = "buttonStep"; - buttonStep.Size = new Size(94, 29); - buttonStep.TabIndex = 8; - buttonStep.Text = "Шаг"; - buttonStep.UseVisualStyleBackColor = true; - buttonStep.Click += ButtonStep_Click; + CreateBaseCarButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + CreateBaseCarButton.Location = new Point(12, 441); + CreateBaseCarButton.Name = "CreateBaseCarButton"; + CreateBaseCarButton.Size = new Size(200, 100); + CreateBaseCarButton.TabIndex = 2; + CreateBaseCarButton.Text = "Создать простой бензовоз"; + CreateBaseCarButton.UseVisualStyleBackColor = true; + CreateBaseCarButton.Click += CreateCarButton_Click; // - // comboBoxStrategy + // ChooseCar // - comboBoxStrategy.FormattingEnabled = true; - comboBoxStrategy.Items.AddRange(new object[] { "0", "1" }); - comboBoxStrategy.Location = new Point(719, 27); - comboBoxStrategy.Name = "comboBoxStrategy"; - comboBoxStrategy.Size = new Size(151, 28); - comboBoxStrategy.TabIndex = 9; + ChooseCar.Location = new Point(776, 149); + ChooseCar.Name = "ChooseCar"; + ChooseCar.Size = new Size(94, 52); + ChooseCar.TabIndex = 10; + ChooseCar.Text = "Выбрать машину"; + ChooseCar.UseVisualStyleBackColor = true; + ChooseCar.Click += ButtonSelectTank_Click; // - // GasolineTanker + // Frame // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(882, 553); + Controls.Add(ChooseCar); Controls.Add(comboBoxStrategy); Controls.Add(buttonStep); Controls.Add(buttonCreateGasolineTanker); @@ -153,7 +165,7 @@ Controls.Add(Up); Controls.Add(CreateBaseCarButton); Controls.Add(DrawCar); - Name = "GasolineTanker"; + Name = "Frame"; StartPosition = FormStartPosition.CenterScreen; Text = "GasolineTanker"; ((System.ComponentModel.ISupportInitialize)DrawCar).EndInit(); @@ -161,14 +173,18 @@ } #endregion + private PictureBox DrawCar; - private Button CreateBaseCarButton; - private Button Up; + private ComboBox comboBoxStrategy; + private Button buttonStep; + private Button Right; private Button Left; private Button Down; - private Button Right; + private Button Up; private Button buttonCreateGasolineTanker; - private Button buttonStep; - private ComboBox comboBoxStrategy; + private Button CreateBaseCarButton; + private Button button2; + private Button AddTanker; + private Button ChooseCar; } } \ No newline at end of file diff --git a/Lab/Frame.cs b/Lab/Frame.cs index 5ab168b..5cdf78b 100644 --- a/Lab/Frame.cs +++ b/Lab/Frame.cs @@ -1,6 +1,7 @@ using Lab.DrawningObjects; using Lab.MovementStrategy; using Lab; +using System.Drawing; namespace Lab { @@ -8,11 +9,16 @@ namespace Lab { private DrawTanker? _drawingTanker; private AbstractStrategy? _abstractStrategy; + public DrawTanker? SelectedCar { get; private set; } + public Frame() { InitializeComponent(); + _abstractStrategy = null; + SelectedCar = null; } + private void Draw() { if (_drawingTanker == null) @@ -25,9 +31,19 @@ namespace Lab private void CreateGasolineTankerButton_Click(object sender, EventArgs e) { Random rnd = new(); + Color mainColor = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + mainColor = dialog.Color; + } + Color addColor = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)); + if (dialog.ShowDialog() == DialogResult.OK) + { + addColor = dialog.Color; + } _drawingTanker = new DrawGasolineTanker(rnd.Next(100, 200), rnd.Next(2000, 4000), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), + mainColor, addColor, Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), DrawCar.Width, DrawCar.Height); _drawingTanker.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100)); @@ -37,8 +53,13 @@ namespace Lab private void CreateCarButton_Click(object sender, EventArgs e) { Random rnd = new(); - _drawingTanker = new DrawTanker(rnd.Next(100, 200), rnd.Next(2000, 4000), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), + Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + _drawingTanker = new DrawTanker(rnd.Next(100, 200), rnd.Next(2000, 4000), color, DrawCar.Width, DrawCar.Height); _drawingTanker.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100)); Draw(); @@ -77,7 +98,7 @@ namespace Lab }; if (_abstractStrategy == null) return; - _abstractStrategy.SetData(new DrawingObjectTanker(_drawingTanker), DrawCar.Width, DrawCar.Height); + _abstractStrategy.SetData(_drawingTanker.GetMoveableObject, DrawCar.Width, DrawCar.Height); comboBoxStrategy.Enabled = false; } if (_abstractStrategy == null) @@ -90,5 +111,11 @@ namespace Lab _abstractStrategy = null; } } + + private void ButtonSelectTank_Click(object sender, EventArgs e) + { + SelectedCar = _drawingTanker; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/Lab/Program.cs b/Lab/Program.cs index dfdc54f..5ea6184 100644 --- a/Lab/Program.cs +++ b/Lab/Program.cs @@ -11,7 +11,7 @@ namespace Lab // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new Frame()); + Application.Run(new CollectionsFrame()); } } } \ No newline at end of file diff --git a/Lab/SetGeneric.cs b/Lab/SetGeneric.cs new file mode 100644 index 0000000..35d6d69 --- /dev/null +++ b/Lab/SetGeneric.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Lab.Generics +{ + internal class SetGeneric where T : class + { + private readonly T?[] _places; + + public int Count => _places.Length; + + public SetGeneric(int count) + { + _places = new T?[count]; + } + + public int Insert(T car) + { + int index = -1; + for (int i = 0; i < _places.Length; i++ ) + { + if (_places[i] == null) + { + index = i; break; + } + } + if ( index < 0 ) + { + return -1; + } + for (int i = index; i > 0; i-- ) + { + _places[i] = _places[i - 1]; + } + _places[0] = car; + return 0; + } + + public int Insert(T car, int position) + { + if (position < 0 || position >= Count) + return -1; + if (_places[position] == null) + { + _places[position] = car; + return position; + } + int index = -1; + for (int i = position; i < Count; i++) + { + if (_places[i] == null) + { + index = i; break; + } + } + if (index < 0) + return -1; + for (int i = index; index > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = car; + return position; + } + + public bool Remove(int position) + { + if (position < 0 || position >= Count) + return false; + _places[position] = null; + return true; + } + + public T? Get(int position) + { + if (position < 0 || position >= Count) + return null; + return _places[position]; + } + } +}