Продвинутый объект

This commit is contained in:
Павел Путилин 2022-10-04 21:03:34 +04:00
parent 65eddefb53
commit 648707cd84
6 changed files with 169 additions and 19 deletions

View File

@ -9,15 +9,15 @@ namespace AirplaneWithRadar
/// <summary>
/// Класс-сущность
/// </summary>
public EntityAirplane Airplane { private set; get; }
public EntityAirplane Airplane { protected set; get; }
/// <summary>
/// Левая координата отрисовки самолёта
/// </summary>
private float _startPosX;
protected float _startPosX;
/// <summary>
/// Верхняя кооридната отрисовки самолёта
/// </summary>
private float _startPosY;
protected float _startPosY;
/// <summary>
/// Ширина окна отрисовки
/// </summary>
@ -40,11 +40,27 @@ namespace AirplaneWithRadar
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Цвет</param>
public void Init(int speed, float weight, Color bodyColor)
public DrawningAirplane(int speed, float weight, Color bodyColor)
{
Airplane = new EntityAirplane();
Airplane.Init(speed, weight, bodyColor);
Airplane = new EntityAirplane(speed, weight, bodyColor);
}
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес самолёта</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="airplaneWidth">Ширина отрисовки самолёта</param>
/// <param name="airplaneHeight">Высота отрисовки самолёта</param>
protected DrawningAirplane(int speed, float weight, Color bodyColor, int
airplaneWidth, int airplaneHeight) :
this(speed, weight, bodyColor)
{
_airplaneWidth = airplaneWidth;
_airplaneHeight = airplaneHeight;
}
/// <summary>
/// Установка позиции
/// </summary>
@ -52,6 +68,7 @@ namespace AirplaneWithRadar
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public void SetPosition(int x, int y, int width, int height)
{
_startPosX = x;
@ -111,7 +128,7 @@ namespace AirplaneWithRadar
/// Отрисовка самолёта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue)

View File

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace AirplaneWithRadar
{
internal class DrawningAirplaneWithRadar : DrawningAirplane
{
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="dopColor">Дополнительный цвет</param>
/// <param name="radar">Признак наличия радара</param>
/// <param name="fuelTanks">Признак наличия дополнительных топливных баков</param>
public DrawningAirplaneWithRadar(int speed, float weight, Color bodyColor, Color
dopColor, bool radar, bool fuelTanks) :
base(speed, weight, bodyColor, 100, 10)
{
Airplane = new EntityAirplaneWithRadar(speed, weight, bodyColor, dopColor, radar, fuelTanks);
}
public override void DrawTransport(Graphics g)
{
if (Airplane is not EntityAirplaneWithRadar airplaneWithRadar)
{
return;
}
Pen pen = new(Color.Black);
Brush dopBrush = new SolidBrush(airplaneWithRadar.DopColor);
if (airplaneWithRadar.Radar)
{
g.FillEllipse(dopBrush, _startPosX + 40, _startPosY, 20, 5);
g.DrawLine(pen, _startPosX + 43, _startPosY + 4, _startPosX + 43, _startPosY + 7);
g.DrawLine(pen, _startPosX + 57, _startPosY + 4, _startPosX + 57, _startPosY + 7);
}
_startPosX += 10;
_startPosY += 5;
base.DrawTransport(g);
_startPosX -= 10;
_startPosY -= 5;
if (airplaneWithRadar.FuelTanks)
{
g.FillEllipse(dopBrush, _startPosX + 35, _startPosY + 13, 30, 5);
}
}
}
}

View File

@ -29,7 +29,7 @@ namespace AirplaneWithRadar
/// <param name="weight"></param>
/// <param name="bodyColor"></param>
/// <returns></returns>
public void Init(int speed, float weight, Color bodyColor)
public EntityAirplane(int speed, float weight, Color bodyColor)
{
Random rnd = new();
Speed = speed <= 0 ? rnd.Next(50, 150) : speed;

View File

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirplaneWithRadar
{
internal class EntityAirplaneWithRadar : EntityAirplane
{
/// <summary>
/// Дополнительный цвет
/// </summary>
public Color DopColor { get; private set; }
/// <summary>
/// Признак наличия радара
/// </summary>
public bool Radar { get; private set; }
/// <summary>
/// Признак наличия дополнительного топливного бака
/// </summary>
public bool FuelTanks { get; private set; }
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес самолёта</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="dopColor">Дополнительный цвет</param>
/// <param name="radar">Признак наличия радара</param>
/// <param name="fuelTanks">Признак наличия дополнительных топливных баков</param>
public EntityAirplaneWithRadar(int speed, float weight, Color bodyColor, Color
dopColor, bool radar, bool fuelTanks) :
base(speed, weight, bodyColor)
{
DopColor = dopColor;
Radar = radar;
FuelTanks = fuelTanks;
}
}
}

View File

@ -38,6 +38,7 @@
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonCreateModif = new System.Windows.Forms.Button();
this.statusStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxAirplane)).BeginInit();
this.SuspendLayout();
@ -89,7 +90,7 @@
this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreate.Location = new System.Drawing.Point(12, 395);
this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(75, 23);
this.buttonCreate.Size = new System.Drawing.Size(96, 23);
this.buttonCreate.TabIndex = 2;
this.buttonCreate.Text = "Создать";
this.buttonCreate.UseVisualStyleBackColor = true;
@ -143,11 +144,22 @@
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonCreateModif
//
this.buttonCreateModif.Location = new System.Drawing.Point(12, 366);
this.buttonCreateModif.Name = "buttonCreateModif";
this.buttonCreateModif.Size = new System.Drawing.Size(96, 23);
this.buttonCreateModif.TabIndex = 7;
this.buttonCreateModif.Text = "Модификация";
this.buttonCreateModif.UseVisualStyleBackColor = true;
this.buttonCreateModif.Click += new System.EventHandler(this.buttonCreateModif_Click);
//
// FormAirplane
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.buttonCreateModif);
this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonLeft);
@ -177,5 +189,6 @@
private Button buttonLeft;
private Button buttonDown;
private Button buttonRight;
private Button buttonCreateModif;
}
}

