Добавление родителей и ввод конструкторов

This commit is contained in:
mar-va 2024-03-18 12:34:40 +04:00
parent 7f07cfaa24
commit 9c1f576994
8 changed files with 377 additions and 287 deletions

View File

@ -1,206 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAccordionBus;
public class DrawningAccordionBus
{
public EntityAccordionBus? EntityAccordionBus { get; private set; }
private int? _pictureWidth;
private int? _pictureHeight;
private int? _startPosX;
private int? _startPosY;
private int _drawningAccordionBusWidth = 60;
private readonly int _drawningAccordionBusHeight = 50;
public void Init(EntityAccordionBus entity)
{
EntityAccordionBus = entity;
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
public bool SetPictureSize(int width, int height)
{
if (_drawningAccordionBusWidth <= width && _drawningAccordionBusHeight <= height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX.HasValue && _startPosY.HasValue)
{
if (_startPosX + _drawningAccordionBusWidth > width)
{
_startPosX = width - _drawningAccordionBusWidth;
}
if (_startPosY + _drawningAccordionBusHeight > height)
{
_startPosY = height - _drawningAccordionBusHeight;
}
}
return true;
}
return false;
}
public void SetPosition(int x, int y)
{
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
_startPosX = x;
_startPosY = y;
if (x < 0)
{
_startPosX = 0;
}
if (y < 0)
{
_startPosY = 0;
}
if (x + _drawningAccordionBusWidth > _pictureWidth) {
_startPosX = _pictureWidth - _drawningAccordionBusWidth;
}
if (y + _drawningAccordionBusHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _drawningAccordionBusHeight;
}
}
public bool MoveTransport(DirectionType direction)
{
if (EntityAccordionBus == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return false;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityAccordionBus.Step > 0)
{
_startPosX -= (int)EntityAccordionBus.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntityAccordionBus.Step > 0)
{
_startPosY -= (int)EntityAccordionBus.Step;
}
return true;
// вправо
case DirectionType.Right:
if (_startPosX.Value + _drawningAccordionBusWidth + EntityAccordionBus.Step < _pictureWidth.Value)
{
_startPosX += (int)EntityAccordionBus.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + _drawningAccordionBusHeight + EntityAccordionBus.Step < _pictureHeight.Value)
{
_startPosY += (int)EntityAccordionBus.Step;
}
return true;
default:
return false;
}
}
public void DrawTransport(Graphics g)
{
if (EntityAccordionBus == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(EntityAccordionBus.AdditionalColor);
Brush bodyBrush = new SolidBrush(EntityAccordionBus.BodyColor);
// границы автобуса
g.FillRectangle(bodyBrush, _startPosX.Value + 10, _startPosY.Value + 10, 50, 30);
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 10, 50, 30);
// окна
Brush brushBlue = new SolidBrush(Color.Blue);
g.FillEllipse(brushBlue, _startPosX.Value + 15, _startPosY.Value + 15, 5, 10);
g.FillEllipse(brushBlue, _startPosX.Value + 50, _startPosY.Value + 15, 5, 10);
g.FillEllipse(brushBlue, _startPosX.Value + 40, _startPosY.Value + 15, 5, 10);
g.DrawEllipse(pen, _startPosX.Value + 15, _startPosY.Value + 15, 5, 10);
g.DrawEllipse(pen, _startPosX.Value + 50, _startPosY.Value + 15, 5, 10);
g.DrawEllipse(pen, _startPosX.Value + 40, _startPosY.Value + 15, 5, 10);
// крылья
if (EntityAccordionBus.Compartment)
{
_drawningAccordionBusWidth = 130;
// отсек
g.FillRectangle(bodyBrush, _startPosX.Value + 70, _startPosY.Value + 10, 50, 30);
g.FillRectangle(additionalBrush, _startPosX.Value + 60, _startPosY.Value + 15, 10, 20);
g.DrawRectangle(pen, _startPosX.Value + 70, _startPosY.Value + 10, 50, 30);
g.DrawRectangle(pen, _startPosX.Value + 60, _startPosY.Value + 15, 10, 20);
// колеса
g.FillEllipse(additionalBrush, _startPosX.Value + 75, _startPosY.Value + 35, 10, 10);
g.FillEllipse(additionalBrush, _startPosX.Value + 105, _startPosY.Value + 35, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 75, _startPosY.Value + 35, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 105, _startPosY.Value + 35, 10, 10);
if (EntityAccordionBus.Entrance)
{
g.FillRectangle(additionalBrush, _startPosX.Value + 88, _startPosY.Value + 20, 10, 20);
g.DrawRectangle(pen, _startPosX.Value + 88, _startPosY.Value + 20, 10, 20);
}
if (EntityAccordionBus.Windows)
{
g.FillEllipse(brushBlue, _startPosX.Value + 75, _startPosY.Value + 15, 5, 10);
g.FillEllipse(brushBlue, _startPosX.Value + 110, _startPosY.Value + 15, 5, 10);
g.FillEllipse(brushBlue, _startPosX.Value + 100, _startPosY.Value + 15, 5, 10);
g.DrawEllipse(pen, _startPosX.Value + 75, _startPosY.Value + 15, 5, 10);
g.DrawEllipse(pen, _startPosX.Value + 110, _startPosY.Value + 15, 5, 10);
g.DrawEllipse(pen, _startPosX.Value + 100, _startPosY.Value + 15, 5, 10);
}
}
// двери
g.FillRectangle(additionalBrush, _startPosX.Value + 28, _startPosY.Value + 20, 10, 20);
g.DrawRectangle(pen, _startPosX.Value + 28, _startPosY.Value + 20, 10, 20);
// колеса
g.FillEllipse(additionalBrush, _startPosX.Value + 15, _startPosY.Value + 35, 10, 10);
g.FillEllipse(additionalBrush, _startPosX.Value + 45, _startPosY.Value + 35, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 15, _startPosY.Value + 35, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 45, _startPosY.Value + 35, 10, 10);
}
}

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAccordionBus;
namespace ProjectAccordionBus.Drawnings;
/// <summary>
/// Направление перемещения

