PIbd-21 Yakovlev M.G. lab work 03 #3

Closed
YakovlevMaxim wants to merge 3 commits from Lab3SailBoat into Lab2SailBoat
10 changed files with 538 additions and 9 deletions
Showing only changes of commit bad7f562ec - Show all commits

View File

@ -1,4 +1,5 @@
using SailBoat.Entities;
using SailBoat.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Configuration;
@ -106,5 +107,7 @@ namespace SailBoat.DrawningObjects
g.FillEllipse(BodyBrush, _startPosX + 15, _startPosY + 65, 95, 50);
g.DrawEllipse(pen, _startPosX + 15, _startPosY + 65, 95, 50);
}
public IMoveableObject GetMoveableObjects => new DrawningObjectBoat(this);
}
}

View File

@ -21,6 +21,7 @@ namespace SailBoat.DrawningObjects
{
if (EntityBoat is not EntitySailBoat sailBoat) { return; }
Pen pen = new(Color.Black);
Brush bodyColor = new SolidBrush(sailBoat.BodyColor);
Brush additionalBrush = new SolidBrush(sailBoat.AdditionalColor);
g.DrawLine(pen, _startPosX + 10, _startPosY + 60, _startPosX + 110, _startPosY + 60);
@ -28,19 +29,18 @@ namespace SailBoat.DrawningObjects
g.DrawLine(pen, _startPosX + 180, _startPosY + 90, _startPosX + 110, _startPosY + 120);
g.DrawLine(pen, _startPosX + 110, _startPosY + 120, _startPosX + 10, _startPosY + 120);
g.DrawLine(pen, _startPosX + 10, _startPosY + 120, _startPosX + 10, _startPosY + 60);
g.FillEllipse(additionalBrush, _startPosX + 15, _startPosY + 65, 95, 50);
g.FillEllipse(bodyColor, _startPosX + 15, _startPosY + 65, 95, 50);
g.DrawEllipse(pen, _startPosX + 15, _startPosY + 65, 95, 50);
base.DrawTransport(g);
if (sailBoat.RainforcedBody)
{
Brush rainforcedBody = new SolidBrush(Color.DarkGray);
g.FillRectangle(rainforcedBody, _startPosX + 2, _startPosY + 65, 10, 50);
g.FillRectangle(additionalBrush, _startPosX + 2, _startPosY + 65, 10, 50);
g.DrawRectangle(pen, _startPosX + 2, _startPosY + 65, 10, 50);
g.FillRectangle(rainforcedBody, _startPosX + 15, _startPosY + 53, 90, 10);
g.FillRectangle(additionalBrush, _startPosX + 15, _startPosY + 53, 90, 10);
g.DrawRectangle(pen, _startPosX + 15, _startPosY + 53, 90, 10);
g.FillRectangle(rainforcedBody, _startPosX + 15, _startPosY + 118, 90, 10);
g.FillRectangle(additionalBrush, _startPosX + 15, _startPosY + 118, 90, 10);
g.DrawRectangle(pen, _startPosX + 15, _startPosY + 118, 90, 10);
}

View File

@ -0,0 +1,126 @@
namespace SailBoat
{
partial class FormBoatCollection
{
/// <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()
{
tools = new GroupBox();
maskedTextBoxNumber = new MaskedTextBox();
buttonRefreshCollection = new Button();
buttonRemoveBoat = new Button();
buttonAddBoat = new Button();
pictureBoxCollection = new PictureBox();
tools.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout();
//
// tools
//
tools.Controls.Add(maskedTextBoxNumber);
tools.Controls.Add(buttonRefreshCollection);
tools.Controls.Add(buttonRemoveBoat);
tools.Controls.Add(buttonAddBoat);
tools.Dock = DockStyle.Right;
tools.Location = new Point(716, 0);
tools.Name = "tools";
tools.Size = new Size(200, 608);
tools.TabIndex = 0;
tools.TabStop = false;
tools.Text = "Инструменты";
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(45, 104);
maskedTextBoxNumber.Mask = "00000";
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(112, 23);
maskedTextBoxNumber.TabIndex = 4;
maskedTextBoxNumber.ValidatingType = typeof(int);
//
// buttonRefreshCollection
//
buttonRefreshCollection.Location = new Point(6, 211);
buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(188, 39);
buttonRefreshCollection.TabIndex = 3;
buttonRefreshCollection.Text = "Обновить коллекцию";
buttonRefreshCollection.UseVisualStyleBackColor = true;
buttonRefreshCollection.Click += buttonRefreshCollection_Click;
//
// buttonRemoveBoat
//
buttonRemoveBoat.Location = new Point(6, 133);
buttonRemoveBoat.Name = "buttonRemoveBoat";
buttonRemoveBoat.Size = new Size(188, 39);
buttonRemoveBoat.TabIndex = 1;
buttonRemoveBoat.Text = "Удалить лодку";
buttonRemoveBoat.UseVisualStyleBackColor = true;
buttonRemoveBoat.Click += buttonRemoveBoat_Click;
//
// buttonAddBoat
//
buttonAddBoat.Location = new Point(6, 22);
buttonAddBoat.Name = "buttonAddBoat";
buttonAddBoat.Size = new Size(188, 39);
buttonAddBoat.TabIndex = 0;
buttonAddBoat.Text = "Добавить лодку";
buttonAddBoat.UseVisualStyleBackColor = true;
buttonAddBoat.Click += buttonAddBoat_Click;
//
// pictureBoxCollection
//
pictureBoxCollection.Location = new Point(0, 0);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(710, 608);
pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false;
//
// FormBoatCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(916, 608);
Controls.Add(pictureBoxCollection);
Controls.Add(tools);
Name = "FormBoatCollection";
Text = "Набор лодок";
tools.ResumeLayout(false);
tools.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
ResumeLayout(false);
}
#endregion
private GroupBox tools;
private Button buttonAddBoat;
private Button buttonRemoveBoat;
private Button buttonRefreshCollection;
private PictureBox pictureBoxCollection;
private MaskedTextBox maskedTextBoxNumber;
}
}

View File

@ -0,0 +1,67 @@
using SailBoat.DrawningObjects;
using SailBoat.MovementStrategy;
using SailBoat.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;
namespace SailBoat
{
public partial class FormBoatCollection : Form
{
private readonly BoatsGenericCollection<DrawningBoat, DrawningObjectBoat> _boats;
public FormBoatCollection()
{
InitializeComponent();
_boats = new BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
private void buttonAddBoat_Click(object sender, EventArgs e)
{
FormSailBoat form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if ((_boats + form.SelectedBoat) == 0)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _boats.ShowBoats();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void buttonRemoveBoat_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
if (maskedTextBoxNumber.Text == "") { return; }
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (_boats - pos)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _boats.ShowBoats();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void buttonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _boats.ShowBoats();
}
}
}

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 @@
comboBoxStrategy = new ComboBox();
buttonStep = new Button();
buttonCreateBoat = new Button();
buttonSelectBoat = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxSailBoat).BeginInit();
SuspendLayout();
//
@ -139,11 +140,22 @@
buttonCreateBoat.UseVisualStyleBackColor = true;
buttonCreateBoat.Click += buttonCreateBoat_Click;
//
// buttonSelectBoat
//
buttonSelectBoat.Location = new Point(195, 407);
buttonSelectBoat.Name = "buttonSelectBoat";
buttonSelectBoat.Size = new Size(106, 42);
buttonSelectBoat.TabIndex = 14;
buttonSelectBoat.Text = "Выбрать";
buttonSelectBoat.UseVisualStyleBackColor = true;
buttonSelectBoat.Click += buttonSelectBoat_Click;
//
// FormSailBoat
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(884, 461);
Controls.Add(buttonSelectBoat);
Controls.Add(buttonCreateBoat);
Controls.Add(buttonStep);
Controls.Add(comboBoxStrategy);
@ -172,5 +184,6 @@
private ComboBox comboBoxStrategy;
private Button buttonStep;
private Button buttonCreateBoat;
private Button buttonSelectBoat;
}
}

