From e573f439b5415afaa2e51a6eaf3260e2616379f1 Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 13 Dec 2023 23:49:20 +0400 Subject: [PATCH 1/3] Laba3-90% --- DumpTruck/DumpTruck/DrawningTruck.cs | 8 +- DumpTruck/DumpTruck/DumpTruck.csproj | 4 +- DumpTruck/DumpTruck/FormDumpTruck.Designer.cs | 215 ++++++++++-------- DumpTruck/DumpTruck/FormDumpTruck.cs | 40 +++- DumpTruck/DumpTruck/FormDumpTruck.resx | 62 ++++- .../DumpTruck/FormTruckCollection.Designer.cs | 123 ++++++++++ DumpTruck/DumpTruck/FormTruckCollection.cs | 72 ++++++ DumpTruck/DumpTruck/FormTruckCollection.resx | 120 ++++++++++ DumpTruck/DumpTruck/Program.cs | 4 +- DumpTruck/DumpTruck/SetGeneric.cs | 110 +++++++++ .../DumpTruck/TrucksGenericCollection.cs | 140 ++++++++++++ 11 files changed, 789 insertions(+), 109 deletions(-) create mode 100644 DumpTruck/DumpTruck/FormTruckCollection.Designer.cs create mode 100644 DumpTruck/DumpTruck/FormTruckCollection.cs create mode 100644 DumpTruck/DumpTruck/FormTruckCollection.resx create mode 100644 DumpTruck/DumpTruck/SetGeneric.cs create mode 100644 DumpTruck/DumpTruck/TrucksGenericCollection.cs diff --git a/DumpTruck/DumpTruck/DrawningTruck.cs b/DumpTruck/DumpTruck/DrawningTruck.cs index df0a319..7033f26 100644 --- a/DumpTruck/DumpTruck/DrawningTruck.cs +++ b/DumpTruck/DumpTruck/DrawningTruck.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using DumpTruck.Entities; +using DumpTruck.MovementStrategy; namespace DumpTruck.DrawningObjects { @@ -19,6 +20,11 @@ namespace DumpTruck.DrawningObjects /// /// Ширина окна /// + /// + /// Получение объекта IMoveableObject из объекта DrawningCar + /// + public IMoveableObject GetMoveableObject => new DrawningObjectTruck(this); + /// public int _pictureWidth; /// /// Высота окна @@ -64,7 +70,7 @@ namespace DumpTruck.DrawningObjects /// Вес /// Основной цвет /// Ширина картинки - /// Высота картинки + /// Высота картинки public DrawningTruck(int speed, double weight, Color bodyColor,Color additionalColor, int width, int height) { // TODO: Продумать проверки diff --git a/DumpTruck/DumpTruck/DumpTruck.csproj b/DumpTruck/DumpTruck/DumpTruck.csproj index b57c89e..616bcc0 100644 --- a/DumpTruck/DumpTruck/DumpTruck.csproj +++ b/DumpTruck/DumpTruck/DumpTruck.csproj @@ -2,10 +2,12 @@ WinExe - net6.0-windows + net6.0-windows10.0.22621.0 enable true enable + 10.0.22621.0 + True \ No newline at end of file diff --git a/DumpTruck/DumpTruck/FormDumpTruck.Designer.cs b/DumpTruck/DumpTruck/FormDumpTruck.Designer.cs index 108c2ad..8effbed 100644 --- a/DumpTruck/DumpTruck/FormDumpTruck.Designer.cs +++ b/DumpTruck/DumpTruck/FormDumpTruck.Designer.cs @@ -27,134 +27,152 @@ /// private void InitializeComponent() { - this.pictureBoxDumpTruck = new System.Windows.Forms.PictureBox(); - this.buttonUp = new System.Windows.Forms.Button(); - this.buttonLeft = new System.Windows.Forms.Button(); - this.buttonRight = new System.Windows.Forms.Button(); - this.buttonDown = new System.Windows.Forms.Button(); - this.Create_Truck_Button = new System.Windows.Forms.Button(); - this.CreateDumpTruckButton = new System.Windows.Forms.Button(); - this.comboBoxStrategy = new System.Windows.Forms.ComboBox(); - this.Step_Button = new System.Windows.Forms.Button(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxDumpTruck)).BeginInit(); - this.SuspendLayout(); + pictureBoxDumpTruck = new PictureBox(); + buttonUp = new Button(); + buttonLeft = new Button(); + buttonRight = new Button(); + buttonDown = new Button(); + Create_Truck_Button = new Button(); + CreateDumpTruckButton = new Button(); + comboBoxStrategy = new ComboBox(); + Step_Button = new Button(); + buttonSelectTruck = new Button(); + ((System.ComponentModel.ISupportInitialize)pictureBoxDumpTruck).BeginInit(); + SuspendLayout(); // // pictureBoxDumpTruck // - this.pictureBoxDumpTruck.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBoxDumpTruck.Location = new System.Drawing.Point(0, 0); - this.pictureBoxDumpTruck.Name = "pictureBoxDumpTruck"; - this.pictureBoxDumpTruck.Size = new System.Drawing.Size(884, 461); - this.pictureBoxDumpTruck.TabIndex = 0; - this.pictureBoxDumpTruck.TabStop = false; + pictureBoxDumpTruck.Dock = DockStyle.Fill; + pictureBoxDumpTruck.Location = new Point(0, 0); + pictureBoxDumpTruck.Margin = new Padding(3, 4, 3, 4); + pictureBoxDumpTruck.Name = "pictureBoxDumpTruck"; + pictureBoxDumpTruck.Size = new Size(1010, 615); + pictureBoxDumpTruck.TabIndex = 0; + pictureBoxDumpTruck.TabStop = false; // // buttonUp // - this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonUp.Location = new System.Drawing.Point(795, 374); - this.buttonUp.Name = "buttonUp"; - this.buttonUp.Size = new System.Drawing.Size(24, 26); - this.buttonUp.TabIndex = 2; - this.buttonUp.Text = "1"; - this.buttonUp.UseVisualStyleBackColor = true; - this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); + buttonUp.BackgroundImageLayout = ImageLayout.Zoom; + buttonUp.Location = new Point(909, 499); + buttonUp.Margin = new Padding(3, 4, 3, 4); + buttonUp.Name = "buttonUp"; + buttonUp.Size = new Size(27, 35); + buttonUp.TabIndex = 2; + buttonUp.Text = "1"; + buttonUp.UseVisualStyleBackColor = true; + buttonUp.Click += ButtonMove_Click; // // buttonLeft // - this.buttonLeft.BackColor = System.Drawing.SystemColors.ControlDark; - this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonLeft.Location = new System.Drawing.Point(765, 406); - this.buttonLeft.Name = "buttonLeft"; - this.buttonLeft.Size = new System.Drawing.Size(24, 26); - this.buttonLeft.TabIndex = 3; - this.buttonLeft.Text = "3"; - this.buttonLeft.UseVisualStyleBackColor = false; - this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); + buttonLeft.BackColor = SystemColors.ControlDark; + buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; + buttonLeft.Location = new Point(874, 541); + buttonLeft.Margin = new Padding(3, 4, 3, 4); + buttonLeft.Name = "buttonLeft"; + buttonLeft.Size = new Size(27, 35); + buttonLeft.TabIndex = 3; + buttonLeft.Text = "3"; + buttonLeft.UseVisualStyleBackColor = false; + buttonLeft.Click += ButtonMove_Click; // // buttonRight // - this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonRight.Location = new System.Drawing.Point(825, 407); - this.buttonRight.Name = "buttonRight"; - this.buttonRight.Size = new System.Drawing.Size(24, 26); - this.buttonRight.TabIndex = 4; - this.buttonRight.Text = "4"; - this.buttonRight.UseVisualStyleBackColor = true; - this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); + buttonRight.BackgroundImageLayout = ImageLayout.Zoom; + buttonRight.Location = new Point(943, 543); + buttonRight.Margin = new Padding(3, 4, 3, 4); + buttonRight.Name = "buttonRight"; + buttonRight.Size = new Size(27, 35); + buttonRight.TabIndex = 4; + buttonRight.Text = "4"; + buttonRight.UseVisualStyleBackColor = true; + buttonRight.Click += ButtonMove_Click; // // buttonDown // - this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonDown.Location = new System.Drawing.Point(795, 406); - this.buttonDown.Name = "buttonDown"; - this.buttonDown.Size = new System.Drawing.Size(24, 26); - this.buttonDown.TabIndex = 5; - this.buttonDown.Text = "2"; - this.buttonDown.UseVisualStyleBackColor = true; - this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); + buttonDown.BackgroundImageLayout = ImageLayout.Zoom; + buttonDown.Location = new Point(909, 541); + buttonDown.Margin = new Padding(3, 4, 3, 4); + buttonDown.Name = "buttonDown"; + buttonDown.Size = new Size(27, 35); + buttonDown.TabIndex = 5; + buttonDown.Text = "2"; + buttonDown.UseVisualStyleBackColor = true; + buttonDown.Click += ButtonMove_Click; // // Create_Truck_Button // - this.Create_Truck_Button.Location = new System.Drawing.Point(12, 392); - this.Create_Truck_Button.Name = "Create_Truck_Button"; - this.Create_Truck_Button.Size = new System.Drawing.Size(123, 23); - this.Create_Truck_Button.TabIndex = 6; - this.Create_Truck_Button.Text = "Create Truck"; - this.Create_Truck_Button.UseVisualStyleBackColor = true; - this.Create_Truck_Button.Click += new System.EventHandler(this.Create_Truck_Button_Click); + Create_Truck_Button.Location = new Point(14, 523); + Create_Truck_Button.Margin = new Padding(3, 4, 3, 4); + Create_Truck_Button.Name = "Create_Truck_Button"; + Create_Truck_Button.Size = new Size(141, 31); + Create_Truck_Button.TabIndex = 6; + Create_Truck_Button.Text = "Create Truck"; + Create_Truck_Button.UseVisualStyleBackColor = true; + Create_Truck_Button.Click += Create_Truck_Button_Click; // // CreateDumpTruckButton // - this.CreateDumpTruckButton.Location = new System.Drawing.Point(166, 392); - this.CreateDumpTruckButton.Name = "CreateDumpTruckButton"; - this.CreateDumpTruckButton.Size = new System.Drawing.Size(168, 23); - this.CreateDumpTruckButton.TabIndex = 7; - this.CreateDumpTruckButton.Text = "Create DumpTruck"; - this.CreateDumpTruckButton.UseVisualStyleBackColor = true; - this.CreateDumpTruckButton.Click += new System.EventHandler(this.CreateDumpTruckButton_Click); + CreateDumpTruckButton.Location = new Point(190, 523); + CreateDumpTruckButton.Margin = new Padding(3, 4, 3, 4); + CreateDumpTruckButton.Name = "CreateDumpTruckButton"; + CreateDumpTruckButton.Size = new Size(192, 31); + CreateDumpTruckButton.TabIndex = 7; + CreateDumpTruckButton.Text = "Create DumpTruck"; + CreateDumpTruckButton.UseVisualStyleBackColor = true; + CreateDumpTruckButton.Click += CreateDumpTruckButton_Click; // // comboBoxStrategy // - this.comboBoxStrategy.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.comboBoxStrategy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; - this.comboBoxStrategy.FormattingEnabled = true; - this.comboBoxStrategy.Items.AddRange(new object[] { - "MoveToCenter", - "MoveToBorder"}); - this.comboBoxStrategy.Location = new System.Drawing.Point(751, 12); - this.comboBoxStrategy.Name = "comboBoxStrategy"; - this.comboBoxStrategy.Size = new System.Drawing.Size(121, 23); - this.comboBoxStrategy.TabIndex = 8; + comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right; + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "MoveToCenter", "MoveToBorder" }); + comboBoxStrategy.Location = new Point(858, 16); + comboBoxStrategy.Margin = new Padding(3, 4, 3, 4); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(138, 28); + comboBoxStrategy.TabIndex = 8; // // Step_Button // - this.Step_Button.Location = new System.Drawing.Point(797, 41); - this.Step_Button.Name = "Step_Button"; - this.Step_Button.Size = new System.Drawing.Size(75, 23); - this.Step_Button.TabIndex = 9; - this.Step_Button.Text = "Step"; - this.Step_Button.UseVisualStyleBackColor = true; - this.Step_Button.Click += new System.EventHandler(this.Step_Button_Click); + Step_Button.Location = new Point(911, 55); + Step_Button.Margin = new Padding(3, 4, 3, 4); + Step_Button.Name = "Step_Button"; + Step_Button.Size = new Size(86, 31); + Step_Button.TabIndex = 9; + Step_Button.Text = "Step"; + Step_Button.UseVisualStyleBackColor = true; + Step_Button.Click += Step_Button_Click; + // + // buttonSelectTruck + // + buttonSelectTruck.Location = new Point(443, 523); + buttonSelectTruck.Name = "buttonSelectTruck"; + buttonSelectTruck.Size = new Size(94, 29); + buttonSelectTruck.TabIndex = 10; + buttonSelectTruck.Text = "SelectTruck"; + buttonSelectTruck.UseVisualStyleBackColor = true; // // FormDumpTruck // - this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(884, 461); - this.Controls.Add(this.Step_Button); - this.Controls.Add(this.comboBoxStrategy); - this.Controls.Add(this.CreateDumpTruckButton); - this.Controls.Add(this.Create_Truck_Button); - this.Controls.Add(this.buttonDown); - this.Controls.Add(this.buttonRight); - this.Controls.Add(this.buttonLeft); - this.Controls.Add(this.buttonUp); - this.Controls.Add(this.pictureBoxDumpTruck); - this.Name = "FormDumpTruck"; - this.Text = "FormDumpTruck"; - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxDumpTruck)).EndInit(); - this.ResumeLayout(false); - + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1010, 615); + Controls.Add(buttonSelectTruck); + Controls.Add(Step_Button); + Controls.Add(comboBoxStrategy); + Controls.Add(CreateDumpTruckButton); + Controls.Add(Create_Truck_Button); + Controls.Add(buttonDown); + Controls.Add(buttonRight); + Controls.Add(buttonLeft); + Controls.Add(buttonUp); + Controls.Add(pictureBoxDumpTruck); + Margin = new Padding(3, 4, 3, 4); + Name = "FormDumpTruck"; + Text = "FormDumpTruck"; + ((System.ComponentModel.ISupportInitialize)pictureBoxDumpTruck).EndInit(); + ResumeLayout(false); } #endregion @@ -168,5 +186,6 @@ private Button CreateDumpTruckButton; private ComboBox comboBoxStrategy; private Button Step_Button; + private Button buttonSelectTruck; } } \ No newline at end of file diff --git a/DumpTruck/DumpTruck/FormDumpTruck.cs b/DumpTruck/DumpTruck/FormDumpTruck.cs index d7c5ead..0129dba 100644 --- a/DumpTruck/DumpTruck/FormDumpTruck.cs +++ b/DumpTruck/DumpTruck/FormDumpTruck.cs @@ -19,10 +19,21 @@ namespace DumpTruck private AbstractStrategy? _abstractStrategy; + /// Выбранный автомобиль + /// + public DrawningTruck? selectedTruck { get; private set; } + + + /// + /// Инициализация формы + /// public FormDumpTruck() { InitializeComponent(); + _abstractStrategy = null; + selectedTruck = null; } + private void Draw() { if (_drawningTruck == null) @@ -38,9 +49,14 @@ namespace DumpTruck private void Create_Truck_Button_Click(object sender, EventArgs e) { Random random = new(); + Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } _drawningTruck = new DrawningTruck(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + color,color, pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height); _drawningTruck.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); @@ -48,9 +64,14 @@ namespace DumpTruck private void CreateDumpTruckButton_Click(object sender, EventArgs e) { Random random = new(); + Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } _drawningTruck = new DrawningDumpTruck(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + color,color, Convert.ToBoolean(1), pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height); _drawningTruck.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); @@ -83,8 +104,8 @@ namespace DumpTruck break; } Draw(); - } - + } + private void Step_Button_Click(object sender, EventArgs e) { if (_drawningTruck == null) @@ -120,6 +141,11 @@ namespace DumpTruck comboBoxStrategy.Enabled = true; _abstractStrategy = null; } - } + } + private void buttonSelectTruck_Click(object sender, EventArgs e) + { + selectedTruck = _drawningTruck; + DialogResult = DialogResult.OK; + } } } diff --git a/DumpTruck/DumpTruck/FormDumpTruck.resx b/DumpTruck/DumpTruck/FormDumpTruck.resx index f298a7b..af32865 100644 --- a/DumpTruck/DumpTruck/FormDumpTruck.resx +++ b/DumpTruck/DumpTruck/FormDumpTruck.resx @@ -1,4 +1,64 @@ - + + + diff --git a/DumpTruck/DumpTruck/FormTruckCollection.Designer.cs b/DumpTruck/DumpTruck/FormTruckCollection.Designer.cs new file mode 100644 index 0000000..b6c8f80 --- /dev/null +++ b/DumpTruck/DumpTruck/FormTruckCollection.Designer.cs @@ -0,0 +1,123 @@ +namespace DumpTruck +{ + partial class FormTruckCollection + { + /// + /// 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() + { + groupBox1 = new GroupBox(); + maskedTextBoxNumber = new MaskedTextBox(); + buttonRefreshCollection = new Button(); + buttonRemoveTruck = new Button(); + buttonAddTruck = new Button(); + pictureBoxCollection = new PictureBox(); + groupBox1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + SuspendLayout(); + // + // groupBox1 + // + groupBox1.Controls.Add(maskedTextBoxNumber); + groupBox1.Controls.Add(buttonRefreshCollection); + groupBox1.Controls.Add(buttonRemoveTruck); + groupBox1.Controls.Add(buttonAddTruck); + groupBox1.Location = new Point(588, 12); + groupBox1.Name = "groupBox1"; + groupBox1.Size = new Size(200, 426); + groupBox1.TabIndex = 0; + groupBox1.TabStop = false; + groupBox1.Text = "Инструменты"; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Location = new Point(52, 101); + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(100, 23); + maskedTextBoxNumber.TabIndex = 4; + // + // buttonRefreshCollection + // + buttonRefreshCollection.Location = new Point(6, 224); + buttonRefreshCollection.Name = "buttonRefreshCollection"; + buttonRefreshCollection.Size = new Size(188, 33); + buttonRefreshCollection.TabIndex = 3; + buttonRefreshCollection.Text = "Обновить коллекцию"; + buttonRefreshCollection.UseVisualStyleBackColor = true; + buttonRefreshCollection.Click += buttonRefreshCollection_Click; + // + // buttonRemoveTruck + // + buttonRemoveTruck.Location = new Point(6, 130); + buttonRemoveTruck.Name = "buttonRemoveTruck"; + buttonRemoveTruck.Size = new Size(188, 34); + buttonRemoveTruck.TabIndex = 2; + buttonRemoveTruck.Text = "Удалить грузовик"; + buttonRemoveTruck.UseVisualStyleBackColor = true; + buttonRemoveTruck.Click += buttonRemoveTruck_Click; + // + // buttonAddTruck + // + buttonAddTruck.Location = new Point(6, 22); + buttonAddTruck.Name = "buttonAddTruck"; + buttonAddTruck.Size = new Size(188, 31); + buttonAddTruck.TabIndex = 1; + buttonAddTruck.Text = "Добавить грузовик"; + buttonAddTruck.UseVisualStyleBackColor = true; + buttonAddTruck.Click += buttonAddTruck_Click; + // + // pictureBoxCollection + // + pictureBoxCollection.Location = new Point(12, 12); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(570, 426); + pictureBoxCollection.TabIndex = 1; + pictureBoxCollection.TabStop = false; + // + // FormTruckCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(pictureBoxCollection); + Controls.Add(groupBox1); + Name = "FormTruckCollection"; + Text = "Набор грузовиков"; + groupBox1.ResumeLayout(false); + groupBox1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + ResumeLayout(false); + } + + #endregion + + private GroupBox groupBox1; + private Button buttonAddTruck; + private Button buttonRemoveTruck; + private Button buttonRefreshCollection; + private PictureBox pictureBoxCollection; + private MaskedTextBox maskedTextBoxNumber; + } +} \ No newline at end of file diff --git a/DumpTruck/DumpTruck/FormTruckCollection.cs b/DumpTruck/DumpTruck/FormTruckCollection.cs new file mode 100644 index 0000000..9bb6e43 --- /dev/null +++ b/DumpTruck/DumpTruck/FormTruckCollection.cs @@ -0,0 +1,72 @@ +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; +using DumpTruck.DrawningObjects; +using DumpTruck.Generics; +using DumpTruck.MovementStrategy; + + +namespace DumpTruck +{ + public partial class FormTruckCollection : Form + { + /// + /// Набор объектов + /// + private readonly TrucksGenericCollection _trucks; + + public FormTruckCollection() + { + InitializeComponent(); + _trucks = new TrucksGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + + private void buttonAddTruck_Click(object sender, EventArgs e) + { + FormDumpTruck form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_trucks + form.selectedTruck != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _trucks.ShowTrucks(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + + } + + private void buttonRemoveTruck_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (_trucks - pos) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _trucks.ShowTrucks(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + + } + + private void buttonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _trucks.ShowTrucks(); + } + } +} diff --git a/DumpTruck/DumpTruck/FormTruckCollection.resx b/DumpTruck/DumpTruck/FormTruckCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/DumpTruck/DumpTruck/FormTruckCollection.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/DumpTruck/DumpTruck/Program.cs b/DumpTruck/DumpTruck/Program.cs index b619d5a..6b3ec3d 100644 --- a/DumpTruck/DumpTruck/Program.cs +++ b/DumpTruck/DumpTruck/Program.cs @@ -1,3 +1,5 @@ +using DumpTruck; + namespace DumpTruck { internal static class Program @@ -11,7 +13,7 @@ namespace DumpTruck // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormDumpTruck()); + Application.Run(new FormTruckCollection()); } } } \ No newline at end of file diff --git a/DumpTruck/DumpTruck/SetGeneric.cs b/DumpTruck/DumpTruck/SetGeneric.cs new file mode 100644 index 0000000..2f3cdb8 --- /dev/null +++ b/DumpTruck/DumpTruck/SetGeneric.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.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 truck) + { + return Insert(truck, 0); + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый автомобиль + /// Позиция + /// + public int Insert(T truck, int position) + { + // TODO проверка позиции + if (position < 0 || position > Count) + { + return -1; + } + // TODO проверка, что элемент массива по этой позиции пустой, если нет, то + if (_places[position] != null) + { + // проверка, что после вставляемого элемента в массиве есть пустой элемент + int nullIndex = -1; + for (int i = position + 1; i < Count; i++) + { + if (_places[i] == null) + { + nullIndex = i; + break; + } + } + // Если пустого элемента нет, то выходим + if (nullIndex < 0) + { + return -1; + } + // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента + int j = nullIndex - 1; + while (j >= position) + { + _places[j + 1] = _places[j]; + j--; + } + } + // TODO вставка по позиции + _places[position] = truck; + return position; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + // TODO проверка позиции + if ((position < 0) || (position > Count)) return false; + // TODO удаление объекта из массива, присвоив элементу массива значение null + _places[position] = null; + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? Get(int position) + { + // TODO проверка позиции + if ((position < 0) || (position > Count)) return null; + return _places[position]; + } + } +} \ No newline at end of file diff --git a/DumpTruck/DumpTruck/TrucksGenericCollection.cs b/DumpTruck/DumpTruck/TrucksGenericCollection.cs new file mode 100644 index 0000000..da192b2 --- /dev/null +++ b/DumpTruck/DumpTruck/TrucksGenericCollection.cs @@ -0,0 +1,140 @@ +using DumpTruck.DrawningObjects; +using DumpTruck.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.Generics +{ + internal class TrucksGenericCollection + where T : DrawningTruck + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 110; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 70; + /// + /// Набор объектов + /// + private readonly SetGeneric _collection; + /// + /// Конструктор + /// + /// + /// + public TrucksGenericCollection(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 +(TrucksGenericCollection collect, T? + obj) + { + if (obj == null) + { + return -1; + } + return collect._collection.Insert(obj); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static bool operator -(TrucksGenericCollection collect, int pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + return collect._collection.Remove(pos); + } + return false; + } + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowTrucks() + { + 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; + + for (int i = 0; i < _collection.Count; i++) + { + // TODO получение объекта + // TODO установка позиции + // TODO прорисовка объекта + DrawningTruck? truck = _collection.Get(i); + if (truck == null) + continue; + truck.SetPosition(i % width * _placeSizeWidth, i / width * _placeSizeHeight); + truck.DrawTransport(g); + } + } + } +} \ No newline at end of file -- 2.25.1 From 729ce566423c10ba6c6cd021d5e70f82d1976e79 Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 14 Dec 2023 20:24:57 +0400 Subject: [PATCH 2/3] laba3 -100% --- DumpTruck/DumpTruck/DrawningTruck.cs | 4 +++ DumpTruck/DumpTruck/FormDumpTruck.Designer.cs | 1 + DumpTruck/DumpTruck/FormDumpTruck.cs | 14 ++++++-- .../DumpTruck/FormTruckCollection.Designer.cs | 36 +++++++++++-------- DumpTruck/DumpTruck/FormTruckCollection.cs | 7 +++- .../DumpTruck/TrucksGenericCollection.cs | 4 +-- 6 files changed, 46 insertions(+), 20 deletions(-) diff --git a/DumpTruck/DumpTruck/DrawningTruck.cs b/DumpTruck/DumpTruck/DrawningTruck.cs index 7033f26..bc1d030 100644 --- a/DumpTruck/DumpTruck/DrawningTruck.cs +++ b/DumpTruck/DumpTruck/DrawningTruck.cs @@ -95,6 +95,10 @@ namespace DumpTruck.DrawningObjects protected DrawningTruck(int speed, double weight, Color bodyColor, Color additionalColor, int width, int height, int truckWidth, int truckHeight) { // TODO: Продумать проверки + if (width < _truckWidth || height < _truckHeight) + { + return; + } _pictureWidth = width; _pictureHeight = height; _truckWidth = truckWidth; diff --git a/DumpTruck/DumpTruck/FormDumpTruck.Designer.cs b/DumpTruck/DumpTruck/FormDumpTruck.Designer.cs index 8effbed..1a74e81 100644 --- a/DumpTruck/DumpTruck/FormDumpTruck.Designer.cs +++ b/DumpTruck/DumpTruck/FormDumpTruck.Designer.cs @@ -152,6 +152,7 @@ buttonSelectTruck.TabIndex = 10; buttonSelectTruck.Text = "SelectTruck"; buttonSelectTruck.UseVisualStyleBackColor = true; + buttonSelectTruck.Click += buttonSelectTruck_Click_1; // // FormDumpTruck // diff --git a/DumpTruck/DumpTruck/FormDumpTruck.cs b/DumpTruck/DumpTruck/FormDumpTruck.cs index 0129dba..c99f10d 100644 --- a/DumpTruck/DumpTruck/FormDumpTruck.cs +++ b/DumpTruck/DumpTruck/FormDumpTruck.cs @@ -55,8 +55,9 @@ namespace DumpTruck { color = dialog.Color; } + _drawningTruck = new DrawningTruck(random.Next(100, 300), random.Next(1000, 3000), - color,color, + color, color, pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height); _drawningTruck.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); @@ -70,8 +71,14 @@ namespace DumpTruck { color = dialog.Color; } + Color color2 = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog2 = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color2 = dialog2.Color; + } _drawningTruck = new DrawningDumpTruck(random.Next(100, 300), random.Next(1000, 3000), - color,color, + color, color2, Convert.ToBoolean(1), pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height); _drawningTruck.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); @@ -142,7 +149,8 @@ namespace DumpTruck _abstractStrategy = null; } } - private void buttonSelectTruck_Click(object sender, EventArgs e) + + private void buttonSelectTruck_Click_1(object sender, EventArgs e) { selectedTruck = _drawningTruck; DialogResult = DialogResult.OK; diff --git a/DumpTruck/DumpTruck/FormTruckCollection.Designer.cs b/DumpTruck/DumpTruck/FormTruckCollection.Designer.cs index b6c8f80..ba3a3e5 100644 --- a/DumpTruck/DumpTruck/FormTruckCollection.Designer.cs +++ b/DumpTruck/DumpTruck/FormTruckCollection.Designer.cs @@ -44,25 +44,29 @@ groupBox1.Controls.Add(buttonRefreshCollection); groupBox1.Controls.Add(buttonRemoveTruck); groupBox1.Controls.Add(buttonAddTruck); - groupBox1.Location = new Point(588, 12); + groupBox1.Location = new Point(689, 16); + groupBox1.Margin = new Padding(3, 4, 3, 4); groupBox1.Name = "groupBox1"; - groupBox1.Size = new Size(200, 426); + groupBox1.Padding = new Padding(3, 4, 3, 4); + groupBox1.Size = new Size(229, 568); groupBox1.TabIndex = 0; groupBox1.TabStop = false; groupBox1.Text = "Инструменты"; // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(52, 101); + maskedTextBoxNumber.Location = new Point(59, 135); + maskedTextBoxNumber.Margin = new Padding(3, 4, 3, 4); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - maskedTextBoxNumber.Size = new Size(100, 23); + maskedTextBoxNumber.Size = new Size(114, 27); maskedTextBoxNumber.TabIndex = 4; // // buttonRefreshCollection // - buttonRefreshCollection.Location = new Point(6, 224); + buttonRefreshCollection.Location = new Point(7, 299); + buttonRefreshCollection.Margin = new Padding(3, 4, 3, 4); buttonRefreshCollection.Name = "buttonRefreshCollection"; - buttonRefreshCollection.Size = new Size(188, 33); + buttonRefreshCollection.Size = new Size(215, 44); buttonRefreshCollection.TabIndex = 3; buttonRefreshCollection.Text = "Обновить коллекцию"; buttonRefreshCollection.UseVisualStyleBackColor = true; @@ -70,9 +74,10 @@ // // buttonRemoveTruck // - buttonRemoveTruck.Location = new Point(6, 130); + buttonRemoveTruck.Location = new Point(7, 173); + buttonRemoveTruck.Margin = new Padding(3, 4, 3, 4); buttonRemoveTruck.Name = "buttonRemoveTruck"; - buttonRemoveTruck.Size = new Size(188, 34); + buttonRemoveTruck.Size = new Size(215, 45); buttonRemoveTruck.TabIndex = 2; buttonRemoveTruck.Text = "Удалить грузовик"; buttonRemoveTruck.UseVisualStyleBackColor = true; @@ -80,9 +85,10 @@ // // buttonAddTruck // - buttonAddTruck.Location = new Point(6, 22); + buttonAddTruck.Location = new Point(7, 29); + buttonAddTruck.Margin = new Padding(3, 4, 3, 4); buttonAddTruck.Name = "buttonAddTruck"; - buttonAddTruck.Size = new Size(188, 31); + buttonAddTruck.Size = new Size(215, 41); buttonAddTruck.TabIndex = 1; buttonAddTruck.Text = "Добавить грузовик"; buttonAddTruck.UseVisualStyleBackColor = true; @@ -90,19 +96,21 @@ // // pictureBoxCollection // - pictureBoxCollection.Location = new Point(12, 12); + pictureBoxCollection.Location = new Point(12, 16); + pictureBoxCollection.Margin = new Padding(3, 4, 3, 4); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(570, 426); + pictureBoxCollection.Size = new Size(651, 568); pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; // // FormTruckCollection // - AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + ClientSize = new Size(935, 593); Controls.Add(pictureBoxCollection); Controls.Add(groupBox1); + Margin = new Padding(3, 4, 3, 4); Name = "FormTruckCollection"; Text = "Набор грузовиков"; groupBox1.ResumeLayout(false); diff --git a/DumpTruck/DumpTruck/FormTruckCollection.cs b/DumpTruck/DumpTruck/FormTruckCollection.cs index 9bb6e43..9cb1184 100644 --- a/DumpTruck/DumpTruck/FormTruckCollection.cs +++ b/DumpTruck/DumpTruck/FormTruckCollection.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using System.Xml; using DumpTruck.DrawningObjects; using DumpTruck.Generics; using DumpTruck.MovementStrategy; @@ -47,16 +48,20 @@ namespace DumpTruck private void buttonRemoveTruck_Click(object sender, EventArgs e) { - if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } + + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); if (_trucks - pos) { MessageBox.Show("Объект удален"); pictureBoxCollection.Image = _trucks.ShowTrucks(); } + else { MessageBox.Show("Не удалось удалить объект"); diff --git a/DumpTruck/DumpTruck/TrucksGenericCollection.cs b/DumpTruck/DumpTruck/TrucksGenericCollection.cs index da192b2..d56e408 100644 --- a/DumpTruck/DumpTruck/TrucksGenericCollection.cs +++ b/DumpTruck/DumpTruck/TrucksGenericCollection.cs @@ -23,11 +23,11 @@ namespace DumpTruck.Generics /// /// Размер занимаемого объектом места (ширина) /// - private readonly int _placeSizeWidth = 110; + private readonly int _placeSizeWidth = 140; /// /// Размер занимаемого объектом места (высота) /// - private readonly int _placeSizeHeight = 70; + private readonly int _placeSizeHeight = 100; /// /// Набор объектов /// -- 2.25.1 From 76412b309a64722a9499dfde64bf04128992b554 Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 15 Dec 2023 14:10:17 +0400 Subject: [PATCH 3/3] pull --- DumpTruck/DumpTruck/DrawningDumpTruck.cs | 2 +- DumpTruck/DumpTruck/DrawningTruck.cs | 2 +- DumpTruck/DumpTruck/FormDumpTruck.cs | 12 ++++++------ DumpTruck/DumpTruck/TrucksGenericCollection.cs | 7 +++---- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/DumpTruck/DumpTruck/DrawningDumpTruck.cs b/DumpTruck/DumpTruck/DrawningDumpTruck.cs index 6ecb5ee..1fd16b9 100644 --- a/DumpTruck/DumpTruck/DrawningDumpTruck.cs +++ b/DumpTruck/DumpTruck/DrawningDumpTruck.cs @@ -40,7 +40,7 @@ namespace DumpTruck.DrawningObjects if (dumpTruck.Trailer) { //прицеп - Brush trailer = new SolidBrush(Color.Black); + Brush trailer = new SolidBrush(EntityTruck.AdditionalColor); g.FillRectangle(trailer, _startPosX, _startPosY, 5, 38); g.FillRectangle(trailer, _startPosX + 5, _startPosY + 33, 70, 5); g.FillRectangle(trailer, _startPosX + 70, _startPosY, 5, 38); diff --git a/DumpTruck/DumpTruck/DrawningTruck.cs b/DumpTruck/DumpTruck/DrawningTruck.cs index bc1d030..fff37be 100644 --- a/DumpTruck/DumpTruck/DrawningTruck.cs +++ b/DumpTruck/DumpTruck/DrawningTruck.cs @@ -203,7 +203,7 @@ namespace DumpTruck.DrawningObjects //кузов Brush br = new SolidBrush(EntityTruck.BodyColor); g.FillRectangle(br, _startPosX + 81, _startPosY + 1, 29, 40); - Brush br1 = new SolidBrush(EntityTruck.AdditionalColor); + Brush br1 = new SolidBrush(EntityTruck.BodyColor); g.FillRectangle(br1, _startPosX + 1, _startPosY + 41, 109, 19); //колеса Brush wheels = new SolidBrush(Color.Black); diff --git a/DumpTruck/DumpTruck/FormDumpTruck.cs b/DumpTruck/DumpTruck/FormDumpTruck.cs index c99f10d..09a7b96 100644 --- a/DumpTruck/DumpTruck/FormDumpTruck.cs +++ b/DumpTruck/DumpTruck/FormDumpTruck.cs @@ -65,20 +65,20 @@ namespace DumpTruck private void CreateDumpTruckButton_Click(object sender, EventArgs e) { Random random = new(); - Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); - ColorDialog dialog = new(); - if (dialog.ShowDialog() == DialogResult.OK) + Color color1 = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog1 = new(); + if (dialog1.ShowDialog() == DialogResult.OK) { - color = dialog.Color; + color1 = dialog1.Color; } Color color2 = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); ColorDialog dialog2 = new(); - if (dialog.ShowDialog() == DialogResult.OK) + if (dialog2.ShowDialog() == DialogResult.OK) { color2 = dialog2.Color; } _drawningTruck = new DrawningDumpTruck(random.Next(100, 300), random.Next(1000, 3000), - color, color2, + color1, color2, Convert.ToBoolean(1), pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height); _drawningTruck.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); diff --git a/DumpTruck/DumpTruck/TrucksGenericCollection.cs b/DumpTruck/DumpTruck/TrucksGenericCollection.cs index d56e408..4633981 100644 --- a/DumpTruck/DumpTruck/TrucksGenericCollection.cs +++ b/DumpTruck/DumpTruck/TrucksGenericCollection.cs @@ -51,8 +51,7 @@ namespace DumpTruck.Generics /// /// /// - public static int operator +(TrucksGenericCollection collect, T? - obj) + public static int operator +(TrucksGenericCollection collect, T? obj) { if (obj == null) { @@ -107,7 +106,7 @@ namespace DumpTruck.Generics { for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) - {//линия рамзетки места + {//рамзетка места g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); @@ -129,7 +128,7 @@ namespace DumpTruck.Generics // TODO получение объекта // TODO установка позиции // TODO прорисовка объекта - DrawningTruck? truck = _collection.Get(i); + T? truck = _collection.Get(i); if (truck == null) continue; truck.SetPosition(i % width * _placeSizeWidth, i / width * _placeSizeHeight); -- 2.25.1