Compare commits

...

2 Commits

Author SHA1 Message Date
c21d23e3e6 лабораторная 3 2023-10-21 20:39:45 +04:00
06b1fe82e7 лаба 3 2023-10-20 21:35:03 +04:00
11 changed files with 455 additions and 67 deletions

View File

@ -9,9 +9,8 @@
</PropertyGroup>
<ItemGroup>
<Compile Remove="AirplanesGenericCollection.cs" />
<Compile Remove="Class1.cs" />
<Compile Remove="SetGeneric.cs" />
<Compile Remove="GenericClass.cs" />
</ItemGroup>
</Project>

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -7,47 +6,95 @@ using System.Threading.Tasks;
using AirplaneWithRadar.PaintObjects;
using AirplaneWithRadar.MovementStrategy;
namespace AirplaneWithRadar.Generics;
internal class AirplanesGenericCollection<T,U>
where T : PaintAirplane
where U : IMoveableObject
namespace AirplaneWithRadar.Generics
{
private readonly int pictWidth;
private readonly int pictHeight;
private readonly int placeSizeWidth = 210;
private readonly int placeSizeHeight = 90;
private readonly SetGeneric<T> collection;
public AirplanesGenericCollection(int picWidth, int picHeight)
internal class AirplanesGenericCollection<T, U>
where T : PaintAirplane
where U : IMoveableObject
{
int width = picWidth / placeSizeWidth;
int height = picHeight / placeSizeHeight;
pictWidth = picWidth;
pictHeight = picHeight;
collection = new SetGeneric<T>(width * height);
private readonly int pictWidth;
private readonly int pictHeight;
private readonly int placeSizeWidth = 200;
private readonly int placeSizeHeight = 70;
private readonly SetGeneric<T> collection;
}
public static bool operator +(AirplanesGenericCollection<T, U> collect, T?
obj)
{
if (obj == null)
public AirplanesGenericCollection(int picWidth, int picHeight)
{
return false;
int width = picWidth / placeSizeWidth;
int height = picHeight / placeSizeHeight;
pictWidth = picWidth;
pictHeight = picHeight;
collection = new SetGeneric<T>(width * height);
}
return collect?.collection.Insert(obj) ?? false;
}
public static T? operator -(AirplanesGenericCollection<T, U> collect, int
pos)
{
T? obj = collect.collection.Get(pos);
if (obj != null)
public static int? operator +(AirplanesGenericCollection<T, U> collect, T? obj)
{
collect.collection.Remove(pos);
if (obj == null)
{
return -1;
}
return collect?.collection.Insert(obj);
}
public static bool operator -(AirplanesGenericCollection<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;
}
public Bitmap ShowAirplanes()
{
Bitmap bmp = new(pictWidth, pictHeight);
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 < pictWidth / placeSizeWidth; i++)
{
for (int j = 0; j < pictHeight / placeSizeHeight + 1; ++j)
{
g.DrawLine(pen, i * placeSizeWidth, j * placeSizeHeight, i * placeSizeWidth + placeSizeWidth / 2, j * placeSizeHeight);
}
g.DrawLine(pen, i * placeSizeWidth, 0, i * placeSizeWidth, pictHeight / placeSizeHeight * placeSizeHeight);
}
}
private void DrawObjects(Graphics g)
{
int width = pictWidth / placeSizeWidth;
int height = pictHeight / placeSizeHeight;
int j = 3;
int k = 0;
for (int i = 0; i < collection.Count; i++)
{
PaintAirplane? airplane = collection.Get(i);
if (j < 0)
{
j += 4;
k++;
}
if (airplane != null)
{
airplane.SetPosition(placeSizeWidth * j, placeSizeHeight * k);
airplane.DrawTransport(g);
}
j--;
}
}
return obj;
}
}

View File