View File

@ -1,5 +1,6 @@
using SailBoat.DrawningObjects;
using SailBoat.MovementStrategy;
using System.Drawing;
namespace SailBoat
{
@ -8,12 +9,18 @@ namespace SailBoat
private DrawningBoat? _drawningBoat;
private AbstractStrategy? _abstractStrategy;
public DrawningBoat? SelectedBoat { get; private set; }
public FormSailBoat()
{
InitializeComponent();
_abstractStrategy = null;
SelectedBoat = null;
}
private void Draw()
{
if (_drawningBoat == null) return;
Bitmap bmp = new(pictureBoxSailBoat.Width, pictureBoxSailBoat.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningBoat.DrawTransport(gr);
@ -24,15 +31,33 @@ namespace SailBoat
private void buttonCreateSailBoat_Click(object sender, EventArgs e)
{
Random random = new();
_drawningBoat = new DrawningSailBoat(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)), pictureBoxSailBoat.Width, pictureBoxSailBoat.Height);
_drawningBoat.SetPosition(random.Next(10, 100), random.Next(10,100));
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));
if (dialog.ShowDialog() == DialogResult.OK)
{
dopcolor = dialog.Color;
}
_drawningBoat = new DrawningSailBoat(random.Next(100, 300), random.Next(1000, 3000), color, dopcolor, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxSailBoat.Width, pictureBoxSailBoat.Height);
_drawningBoat.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonCreateBoat_Click(object sender, EventArgs e)
{
Random random = new();
_drawningBoat = new DrawningBoat(random.Next(300, 700), random.Next(2000, 4000), Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)), pictureBoxSailBoat.Width, pictureBoxSailBoat.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;
}
_drawningBoat = new DrawningBoat(random.Next(300, 700), random.Next(2000, 4000), color, pictureBoxSailBoat.Width, pictureBoxSailBoat.Height);
_drawningBoat.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
@ -76,6 +101,7 @@ namespace SailBoat
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null) return;
comboBoxStrategy.Enabled = false;
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
@ -84,5 +110,11 @@ namespace SailBoat
_abstractStrategy = null;
}
}
private void buttonSelectBoat_Click(object sender, EventArgs e)
{
SelectedBoat = _drawningBoat;
DialogResult = DialogResult.OK;
}
}
}

