PIbd-22. Fedorenko G.Y. Lab Work 3 #4

Closed
fedorenko wants to merge 3 commits from lab3 into lab2
9 changed files with 278 additions and 160 deletions
Showing only changes of commit 75e016e629 - Show all commits

View File

@ -47,9 +47,6 @@ namespace Hydroplane.DrawningObjects
EntityPlane = new EntityPlane(speed, weight, bodyColor);
}
/// <summary>
/// Получение объекта IMoveableObject из объекта DrawningCar
/// </summary>
public IMoveableObject GetMoveableObject => new DrawningObjectPlane(this);
public void SetPosition(int x, int y)

View File

@ -37,6 +37,7 @@
buttonCreateHydroplane = new Button();
comboBoxStrategy = new ComboBox();
buttonStep = new Button();
buttonChoose = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxHydroplane).BeginInit();
SuspendLayout();
//
@ -139,11 +140,22 @@
buttonStep.UseVisualStyleBackColor = true;
buttonStep.Click += buttonStep_Click;
//
// buttonChoose
//
buttonChoose.Location = new Point(309, 398);
buttonChoose.Name = "buttonChoose";
buttonChoose.Size = new Size(125, 35);
buttonChoose.TabIndex = 9;
buttonChoose.Text = "Choose";
buttonChoose.UseVisualStyleBackColor = true;
buttonChoose.Click += ButtonSelectPLane_Click;
//
// FormHydroplane
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(884, 461);
Controls.Add(buttonChoose);
Controls.Add(buttonStep);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonCreateHydroplane);
@ -172,5 +184,6 @@
private Button buttonCreateHydroplane;
private ComboBox comboBoxStrategy;
private Button buttonStep;
private Button buttonChoose;
}
}

View File

@ -17,9 +17,13 @@ namespace Hydroplane
{
private DrawningPlane? _drawingPlane;
private AbstractStrategy? _abstractStrategy;
public DrawningPlane? SelectedPlane { get; private set; }
public FormHydroplane()
{
InitializeComponent();
_abstractStrategy = null;
SelectedPlane = null;
}
private void Draw()
{
@ -36,7 +40,13 @@ namespace Hydroplane
private void buttonCreatePlane_Click(object sender, EventArgs e)
{
Random random = new();
_drawingPlane = new DrawningPlane(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBoxHydroplane.Width, pictureBoxHydroplane.Height);
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;
}
_drawingPlane = new DrawningPlane(random.Next(100, 300), random.Next(1000, 3000), color, pictureBoxHydroplane.Width, pictureBoxHydroplane.Height);
_drawingPlane.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
@ -44,7 +54,18 @@ namespace Hydroplane
private void buttonCreateHydroplane_Click(object sender, EventArgs e)
{
Random random = new();
_drawingPlane = new DrawningHydroplane(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 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;
}
_drawingPlane = new DrawningHydroplane(random.Next(100, 300), random.Next(1000, 3000), mainColor, addColor,
Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)), pictureBoxHydroplane.Width, pictureBoxHydroplane.Height);
_drawingPlane.SetPosition(random.Next(10, 100), random.Next(10, 100));
@ -94,8 +115,7 @@ namespace Hydroplane
{
return;
}
_abstractStrategy.SetData(new DrawningObjectPlane(_drawingPlane), pictureBoxHydroplane.Width,
pictureBoxHydroplane.Height);
_abstractStrategy.SetData(_drawingPlane.GetMoveableObject, pictureBoxHydroplane.Width, pictureBoxHydroplane.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
@ -111,5 +131,11 @@ namespace Hydroplane
}
}
private void ButtonSelectPLane_Click(object sender, EventArgs e)
{
SelectedPlane = _drawingPlane;
DialogResult = DialogResult.OK;
}
}
}

View File

@ -28,12 +28,108 @@
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Text = "FormHydroplaneCollection";
panel1 = new Panel();
UpdateButton = new Button();
DeleteButton = new Button();
AddButton = new Button();
InputNum = new TextBox();
label1 = new Label();
DrawPlane = new PictureBox();
panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)DrawPlane).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 = "Инструменты";
//
// DrawPlane
//
DrawPlane.Dock = DockStyle.Fill;
DrawPlane.Location = new Point(0, 0);
DrawPlane.Name = "DrawPlane";
DrawPlane.Size = new Size(550, 450);
DrawPlane.TabIndex = 1;
DrawPlane.TabStop = false;
//
// CollectionsFrame
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(DrawPlane);
Controls.Add(panel1);
Name = "CollectionsFrame";
Text = "Form1";
panel1.ResumeLayout(false);
panel1.PerformLayout();
((System.ComponentModel.ISupportInitialize)DrawPlane).EndInit();
ResumeLayout(false);
}
#endregion
private Panel panel1;
private Button UpdateButton;
private Button DeleteButton;
private Button AddButton;
private TextBox InputNum;
private Label label1;
private PictureBox DrawPlane;
}
}

View File