View File

@ -9,14 +9,15 @@ namespace AirplaneWithRadar
InitializeComponent();
}
/// <summary>
/// Ìåòîä ïðîðèñîâêè ñàìîë¸òà
/// </summary>
private void pictureBoxAirplane_Click(object sender, EventArgs e)
{
}
/// <summary>
/// Ìåòîä ïðîðèñîâêè ñàìîë¸òà
/// </summary>
private void Draw()
{
Bitmap bmp = new(pictureBoxAirplane.Width, pictureBoxAirplane.Height);
@ -24,22 +25,30 @@ namespace AirplaneWithRadar
_airplane?.DrawTransport(gr);
pictureBoxAirplane.Image = bmp;
}
/// <summary>
/// Ìåòîä óñòàíîâêè äàííûõ
/// </summary>
private void SetData()
{
Random rnd = new();
_airplane.SetPosition(rnd.Next(20, 100), rnd.Next(20, 100), pictureBoxAirplane.Width, pictureBoxAirplane.Height);
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {_airplane.Airplane.Speed}";
toolStripStatusLabelWeight.Text = $"Âåñ: {_airplane.Airplane.Weight}";
toolStripStatusLabelBodyColor.Text = $"Öâåò: {_airplane.Airplane.BodyColor.Name}";
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
///
private void buttonCreate_Click(object sender, EventArgs e)
{
Random rnd = new();
_airplane = new DrawningAirplane();
_airplane.Init(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
_airplane.SetPosition(rnd.Next(20, 100), rnd.Next(20, 100), pictureBoxAirplane.Width, pictureBoxAirplane.Height);
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {_airplane.Airplane.Speed}";
toolStripStatusLabelWeight.Text = $"Âåñ: {_airplane.Airplane.Weight}";
toolStripStatusLabelBodyColor.Text = $"Öâåò: {_airplane.Airplane.BodyColor.Name}";
_airplane = new DrawningAirplane(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
SetData();
Draw();
}
@ -79,5 +88,22 @@ namespace AirplaneWithRadar
_airplane?.ChangeBorders(pictureBoxAirplane.Width, pictureBoxAirplane.Height);
Draw();
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ìîäèôèêàöèÿ"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreateModif_Click(object sender, EventArgs e)
{
Random rnd = new();
_airplane = new DrawningAirplaneWithRadar(rnd.Next(100, 300), rnd.Next(1000, 2000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Convert.ToBoolean(rnd.Next(0, 2)),
Convert.ToBoolean(rnd.Next(0, 2)));
SetData();
Draw();
}
}
}