@ -0,0 +1,124 @@
namespace AirplaneWithRadar
{
partial class FormAirplaneCollection
{
/// <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()
{
groupBox1 = new GroupBox();
maskedTextBoxNumber = new TextBox();
ButtonRefreshCollection = new Button();
ButtonDeleteAirplane = new Button();
ButtonAddAirplane = 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(ButtonDeleteAirplane);
groupBox1.Controls.Add(ButtonAddAirplane);
groupBox1.Location = new Point(749, 9);
groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(223, 440);
groupBox1.TabIndex = 0;
groupBox1.TabStop = false;
groupBox1.Text = "Инструменты";
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(39, 148);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(161, 23);
maskedTextBoxNumber.TabIndex = 3;
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.Location = new Point(20, 229);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(194, 46);
ButtonRefreshCollection.TabIndex = 2;
ButtonRefreshCollection.Text = "Обновить коллекцию";
ButtonRefreshCollection.UseVisualStyleBackColor = true;
ButtonRefreshCollection.Click += ButtonRefreshCollection_Click;
//
// ButtonDeleteAirplane
//
ButtonDeleteAirplane.Location = new Point(20, 177);
ButtonDeleteAirplane.Name = "ButtonDeleteAirplane";
ButtonDeleteAirplane.Size = new Size(194, 46);
ButtonDeleteAirplane.TabIndex = 1;
ButtonDeleteAirplane.Text = "Удалить самолёт";
ButtonDeleteAirplane.UseVisualStyleBackColor = true;
ButtonDeleteAirplane.Click += ButtonDeleteAirplane_Click;
//
// ButtonAddAirplane
//
ButtonAddAirplane.Location = new Point(20, 22);
ButtonAddAirplane.Name = "ButtonAddAirplane";
ButtonAddAirplane.Size = new Size(194, 46);
ButtonAddAirplane.TabIndex = 0;
ButtonAddAirplane.Text = "Добавить самолёт";
ButtonAddAirplane.UseVisualStyleBackColor = true;
ButtonAddAirplane.Click += ButtonAddAirplane_Click;
//
// pictureBoxCollection
//
pictureBoxCollection.Dock = DockStyle.Fill;
pictureBoxCollection.Location = new Point(0, 0);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(984, 461);
pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false;
//
// FormAirplaneCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(984, 461);
Controls.Add(groupBox1);
Controls.Add(pictureBoxCollection);
Name = "FormAirplaneCollection";
Text = "FormAirplaneCollection";
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
ResumeLayout(false);
}
#endregion
private GroupBox groupBox1;
private Button ButtonRefreshCollection;
private Button ButtonDeleteAirplane;
private Button ButtonAddAirplane;
private PictureBox pictureBoxCollection;
private TextBox maskedTextBoxNumber;
}
}

View File