View File

@ -0,0 +1,95 @@
using SailBoat.DrawningObjects;
using SailBoat.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SailBoat.Generics
{
internal class BoatsGenericCollection<T,U>
where T : DrawningBoat
where U : IMoveableObject
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 220;
private readonly int _placeSizeHeight = 150;
private readonly SetGeneric<T> _collection;
public BoatsGenericCollection(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 +(BoatsGenericCollection<T,U> collect, T? obj)
{
if (obj == null) return -1;
return collect?._collection.Insert(obj);
}
public static bool operator -(BoatsGenericCollection<T,U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
if(obj == null)
{
return false;
}
collect._collection.Remove(pos);
return true;
}
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObjects;
}
public Bitmap ShowBoats()
{
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)
{
int height = _pictureHeight / _placeSizeHeight;
int width = _pictureWidth / _placeSizeWidth;
for (int i = 0; i < _collection.Count ; i++)
{
DrawningBoat boat = _collection.Get(i);
if(boat == null)
{
continue;
}
boat.SetPosition((i % width) * _placeSizeWidth, (i / (height - 1)) * _placeSizeHeight);
boat.DrawTransport(g);
}
}
}
}

View File

@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SailBoat.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 car)
{
int index = 0;
Review

Правильнее было вызвать Insert(T obj, 0);

Правильнее было вызвать Insert(T obj, 0);
while (_places[index] != null) {
index++;
if(index == Count) { return -1; }
}
while (index != 0)
{
_places[index] = _places[index - 1];
index--;
}
_places[0] = car;
return 0;
}
public bool Insert(T car, int position)
{
if (position < 0 || position >= Count)
{
return false;
}
if (_places[position] == null)
{
_places[position] = car;
return true;
}
int index = position;
while (_places[index] != null) index++;
if (index == Count) return false;
for(int i = index; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = car;
return true;
}
public bool Remove(int position)
{
if (position < 0 || position >= Count) return false;
_places[position] = null;
return true;
}
public T? Get(int position)
{
if (position < 0 || position >= Count) return null;
return _places[position];
}
}
}

View File

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