View File

@ -0,0 +1,61 @@
using ProjectAccordionBus.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAccordionBus.Drawnings;
public class DrawningAccordionBus : DrawningBus
{
public DrawningAccordionBus(EntityAccordionBus entity) : base(110, 40)
{
EntityBus = entity;
}
public override void DrawTransport(Graphics g)
{
if (EntityBus == null || EntityBus is not EntityAccordionBus accordionBus || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(accordionBus.AdditionalColor);
base.DrawTransport(g);
Brush brushBlue = new SolidBrush(Color.Blue);
// отсек
g.FillRectangle(additionalBrush, _startPosX.Value + 60, _startPosY.Value, 50, 30);
g.FillRectangle(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 5, 10, 20);
g.DrawRectangle(pen, _startPosX.Value + 60, _startPosY.Value, 50, 30);
g.DrawRectangle(pen, _startPosX.Value + 50, _startPosY.Value + 5, 10, 20);
// колеса
g.FillEllipse(additionalBrush, _startPosX.Value + 65, _startPosY.Value + 25, 10, 10);
g.FillEllipse(additionalBrush, _startPosX.Value + 95, _startPosY.Value + 25, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 65, _startPosY.Value + 25, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 95, _startPosY.Value + 25, 10, 10);
if (accordionBus.Entrance)
{
g.FillRectangle(additionalBrush, _startPosX.Value + 78, _startPosY.Value + 10, 10, 20);
g.DrawRectangle(pen, _startPosX.Value + 78, _startPosY.Value + 10, 10, 20);
}
if (accordionBus.Windows)
{
g.FillEllipse(brushBlue, _startPosX.Value + 65, _startPosY.Value + 5, 5, 10);
g.FillEllipse(brushBlue, _startPosX.Value + 100, _startPosY.Value + 5, 5, 10);
g.FillEllipse(brushBlue, _startPosX.Value + 90, _startPosY.Value + 5, 5, 10);
g.DrawEllipse(pen, _startPosX.Value + 65, _startPosY.Value + 5, 5, 10);
g.DrawEllipse(pen, _startPosX.Value + 100, _startPosY.Value + 5, 5, 10);
g.DrawEllipse(pen, _startPosX.Value + 90, _startPosY.Value + 5, 5, 10);
}
}
}

View File

@ -0,0 +1,176 @@
using ProjectAccordionBus.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAccordionBus.Drawnings;
public class DrawningBus
{
public EntityBus? EntityBus { get; protected set; }
private int? _pictureWidth;
private int? _pictureHeight;
protected int? _startPosX;
protected int? _startPosY;
private int _drawningBusWidth = 50;
private readonly int _drawningBusHeight = 40;
// Пустой конструктор
private DrawningBus()
{
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
// Конструктор
public DrawningBus(EntityBus entity) : this()
{
EntityBus = entity;
}
// Конструктор
protected DrawningBus(int drawningBusWidth, int drawningBusHeight) : this()
{
_drawningBusWidth = drawningBusWidth;
_drawningBusHeight = drawningBusHeight;
}
public bool SetPictureSize(int width, int height)
{
if (_drawningBusWidth <= width && _drawningBusHeight <= height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX.HasValue && _startPosY.HasValue)
{
if (_startPosX + _drawningBusWidth > width)
{
_startPosX = width - _drawningBusWidth;
}
if (_startPosY + _drawningBusHeight > height)
{
_startPosY = height - _drawningBusHeight;
}
}
return true;
}
return false;
}
public void SetPosition(int x, int y)
{
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
_startPosX = x;
_startPosY = y;
if (x < 0)
{
_startPosX = 0;
}
if (y < 0)
{
_startPosY = 0;
}
if (x + _drawningBusWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _drawningBusWidth;
}
if (y + _drawningBusHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _drawningBusHeight;
}
}
public bool MoveTransport(DirectionType direction)
{
if (EntityBus == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return false;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityBus.Step > 0)
{
_startPosX -= (int)EntityBus.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntityBus.Step > 0)
{
_startPosY -= (int)EntityBus.Step;
}
return true;
// вправо
case DirectionType.Right:
if (_startPosX.Value + _drawningBusWidth + EntityBus.Step < _pictureWidth.Value)
{
_startPosX += (int)EntityBus.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + _drawningBusHeight + EntityBus.Step < _pictureHeight.Value)
{
_startPosY += (int)EntityBus.Step;
}
return true;
default:
return false;
}
}
public virtual void DrawTransport(Graphics g)
{
if (EntityBus == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush bodyBrush = new SolidBrush(EntityBus.BodyColor);
// границы автобуса
g.FillRectangle(bodyBrush, _startPosX.Value, _startPosY.Value, 50, 30);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value, 50, 30);
// окна
Brush brushBlue = new SolidBrush(Color.Blue);
g.FillEllipse(brushBlue, _startPosX.Value + 5, _startPosY.Value + 5, 5, 10);
g.FillEllipse(brushBlue, _startPosX.Value + 40, _startPosY.Value + 5, 5, 10);
g.FillEllipse(brushBlue, _startPosX.Value + 30, _startPosY.Value + 5, 5, 10);
g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 5, 5, 10);
g.DrawEllipse(pen, _startPosX.Value + 40, _startPosY.Value + 5, 5, 10);
g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value + 5, 5, 10);
// двери
g.FillRectangle(bodyBrush, _startPosX.Value + 18, _startPosY.Value + 10, 10, 20);
g.DrawRectangle(pen, _startPosX.Value + 18, _startPosY.Value + 10, 10, 20);
// колеса
g.FillEllipse(bodyBrush, _startPosX.Value + 5, _startPosY.Value + 25, 10, 10);
g.FillEllipse(bodyBrush, _startPosX.Value + 35, _startPosY.Value + 25, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 25, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 35, _startPosY.Value + 25, 10, 10);
}
}

