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

Closed
fedorenko wants to merge 3 commits from lab3 into lab2
9 changed files with 540 additions and 5 deletions

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hydroplane.Entities;
using Hydroplane.MovementStrategy;
namespace Hydroplane.DrawningObjects
{
@ -45,6 +46,9 @@ namespace Hydroplane.DrawningObjects
}
EntityPlane = new EntityPlane(speed, weight, bodyColor);
}
public IMoveableObject GetMoveableObject => new DrawningObjectPlane(this);
public void SetPosition(int x, int y)
{
_startPosX = Math.Min(x, _pictureWidth - _planeWidth);

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

@ -0,0 +1,142 @@
namespace Hydroplane
{
partial class FormHydroplaneCollection
{
/// <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();
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(577, 0);
panel1.Margin = new Padding(3, 2, 3, 2);
panel1.Name = "panel1";
panel1.Size = new Size(219, 338);
panel1.TabIndex = 0;
//
// UpdateButton
//
UpdateButton.Location = new Point(9, 181);
UpdateButton.Margin = new Padding(3, 2, 3, 2);
UpdateButton.Name = "UpdateButton";
UpdateButton.Size = new Size(200, 28);
UpdateButton.TabIndex = 4;
UpdateButton.Text = "Обновить коллекцию";
UpdateButton.UseVisualStyleBackColor = true;
UpdateButton.Click += ButtonRefreshCollection_Click;
//
// DeleteButton
//
DeleteButton.Location = new Point(9, 132);
DeleteButton.Margin = new Padding(3, 2, 3, 2);
DeleteButton.Name = "DeleteButton";
DeleteButton.Size = new Size(200, 28);
DeleteButton.TabIndex = 3;
DeleteButton.Text = "Удалить самолёт";
DeleteButton.UseVisualStyleBackColor = true;
DeleteButton.Click += ButtonRemoveCar_Click;
//
// AddButton
//
AddButton.Location = new Point(9, 40);
AddButton.Margin = new Padding(3, 2, 3, 2);
AddButton.Name = "AddButton";
AddButton.Size = new Size(200, 28);
AddButton.TabIndex = 2;
AddButton.Text = "Добавить самолёт";
AddButton.UseVisualStyleBackColor = true;
AddButton.Click += ButtonAddTank_Click;
//
// InputNum
//
InputNum.Location = new Point(9, 100);
InputNum.Margin = new Padding(3, 2, 3, 2);
InputNum.Name = "InputNum";
InputNum.Size = new Size(200, 23);
InputNum.TabIndex = 1;
//
// label1
//
label1.AutoSize = true;
label1.Location = new Point(2, 2);
label1.Name = "label1";
label1.Size = new Size(83, 15);
label1.TabIndex = 0;
label1.Text = "Инструменты";
//
// DrawPlane
//
DrawPlane.Dock = DockStyle.Fill;
DrawPlane.Location = new Point(0, 0);
DrawPlane.Margin = new Padding(3, 2, 3, 2);
DrawPlane.Name = "DrawPlane";
DrawPlane.Size = new Size(577, 338);
DrawPlane.TabIndex = 1;
DrawPlane.TabStop = false;
//
// FormHydroplaneCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(796, 338);
Controls.Add(DrawPlane);
Controls.Add(panel1);
Margin = new Padding(3, 2, 3, 2);
Name = "FormHydroplaneCollection";
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

@ -0,0 +1,72 @@
using Hydroplane.DrawningObjects;
using Hydroplane.Generics;
using Hydroplane.MovementStrategy;
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;
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

@ -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

@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hydroplane.DrawningObjects;
using Hydroplane.MovementStrategy;
namespace Hydroplane.Generics
{
internal class PlanesGenericCollection<T, U>
where T : DrawningPlane
where U : IMoveableObject
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 175;
private readonly int _placeSizeHeight = 85;
private readonly SetGeneric<T> _collection;
public PlanesGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public static int? operator +(PlanesGenericCollection<T, U> collect, T?
obj)
{
if (obj == null)
{
return -1;
}
return collect?._collection.Insert(obj);
}
public static bool operator -(PlanesGenericCollection<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 ShowPlanes()
{
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)
{
for (int i = 0; i < _collection.Count; i++)
{
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());
}
}
}

65
Hydroplane/SetGeneric.cs Normal file
View File

@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hydroplane.Generics
{
internal class SetGeneric<T> where T : class
{
private readonly T?[] _places;
public int Count => _places.Length;
public SetGeneric(int count)
{
_places = new T?[count];
}
public int Insert(T plane)
{
if (_places[Count - 1] != null)
return -1;
return Insert(plane, 0);
}
public int Insert(T plane, int position)
{
if (position < 0 || position >= Count)
return -1;
if (_places[position] != null)
{
int indexEnd = position + 1;
while (_places[indexEnd] != null)
{
indexEnd++;
}
for (int i = indexEnd + 1; i > position; i--)
{
_places[i] = _places[i - 1];
}
}
_places[position] = plane;
return position;
}
public bool Remove(int position)
{
if (position < 0 || position >= _places.Count())
return false;
_places[position] = null;
return true;
}
public T? Get(int position)
{
if (position < 0 || position >= _places.Count())
return null;
return _places[position];
}
}
}