@ -0,0 +1,69 @@
using AirplaneWithRadar.Generics;
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 AirplaneWithRadar.PaintObjects;
using AirplaneWithRadar.MovementStrategy;
namespace AirplaneWithRadar
{
public partial class FormAirplaneCollection : Form
{
private readonly AirplanesGenericCollection<PaintAirplane, PaintObjectAirplane> airplanes;
public FormAirplaneCollection()
{
InitializeComponent();
airplanes = new AirplanesGenericCollection<PaintAirplane, PaintObjectAirplane>(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
private void ButtonAddAirplane_Click(object sender, EventArgs e)
{
PlaneVisual form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (airplanes + form.SelectedAirplane != -1)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = airplanes.ShowAirplanes();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void ButtonDeleteAirplane_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (airplanes - pos != false)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = airplanes.ShowAirplanes();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = airplanes.ShowAirplanes();
}
}
}

View File

@ -0,0 +1,60 @@
<root>
<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

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirplaneWithRadar
{
internal class GenericClass<T>
where T : AirplaneWithRadar.PaintObjects.PaintAirplaneWithRadar
{
}
}

View File

@ -5,6 +5,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirplaneWithRadar.Entities;
using AirplaneWithRadar.MovementStrategy;
namespace AirplaneWithRadar.PaintObjects
{
@ -23,6 +24,8 @@ namespace AirplaneWithRadar.PaintObjects
public int GetWidth => planeWidth;
public int GetHeight => planeHeight;
public IMoveableObject GetMoveableObject => new PaintObjectAirplane(this);
public bool CanMove(Movement dir)
{
if (AirplaneEntity == null)

View File

@ -37,6 +37,7 @@
StrategyComboBox = new ComboBox();
StepButton = new Button();
CreateAirplaneButton = new Button();
buttonSelectAirplane = new Button();
((System.ComponentModel.ISupportInitialize)PlanesPictureBox).BeginInit();
SuspendLayout();
//
@ -136,11 +137,22 @@
CreateAirplaneButton.UseVisualStyleBackColor = true;
CreateAirplaneButton.Click += CreateAirplaneButton_Click;
//
// buttonSelectAirplane
//
buttonSelectAirplane.Location = new Point(282, 391);
buttonSelectAirplane.Name = "buttonSelectAirplane";
buttonSelectAirplane.Size = new Size(120, 40);
buttonSelectAirplane.TabIndex = 9;
buttonSelectAirplane.Text = "Выбрать самолёт";
buttonSelectAirplane.UseVisualStyleBackColor = true;
buttonSelectAirplane.Click += buttonSelectAirplane_Click;
//
// PlaneVisual
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(buttonSelectAirplane);
Controls.Add(CreateAirplaneButton);
Controls.Add(StepButton);
Controls.Add(StrategyComboBox);
@ -167,5 +179,6 @@
private ComboBox StrategyComboBox;
private Button StepButton;
private Button CreateAirplaneButton;
private Button buttonSelectAirplane;
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Drawing;
using AirplaneWithRadar.MovementStrategy;
using AirplaneWithRadar.PaintObjects;
@ -8,7 +9,7 @@ namespace AirplaneWithRadar
{
private PaintAirplane? PaintPlanes;
private AbstractStrategy? abstractStrategy;
public PaintAirplane? SelectedAirplane { get; private set; }
private void Draw()
{
if (PaintPlanes == null)
@ -23,7 +24,8 @@ namespace AirplaneWithRadar
public PlaneVisual()
{
InitializeComponent();
abstractStrategy = null;
SelectedAirplane = null;
}
private void buttonMove_Click(object sender, EventArgs e)
@ -54,8 +56,22 @@ namespace AirplaneWithRadar
{
Random random = new Random();
PaintPlanes = new PaintAirplaneWithRadar(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)), true,
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;
}
Color dopColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
dopColor = dialog.Color;
}
PaintPlanes = new PaintAirplaneWithRadar(random.Next(100, 300), random.Next(1000, 3000), color, dopColor, true,
Convert.ToBoolean(random.Next(0, 2)), PlanesPictureBox.Width, PlanesPictureBox.Height);
PaintPlanes.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
@ -64,7 +80,14 @@ namespace AirplaneWithRadar
private void CreateAirplaneButton_Click(object sender, EventArgs e)
{
Random random = new Random();
PaintPlanes = new PaintAirplane(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
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;
}
PaintPlanes = new PaintAirplane(random.Next(100, 300), random.Next(1000, 3000), color,
PlanesPictureBox.Width, PlanesPictureBox.Height);
PaintPlanes.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
@ -103,5 +126,12 @@ namespace AirplaneWithRadar
abstractStrategy = null;
}
}
private void buttonSelectAirplane_Click(object sender, EventArgs e)
{
SelectedAirplane = PaintPlanes;
DialogResult = DialogResult.OK;
}
}
}

View File

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

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -10,44 +9,73 @@ internal class SetGeneric<T>
where T : class
{
private readonly T?[] places;
public int Count => places.Length;
public int Count => places.Length;
public SetGeneric(int count)
{
places = new T?[count];
}
public bool Insert(T airplane)
public int Insert(T airplane)
{
if (places[0] == null)
int emptyPosition = -1;
for (int i = 0; i < Count; i++)
{
places[0] = airplane;
}
else
{
for (int i = Count - 1; i < 0; i--)
if (places[i] == null)
{
places[i] = places[i - 1];
emptyPosition = i;
break;
}
places[0] = airplane;
}
return true;
if (emptyPosition < 0)
{
return -1;
}
for (int i = emptyPosition; i > 0; i--)
{
places[i] = places[i - 1];
}
places[0] = airplane;
return 0;
}
public bool Insert(T airplane, int position)
public int Insert(T airplane, int position)
{
if (position > Count || position < 0)
{
return false;
return -1;
}
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
// проверка, что после вставляемого элемента в массиве есть пустой элемент
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
// TODO вставка по позиции
if (places[position] == null)
{
places[position] = airplane;
return position;
}
int emptyPosition = -1;
for (int i = position + 1; i < Count; i++)
{
if (places[i]==null)
{
emptyPosition = i;
break;
}
}
if (emptyPosition < 0)
{
return -1;
}
for (int i = emptyPosition; i > position; i--)
{
places[i] = places[i - 1];
}
places[position] = airplane;
return true;
return position;
}
public bool Remove(int position)
{
if (position > Count || position < 0)
if ((position >= Count && position < 0) || places[position] == null)
{
return false;
}
@ -56,7 +84,7 @@ internal class SetGeneric<T>
}
public T? Get(int position)
{
if (position > Count || position < 0)
if (position >= Count && position < 0)
{
return null;
}