View File

@ -4,20 +4,12 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAccordionBus;
namespace ProjectAccordionBus.Entities;
public class EntityAccordionBus
public class EntityAccordionBus : EntityBus
{
public int Speed { get; private set; }
public double Weight { get; private set; }
public Color BodyColor { get; private set; }
public Color AdditionalColor { get; private set; }
public double Step => Speed * 50 / Weight;
public bool Compartment { get; private set; }
public bool Entrance { get; private set; }
@ -25,21 +17,15 @@ public class EntityAccordionBus
public bool Windows { get; private set; }
/// <summary>
/// Инициализация полей объекта-класса спортивного автомобиля
/// Инициализация полей объекта-класса accordion bus
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="compartment">Признак наличия доп. отсека с гармошкой</param>
/// <param name="entrance">Признак наличия входа у доп. отсека</param>
/// <param name="windows">Признак наличия окон у доп. отсека</param>
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool compartment, bool entrance, bool windows)
public EntityAccordionBus(int Speed, double Weight, Color bodyColor, Color additionalColor, bool compartment, bool entrance, bool windows) : base(Speed, Weight, bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Compartment = compartment; // отсек
Entrance = entrance; // крылья

View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace ProjectAccordionBus.Entities;
public class EntityBus
{
public int Speed { get; private set; }
public double Weight { get; private set; }
public Color BodyColor { get; private set; }
public double Step => Speed * 50 / Weight;
/// <summary>
/// Конструктор сущности
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Основной цвет</param>
public EntityBus(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View File

@ -35,6 +35,7 @@
buttonDown = new Button();
buttonUp = new Button();
buttonRight = new Button();
buttonCreateBus = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxAccordionBus).BeginInit();
SuspendLayout();
//
@ -53,9 +54,9 @@
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 351);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(75, 23);
buttonCreate.Size = new Size(188, 23);
buttonCreate.TabIndex = 1;
buttonCreate.Text = "Создать";
buttonCreate.Text = "Создать автобус-гармошку";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += ButtonCreate_Click;
//
@ -107,11 +108,23 @@
buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += ButtonMove_Click;
//
// buttonCreateBus
//
buttonCreateBus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateBus.Location = new Point(218, 351);
buttonCreateBus.Name = "buttonCreateBus";
buttonCreateBus.Size = new Size(138, 23);
buttonCreateBus.TabIndex = 6;
buttonCreateBus.Text = "Создать автобус";
buttonCreateBus.UseVisualStyleBackColor = true;
buttonCreateBus.Click += buttonCreateBus_Click;
//
// FormAccordionBus
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(728, 386);
Controls.Add(buttonCreateBus);
Controls.Add(buttonRight);
Controls.Add(buttonUp);
Controls.Add(buttonDown);
@ -137,5 +150,6 @@
private Button buttonDown;
private Button buttonUp;
private Button buttonRight;
private Button buttonCreateBus;
}
}