@ -1,4 +1,7 @@
using System;
using Hydroplane.DrawningObjects;
using Hydroplane.Generics;
using Hydroplane.MovementStrategy;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
@ -12,9 +15,58 @@ namespace Hydroplane
{
public partial class FormHydroplaneCollection : Form
{
private readonly PlanesGenericCollection<DrawningPlane, DrawningObjectPlane> _planes;
public FormHydroplaneCollection()
{
InitializeComponent();
_planes = new PlanesGenericCollection<DrawningPlane, DrawningObjectPlane>(DrawPlane.Width, DrawPlane.Height);
}
private void ButtonAddTank_Click(object sender, EventArgs e)
{
FormHydroplane form = new FormHydroplane();
if (form.ShowDialog() == DialogResult.OK)
{
if (_planes + form.SelectedPlane != -1)
{
MessageBox.Show("Объект добавлен");
DrawPlane.Image = _planes.ShowPlanes();
}
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 (_planes - pos)
{
MessageBox.Show("Объект удален");
DrawPlane.Image = _planes.ShowPlanes();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
DrawPlane.Image = _planes.ShowPlanes();
}
}
}

View File

@ -1,17 +1,17 @@
<?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
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>
@ -26,36 +26,36 @@
<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
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
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
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
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
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
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
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->

View File

@ -8,40 +8,16 @@ using Hydroplane.MovementStrategy;
namespace Hydroplane.Generics
{
/// <summary>
/// Параметризованный класс для набора объектов DrawningPlane
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
internal class PlanesGenericCollection<T, U>
where T : DrawningPlane
where U : IMoveableObject
{
/// <summary>
/// Ширина окна прорисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна прорисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Размер занимаемого объектом места (ширина)
/// </summary>
private readonly int _placeSizeWidth = 175;
/// <summary>
/// Размер занимаемого объектом места (высота)
/// </summary>
private readonly int _placeSizeHeight = 80;
/// <summary>
/// Набор объектов
/// </summary>
private readonly SetGeneric<T> _collection;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="picWidth"></param>
/// <param name="picHeight"></param>
public PlanesGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
@ -50,51 +26,33 @@ namespace Hydroplane.Generics
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
/// <summary>
/// Перегрузка оператора сложения
/// </summary>
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static bool operator +(PlanesGenericCollection<T, U> collect, T?
public static int? operator +(PlanesGenericCollection<T, U> collect, T?
obj)
{
if (obj == null)
{
return false;
return -1;
}
return collect?._collection.Insert(obj) ?? false;
return collect?._collection.Insert(obj);
}
/// <summary>
/// Перегрузка оператора вычитания
/// </summary>
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static T? operator -(PlanesGenericCollection<T, U> collect, int
public static bool operator -(PlanesGenericCollection<T, U> collect, int
pos)
{
T? obj = collect._collection.Get(pos);
if (obj != null)
if (obj == null)
{
collect._collection.Remove(pos);
return false;
}
return obj;
return collect._collection.Remove(pos);
}
/// <summary>
/// Получение объекта IMoveableObject
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
/// </summary>
/// <returns></returns>
public Bitmap ShowCars()
public Bitmap ShowPlanes()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
@ -102,10 +60,6 @@ namespace Hydroplane.Generics
DrawObjects(gr);
return bmp;
}
/// <summary>
/// Метод отрисовки фона
/// </summary>
/// <param name="g"></param>
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.Black, 3);
@ -122,17 +76,17 @@ namespace Hydroplane.Generics
_placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
}
}
/// <summary>
/// Метод прорисовки объектов
/// </summary>
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
for (int i = 0; i < _collection.Count; i++)
{
// TODO получение объекта
// TODO установка позиции
// TODO прорисовка объекта
T t = _collection.Get(i);
if (t != null)
{
t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
if (t is DrawningPlane) (t as DrawningPlane).DrawTransport(g);
else t.DrawTransport(g);
}
}
}
}

View File

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

View File

@ -6,82 +6,66 @@ using System.Threading.Tasks;
namespace Hydroplane.Generics
{
/// <summary>
/// Параметризованный набор объектов
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetGeneric<T>
where T : class
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="plane">Добавляемый самолёт</param>
/// <returns></returns>
public bool Insert(T plane)
public int Insert(T plane)
{
try
int index = -1;
for (int i = 0; i < _places.Length; i++)
{
for (int i = _places.Length - 1; i > 0; i--)
if (_places[i] == null)
{
_places[i] = _places[i - 1];
index = i; break;
}
_places[0] = plane;
return true;
}
catch
if (index < 0)
{
return false;
return -1;
}
for (int i = index; i > 0; i--)
{
_places[i] = _places[i - 1];
}
_places[0] = plane;
return 0;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="plane">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public bool Insert(T plane, int position)
public int Insert(T plane, int position)
{
if (position < 0 || position >= _places.Count() || plane == null)
{
return false;
}
if (position < 0 || position >= Count)
return -1;
if (_places[position] == null)
{
return false;
_places[position] = plane;
return position;
}
int positionNull = Array.FindIndex(_places, position, x => x == null);
if (positionNull == -1)
return false;
for (int i = positionNull; i > position; i--)
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] = plane;
return true;
return position;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position)
{
if (position < 0 || position >= _places.Count())
@ -91,11 +75,7 @@ namespace Hydroplane.Generics
return true;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? Get(int position)
{
if (position < 0 || position >= _places.Count())