From 5419c0c52d5875ed6f5a1a3472eb9d40ac45c5ad Mon Sep 17 00:00:00 2001 From: vladimir_zinovev Date: Thu, 2 Nov 2023 20:50:20 +0400 Subject: [PATCH] laba3 is done --- ...ArmoredTransportCollectionForm.Designer.cs | 1 - .../ArmoredTransportCollectionForm.cs | 7 +-- .../ArmoredTransportsGenericCollection.cs | 48 ++++++++++------- ProjectTank/ProjectTank/SetGeneric.cs | 51 +++++++++++++------ ProjectTank/ProjectTank/TankForm.Designer.cs | 15 ++++++ ProjectTank/ProjectTank/TankForm.cs | 19 +++++-- 6 files changed, 97 insertions(+), 44 deletions(-) diff --git a/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.Designer.cs b/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.Designer.cs index b8e9ce8..d99b1e8 100644 --- a/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.Designer.cs +++ b/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.Designer.cs @@ -84,7 +84,6 @@ this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(275, 39); this.maskedTextBoxNumber.TabIndex = 1; - this.maskedTextBoxNumber.TextChanged += new System.EventHandler(this.maskedTextBoxNumber_TextChanged); // // ButtonAddArmoredTransport // diff --git a/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.cs b/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.cs index e3255e7..826e20c 100644 --- a/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.cs +++ b/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.cs @@ -40,7 +40,7 @@ namespace ProjectTank TankForm form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_armoredTransports + form.SelectedArmoredTransport) + if (_armoredTransports + form.SelectedArmoredTransport != null) { MessageBox.Show("Объект добавлен"); pictureBoxCollection.Image = _armoredTransports.ShowArmoredTransports(); @@ -84,10 +84,5 @@ namespace ProjectTank { pictureBoxCollection.Image = _armoredTransports.ShowArmoredTransports(); } - - private void maskedTextBoxNumber_TextChanged(object sender, EventArgs e) - { - - } } } diff --git a/ProjectTank/ProjectTank/ArmoredTransportsGenericCollection.cs b/ProjectTank/ProjectTank/ArmoredTransportsGenericCollection.cs index 7e213ed..ff3f105 100644 --- a/ProjectTank/ProjectTank/ArmoredTransportsGenericCollection.cs +++ b/ProjectTank/ProjectTank/ArmoredTransportsGenericCollection.cs @@ -17,6 +17,10 @@ namespace ProjectTank.Generics where T : DrawningArmoredTransport where U : IMoveableObject { + /// + /// Коэффициент изменения ширины парковки + /// + readonly int koefWidth = 2; /// /// Ширина окна прорисовки /// @@ -32,7 +36,7 @@ namespace ProjectTank.Generics /// /// Размер занимаемого объектом места (высота) /// - private readonly int _placeSizeHeight = 80; + private readonly int _placeSizeHeight = 90; /// /// Набор объектов /// @@ -56,13 +60,13 @@ namespace ProjectTank.Generics /// /// /// - public static bool operator +(ArmoredTransportsGenericCollection collect, T? obj) + public static int operator +(ArmoredTransportsGenericCollection collect, T? obj) { - if (obj == null) + if (obj == null || collect == null) { - return false; + return -1; } - return collect?._collection.Insert(obj) ?? false; + return collect._collection.Insert(obj); } /// /// Перегрузка оператора вычитания @@ -70,15 +74,14 @@ namespace ProjectTank.Generics /// /// /// - public static T? operator -(ArmoredTransportsGenericCollection collect, int - pos) + public static bool operator -(ArmoredTransportsGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); - if (obj != null) + T? obj = collect?._collection.Get(pos); + if (obj != null && collect != null) { - collect._collection.Remove(pos); + return collect._collection.Remove(pos); } - return obj; + return false; } /// /// Получение объекта IMoveableObject @@ -108,17 +111,19 @@ namespace ProjectTank.Generics private void DrawBackground(Graphics g) { Pen pen = new(Color.Black, 3); - for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + for (int i = 0; i < (_pictureWidth / _placeSizeWidth)/koefWidth; i++) //изменение ширины парковки { - for (int j = 0; j < _pictureHeight / _placeSizeHeight + - 1; ++j) + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) { //линия рамзетки места - g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, + g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j *_placeSizeHeight); + g.DrawString("P", new Font("Courier New", 30.0F), new SolidBrush(Color.Blue), + new Point(i * _placeSizeWidth, (j-1) * _placeSizeHeight)); } g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } } /// @@ -127,11 +132,18 @@ namespace ProjectTank.Generics /// private void DrawObjects(Graphics g) { + int width = (_pictureWidth / _placeSizeWidth)/koefWidth; //изменение ширины парковки + int height = _pictureHeight / _placeSizeHeight; for (int i = 0; i < _collection.Count; i++) { - // TODO получение объекта - // TODO установка позиции - // TODO прорисовка объекта + DrawningArmoredTransport? armoredTransport = _collection.Get(i); + if (armoredTransport != null) + { + int row = height - 1 - (i / width); + int col = width - 1 - (i % width); + armoredTransport.SetPosition(col * _placeSizeWidth, row * _placeSizeHeight); + armoredTransport.DrawTransport(g); + } } } } diff --git a/ProjectTank/ProjectTank/SetGeneric.cs b/ProjectTank/ProjectTank/SetGeneric.cs index 2fcdd84..63a2874 100644 --- a/ProjectTank/ProjectTank/SetGeneric.cs +++ b/ProjectTank/ProjectTank/SetGeneric.cs @@ -32,12 +32,18 @@ namespace ProjectTank.Generics /// /// Добавление объекта в набор /// - /// Добавляемый автомобиль + /// Добавляемый автомобиль /// - public bool Insert(T car) + public int Insert(T armoredTransport) { - // TODO вставка в начало набора - return true; + if (_places[Count - 1] != null) + { + return -1; + } + else + { + return Insert(armoredTransport,0); + } } /// /// Добавление объекта в набор на конкретную позицию @@ -45,15 +51,22 @@ namespace ProjectTank.Generics /// Добавляемый автомобиль /// Позиция /// - public bool Insert(T car, int position) + public int Insert(T armoredTransport, int position) { - // TODO проверка позиции - // TODO проверка, что элемент массива по этой позиции пустой, если нет, то - // проверка, что после вставляемого элемента в массиве есть пустой элемент - // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - // TODO вставка по позиции - _places[position] = car; - return true; + if (!(position >= 0 && position < Count)) + return -1; + if (_places[position] != null) + { + int ind = position; + while (ind < Count && _places[ind] != null) + ind++; + if (ind == Count) + return -1; + for (int i = ind - 1; i >= position; i--) + _places[i + 1] = _places[i]; + } + _places[position] = armoredTransport; + return position; } /// /// Удаление объекта из набора с конкретной позиции @@ -62,9 +75,12 @@ namespace ProjectTank.Generics /// public bool Remove(int position) { - // TODO проверка позиции - // TODO удаление объекта из массива, присвоив элементу массива значение null - return true; + if (!(position >= 0 && position < Count) || _places[position] == null) + { + return false; + } + _places[position] = null; + return true; } /// /// Получение объекта из набора по позиции @@ -73,7 +89,10 @@ namespace ProjectTank.Generics /// public T? Get(int position) { - // TODO проверка позиции + if (!(position >= 0 && position < Count)) + { + return null; + } return _places[position]; } } diff --git a/ProjectTank/ProjectTank/TankForm.Designer.cs b/ProjectTank/ProjectTank/TankForm.Designer.cs index dcc809e..e4d9053 100644 --- a/ProjectTank/ProjectTank/TankForm.Designer.cs +++ b/ProjectTank/ProjectTank/TankForm.Designer.cs @@ -38,6 +38,7 @@ this.comboBoxStrategy = new System.Windows.Forms.ComboBox(); this.ButtonCreateArmoredTransport = new System.Windows.Forms.Button(); this.ButtonStep = new System.Windows.Forms.Button(); + this.ButtonSelectArmoredTransport = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxTanks)).BeginInit(); this.SuspendLayout(); // @@ -162,11 +163,24 @@ this.ButtonStep.UseVisualStyleBackColor = true; this.ButtonStep.Click += new System.EventHandler(this.ButtonStep_Click); // + // ButtonSelectArmoredTransport + // + this.ButtonSelectArmoredTransport.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.ButtonSelectArmoredTransport.Location = new System.Drawing.Point(664, 806); + this.ButtonSelectArmoredTransport.Margin = new System.Windows.Forms.Padding(6); + this.ButtonSelectArmoredTransport.Name = "ButtonSelectArmoredTransport"; + this.ButtonSelectArmoredTransport.Size = new System.Drawing.Size(227, 49); + this.ButtonSelectArmoredTransport.TabIndex = 10; + this.ButtonSelectArmoredTransport.Text = "Выбор"; + this.ButtonSelectArmoredTransport.UseVisualStyleBackColor = true; + this.ButtonSelectArmoredTransport.Click += new System.EventHandler(this.ButtonSelectArmoredTransport_Click); + // // TankForm // this.AutoScaleDimensions = new System.Drawing.SizeF(13F, 32F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1445, 881); + this.Controls.Add(this.ButtonSelectArmoredTransport); this.Controls.Add(this.ButtonStep); this.Controls.Add(this.ButtonCreateArmoredTransport); this.Controls.Add(this.comboBoxStrategy); @@ -197,5 +211,6 @@ private ComboBox comboBoxStrategy; private Button ButtonCreateArmoredTransport; private Button ButtonStep; + private Button ButtonSelectArmoredTransport; } } \ No newline at end of file diff --git a/ProjectTank/ProjectTank/TankForm.cs b/ProjectTank/ProjectTank/TankForm.cs index 4402c9e..9bebbd4 100644 --- a/ProjectTank/ProjectTank/TankForm.cs +++ b/ProjectTank/ProjectTank/TankForm.cs @@ -53,9 +53,17 @@ namespace ProjectTank { Random random = new(); Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); - //TODO + ColorDialog dialogMain = new(); + if (dialogMain.ShowDialog() == DialogResult.OK) + { + color = dialogMain.Color; + } Color dopColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); - //TODO + ColorDialog dialogDop = new(); + if (dialogDop.ShowDialog() == DialogResult.OK) + { + dopColor = dialogDop.Color; + } _drawningArmoredTransport = new DrawningTank(random.Next(100, 300), random.Next(1000, 3000), color, dopColor,Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), @@ -73,6 +81,11 @@ namespace ProjectTank Random random = new(); Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); //TODO + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } _drawningArmoredTransport = new DrawningArmoredTransport(random.Next(100, 300),random.Next(1000, 3000), color, pictureBoxTanks.Width, pictureBoxTanks.Height); _drawningArmoredTransport.SetPosition(random.Next(10, 100), random.Next(10,100)); @@ -152,7 +165,7 @@ namespace ProjectTank /// /// /// - private void ButtonSelectCar_Click(object sender, EventArgs e) + private void ButtonSelectArmoredTransport_Click(object sender, EventArgs e) { SelectedArmoredTransport = _drawningArmoredTransport; DialogResult = DialogResult.OK;