View File

@ -1,4 +1,6 @@
using System;
using ProjectAccordionBus.Drawnings;
using ProjectAccordionBus.Entities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
@ -12,9 +14,11 @@ namespace ProjectAccordionBus;
public partial class FormAccordionBus : Form
{
private DrawningAccordionBus? _drawningAccordionBus;
private DrawningBus? _drawningBus;
private EntityAccordionBus? _entity;
private EntityBus? _entity;
private EntityAccordionBus? _entityAccordionBus;
public FormAccordionBus()
@ -24,36 +28,57 @@ public partial class FormAccordionBus : Form
private void Draw()
{
if (_drawningAccordionBus == null)
if (_drawningBus == null)
{
return;
}
Bitmap bmp = new(pictureBoxAccordionBus.Width, pictureBoxAccordionBus.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningAccordionBus.DrawTransport(gr);
_drawningBus.DrawTransport(gr);
pictureBoxAccordionBus.Image = bmp;
}
private void ButtonCreate_Click(object sender, EventArgs e)
private void CreateObject(string type)
{
Random random = new();
_drawningAccordionBus = new DrawningAccordionBus();
_entity = new EntityAccordionBus();
_entity.Init(random.Next(100, 300), random.Next(1000, 3000),
switch (type)
{
case nameof(DrawningBus):
_entity = new EntityBus(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
_drawningBus = new DrawningBus(_entity);
break;
case nameof(DrawningAccordionBus):
_entityAccordionBus = new EntityAccordionBus(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)), Convert.ToBoolean(random.Next(0, 2)));
_drawningAccordionBus.Init(_entity);
_drawningAccordionBus.SetPictureSize(pictureBoxAccordionBus.Width, pictureBoxAccordionBus.Height);
_drawningAccordionBus.SetPosition(random.Next(10, 100), random.Next(10, 100));
_drawningBus = new DrawningAccordionBus(_entityAccordionBus);
break;
default:
return;
}
_drawningBus.SetPictureSize(pictureBoxAccordionBus.Width, pictureBoxAccordionBus.Height);
_drawningBus.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
CreateObject(nameof(DrawningAccordionBus));
}
private void buttonCreateBus_Click(object sender, EventArgs e)
{
CreateObject(nameof(DrawningBus));
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningAccordionBus == null)
if (_drawningBus == null)
{
return;
}
@ -63,16 +88,16 @@ public partial class FormAccordionBus : Form
switch (name)
{
case "buttonUp":
result = _drawningAccordionBus.MoveTransport(DirectionType.Up);
result = _drawningBus.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawningAccordionBus.MoveTransport(DirectionType.Down);
result = _drawningBus.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = _drawningAccordionBus.MoveTransport(DirectionType.Left);
result = _drawningBus.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _drawningAccordionBus.MoveTransport(DirectionType.Right);
result = _drawningBus.MoveTransport(DirectionType.Right);
break;
}
@ -81,6 +106,8 @@ public partial class FormAccordionBus : Form
Draw();
}
}
}