Вторая лабораторная работа

This commit is contained in:
allllen4a 2023-10-18 13:00:35 +03:00
parent 2bd34526e3
commit 2b931f9b23
10 changed files with 639 additions and 16 deletions

View File

@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using projectDoubleDeckerBus.Drawings;
using projectDouble_Decker_Bus.MovementStrategy;
using projectDoubleDeckerBus.Generics;
namespace projectDoubleDeckerBus
{
internal class BusesGenericCollection<T, U>
where T : DrawningBus
where U : IMoveableObject
{
/// <summary>
/// Ширина окна прорисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна прорисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Размер занимаемого объектом места (ширина)
/// </summary>
private readonly int _placeSizeWidth = 172;
/// <summary>
/// Размер занимаемого объектом места (высота)
/// </summary>
private readonly int _placeSizeHeight = 112;
/// <summary>
/// Набор объектов
/// </summary>
private readonly SetGeneric<T> _collection;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="picWidth"></param>
/// <param name="picHeight"></param>
public BusesGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
/// <summary>
/// Перегрузка оператора сложения
/// </summary>
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
///
public static int? operator +(BusesGenericCollection<T, U> collect, T?
obj)
{
if (obj == null)
{
return -1;
}
return collect?._collection.Insert(obj);
}
public static bool operator -(BusesGenericCollection<T, U> 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;
}
/// <summary>
/// Вывод всего набора объектов
/// </summary>
/// <returns></returns>
public Bitmap ShowCars()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawObjects(gr);
return bmp;
}
/// <summary>
/// Метод отрисовки фона
/// </summary>
/// <param name="g"></param>
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);
}
}
/// Метод прорисовки объектов
/// </summary>
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
for (int i = 0; i < _collection.Count; i++)
{
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int j = 0; j < _collection.Count; j++)
{
DrawningBus? bus = _collection.Get(j);
if (bus == null)
continue;
bus.SetPosition((width - j % width - 1) * _placeSizeWidth, j / width * _placeSizeHeight);
bus.DrawTransport(g);
}
}
}
}
}

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using projectDoubleDeckerBus;
using System.Drawing;
using projectDoubleDeckerBus.Entity;
using projectDouble_Decker_Bus.MovementStrategy;
namespace projectDoubleDeckerBus.Drawings
{
@ -56,6 +57,9 @@ namespace projectDoubleDeckerBus.Drawings
/// Высота объекта
/// </summary>
public int GetHeight => _busHeight;
public IMoveableObject GetMoveableObject => new
DrawningObjectBus(this);
/// <summary>
/// Инициализация свойств
/// </summary>
@ -162,6 +166,7 @@ namespace projectDoubleDeckerBus.Drawings
{
if (busWidth >= width || busHeight >= height)
{
Console.WriteLine("Проверка не пройдена, нельзя создать объект в этих размерах");
if (busWidth >= width)
{
busWidth = width - busWidth;
@ -172,6 +177,7 @@ namespace projectDoubleDeckerBus.Drawings
}
}
else
Console.WriteLine("Объект создан");
_pictureWidth = width;
_pictureHeight = height;
@ -182,7 +188,7 @@ namespace projectDoubleDeckerBus.Drawings
public void SetPosition(int x, int y)
{
if (((x + _busWidth > _pictureWidth)) || ((y >= 0) && (y + _busHeight > _pictureHeight)))
if (((x >= 0) && (x + _busWidth > _pictureWidth)) || ((y >= 0) && (y + _busHeight > _pictureHeight)))
{
_startPosX = _pictureWidth - _busWidth;
_startPosY = _pictureHeight - _busHeight;
@ -267,6 +273,7 @@ namespace projectDoubleDeckerBus.Drawings
g.DrawEllipse(pen, _startPosX + 130, _startPosY + 50, windowSize, windowSize);
g.DrawEllipse(pen, _startPosX + 145, _startPosY + 50, windowSize, windowSize);
g.FillRectangle(brBlack, _startPosX + 80, _startPosY + 46, 15, 40);
g.FillEllipse(brBlack, _startPosX + 50, _startPosY + 75, 20, 20);
g.FillEllipse(brBlack, _startPosX + 140, _startPosY + 75, 20, 20);

View File

@ -7,7 +7,6 @@ using System.Drawing;
namespace projectDoubleDeckerBus.Entity
{
public class EntityDoubleDeckerBus : EntityBus
{
/// <summary>

View File

@ -0,0 +1,136 @@
namespace projectDoubleDeckerBus
{
partial class FormBusCollection
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
panel1 = new Panel();
label1 = new Label();
InputtextBox = new TextBox();
buttonUpdateCollection = new Button();
buttonDeleteBus = new Button();
buttonAddBus = new Button();
DrawBus = new PictureBox();
panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)DrawBus).BeginInit();
SuspendLayout();
//
// panel1
//
panel1.Controls.Add(label1);
panel1.Controls.Add(InputtextBox);
panel1.Controls.Add(buttonUpdateCollection);
panel1.Controls.Add(buttonDeleteBus);
panel1.Controls.Add(buttonAddBus);
panel1.Controls.Add(DrawBus);
panel1.Dock = DockStyle.Fill;
panel1.Location = new Point(0, 0);
panel1.Name = "panel1";
panel1.Size = new Size(800, 450);
panel1.TabIndex = 0;
//
// label1
//
label1.AutoSize = true;
label1.Location = new Point(700, 9);
label1.Name = "label1";
label1.Size = new Size(34, 15);
label1.TabIndex = 1;
label1.Text = "Tools";
label1.TextAlign = ContentAlignment.TopCenter;
//
// InputtextBox
//
InputtextBox.Location = new Point(700, 150);
InputtextBox.Name = "InputtextBox";
InputtextBox.Size = new Size(88, 23);
InputtextBox.TabIndex = 1;
//
// buttonUpdateCollection
//
buttonUpdateCollection.Location = new Point(700, 206);
buttonUpdateCollection.Name = "buttonUpdateCollection";
buttonUpdateCollection.Size = new Size(88, 39);
buttonUpdateCollection.TabIndex = 1;
buttonUpdateCollection.Text = "Update the collection";
buttonUpdateCollection.UseVisualStyleBackColor = true;
buttonUpdateCollection.Click += buttonUpdateCollection_Click;
//
// buttonDeleteBus
//
buttonDeleteBus.Location = new Point(700, 92);
buttonDeleteBus.Name = "buttonDeleteBus";
buttonDeleteBus.Size = new Size(88, 36);
buttonDeleteBus.TabIndex = 1;
buttonDeleteBus.Text = "Delete bus";
buttonDeleteBus.UseVisualStyleBackColor = true;
buttonDeleteBus.Click += buttonDeleteBus_Click;
//
// buttonAddBus
//
buttonAddBus.Location = new Point(700, 30);
buttonAddBus.Name = "buttonAddBus";
buttonAddBus.Size = new Size(88, 36);
buttonAddBus.TabIndex = 1;
buttonAddBus.Text = "Add bus";
buttonAddBus.UseVisualStyleBackColor = true;
buttonAddBus.Click += buttonAddBus_Click;
//
// DrawBus
//
DrawBus.Dock = DockStyle.Fill;
DrawBus.Location = new Point(0, 0);
DrawBus.Name = "DrawBus";
DrawBus.Size = new Size(800, 450);
DrawBus.TabIndex = 0;
DrawBus.TabStop = false;
//
// FormBusCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(panel1);
Name = "FormBusCollection";
Text = "FormBusCollection";
panel1.ResumeLayout(false);
panel1.PerformLayout();
((System.ComponentModel.ISupportInitialize)DrawBus).EndInit();
ResumeLayout(false);
}
#endregion
private Panel panel1;
private Label label1;
private TextBox InputtextBox;
private Button buttonUpdateCollection;
private Button buttonDeleteBus;
private Button buttonAddBus;
private PictureBox DrawBus;
}
}

View File

@ -0,0 +1,68 @@
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 projectDoubleDeckerBus.Drawings;
using projectDouble_Decker_Bus.MovementStrategy;
using projectDoubleDeckerBus.Generics;
namespace projectDoubleDeckerBus
{
public partial class FormBusCollection : Form
{
private readonly BusesGenericCollection<DrawningBus,
DrawningObjectBus> _buses;
public FormBusCollection()
{
InitializeComponent();
_buses = new BusesGenericCollection<DrawningBus,
DrawningObjectBus>(DrawBus.Width, DrawBus.Height);
}
private void buttonAddBus_Click(object sender, EventArgs e)
{
FormDoubleDeckerBus form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_buses + form.SelectedBus != -1)
{
MessageBox.Show("Объект добавлен");
DrawBus.Image = _buses.ShowCars();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void buttonDeleteBus_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(InputtextBox.Text);
if (_buses - pos != null)
{
MessageBox.Show("Объект удален");
DrawBus.Image = _buses.ShowCars();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void buttonUpdateCollection_Click(object sender, EventArgs e)
{
DrawBus.Image = _buses.ShowCars();
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -37,6 +37,7 @@
ButtonCreateBus = new Button();
buttonStep = new Button();
comboBoxStrategy = new ComboBox();
buttonAddBus = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxDoubleDeckerBus).BeginInit();
SuspendLayout();
//
@ -134,11 +135,22 @@
comboBoxStrategy.Size = new Size(117, 23);
comboBoxStrategy.TabIndex = 9;
//
// buttonAddBus
//
buttonAddBus.Location = new Point(671, 135);
buttonAddBus.Name = "buttonAddBus";
buttonAddBus.Size = new Size(117, 30);
buttonAddBus.TabIndex = 10;
buttonAddBus.Text = "Add bus";
buttonAddBus.UseVisualStyleBackColor = true;
buttonAddBus.Click += buttonAddBus_Click;
//
// FormDoubleDeckerBus
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(buttonAddBus);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonStep);
Controls.Add(ButtonCreateBus);
@ -165,5 +177,6 @@
private Button ButtonCreateBus;
private Button buttonStep;
private ComboBox comboBoxStrategy;
private Button buttonAddBus;
}
}

View File

@ -1,3 +1,12 @@
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 projectDoubleDeckerBus.Drawings;
using projectDoubleDeckerBus.Entity;
using projectDouble_Decker_Bus.MovementStrategy;
@ -9,9 +18,18 @@ namespace projectDoubleDeckerBus
private DrawningBus? _drawningBus;
private AbstractStrategy? _abstractStrategy;
private AbstractStrategy? _strategy;
/// <summary>
/// Âûáðàííûé àâòîáóñ
/// </summary>
public DrawningBus? SelectedBus { get; private set; }
public FormDoubleDeckerBus()
{
InitializeComponent();
_strategy = null;
SelectedBus = null;
}
private void Draw()
@ -29,16 +47,25 @@ namespace projectDoubleDeckerBus
private void buttonCreateDoubleDeckerBus_Click(object sender, EventArgs e)
{
Random random = new();
Color mainColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
mainColor = dialog.Color;
}
Color addColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
if (dialog.ShowDialog() == DialogResult.OK)
{
addColor = dialog.Color;
}
_drawningBus = new DrawningDoubleDeckerBus(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)),
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)),
random.Next(1000, 3000), mainColor,
addColor, Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)),
pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height);
_drawningBus.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
@ -70,10 +97,15 @@ namespace projectDoubleDeckerBus
private void buttonCreateBus_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;
}
_drawningBus = new DrawningBus(random.Next(100, 300),
random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)),
random.Next(1000, 3000), color,
pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height);
_drawningBus.SetPosition(random.Next(10, 100), random.Next(10,
100));
@ -117,5 +149,11 @@ namespace projectDoubleDeckerBus
}
}
private void buttonAddBus_Click(object sender, EventArgs e)
{
SelectedBus = _drawningBus;
DialogResult = DialogResult.OK;
}
}
}

View File

@ -11,7 +11,7 @@ namespace projectDoubleDeckerBus
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new FormDoubleDeckerBus());
Application.Run(new FormBusCollection());
}
}
}

View File

@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace projectDoubleDeckerBus.Generics
{
internal class SetGeneric<T>
where T : class
{
/// <summary>
/// Массив объектов, которые храним
/// </summary>
private readonly T?[] _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetGeneric(int count)
{
_places = new T?[count];
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <returns></returns>
public int Insert(T bus)
{
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] = bus;
return 0;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// /// </summary>
/// <param name="bus">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T bus, int position)
{
if (position < 0 || position >= Count)
return -1;
if (_places[position] == null)
{
_places[position] = bus;
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] = bus;
return position;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position)
{
if (position < 0 || position >= Count)
return false;
_places[position] = null;
return true;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? Get(int position)
{
if (position < 0 || position >= Count)
return null;
return _places[position];
}
}
}