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

This commit is contained in:
shadowik 2022-09-20 04:44:29 +04:00
parent 6d5e7b10c0
commit 55351a628c
5 changed files with 161 additions and 37 deletions

View File

@ -7,20 +7,27 @@ using System.Threading.Tasks;
namespace DoubleDeckerBus
{
internal class DrawningBus
internal class DrawingBus
{
public EntityBus Bus { get; private set; }
public EntityBus Bus { get; protected set; }
private float _startPosX;
private float _startPosY;
protected float _startPosX;
protected float _startPosY;
private int? _pictureWidth = null;
private int? _pictureHeight = null;
private readonly int _busWidth = 100;
private readonly int _busHeight = 50;
private readonly int _busHeight = 30;
public DrawningBus(int speed, float weight, Color bodyColor) {
Bus = new EntityBus();
Bus.Init(speed, weight, bodyColor);
public DrawingBus(int speed, float weight, Color bodyColor) {
Bus = new EntityBus(speed, weight, bodyColor);
}
protected DrawingBus(int speed, float weight, Color bodyColor, int busWidth, int busHeight) : this(speed, weight, bodyColor)
{
_busWidth = busWidth;
_busHeight = busHeight;
}
public void SetPosition(int x, int y, int width, int height) {
@ -66,7 +73,7 @@ namespace DoubleDeckerBus
}
}
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,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus
{
internal class DrawingDDB : DrawingBus
{
public DrawingDDB(int speed, float weight, Color bodyColor, Color extraColor, bool ledder, bool secondStage) : base(speed, weight, bodyColor, 100, 100) {
Bus = new EntityDDB(speed, weight, bodyColor, extraColor, ledder, secondStage);
}
public override void DrawTransport(Graphics g)
{
if (Bus is not EntityDDB doubleDeckerBus)
{
return;
}
Pen pen = new(Color.Black);
Brush extraBrush = new SolidBrush(doubleDeckerBus.ExtraColor);
if (doubleDeckerBus.SecondStage) {
g.FillRectangle(extraBrush, _startPosX, _startPosY + 10, 100, 30);
g.DrawRectangle(pen, _startPosX + 30, _startPosY + 20, 10, 20);
Brush brBlack = new SolidBrush(Color.Black);
g.FillRectangle(brBlack, _startPosX + 30, _startPosY + 20, 10, 20);
Brush brBlue = new SolidBrush(Color.Blue);
g.FillEllipse(brBlue, _startPosX + 10, _startPosY + 15, 10, 15);
g.FillEllipse(brBlue, _startPosX + 50, _startPosY + 15, 10, 15);
g.FillEllipse(brBlue, _startPosX + 70, _startPosY + 15, 10, 15);
g.FillEllipse(brBlue, _startPosX + 90, _startPosY + 15, 10, 15);
}
_startPosY += 30;
base.DrawTransport(g);
_startPosY -= 30;
if (doubleDeckerBus.Ledder) {
//Вертикальные прямые
g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 70)), new Point((int)(_startPosX), (int)(_startPosY + 10)));
g.DrawLine(pen, new Point((int)(_startPosX + 10), (int)(_startPosY + 70)), new Point((int)(_startPosX + 10), (int)(_startPosY + 10)));
//Горизонтальные прямые
g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 20)), new Point((int)(_startPosX + 10), (int)(_startPosY + 20)));
g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 30)), new Point((int)(_startPosX + 10), (int)(_startPosY + 30)));
g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 40)), new Point((int)(_startPosX + 10), (int)(_startPosY + 40)));
g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 50)), new Point((int)(_startPosX + 10), (int)(_startPosY + 50)));
g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 60)), new Point((int)(_startPosX + 10), (int)(_startPosY + 60)));
}
}
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus
{
internal class EntityDDB : EntityBus
{
public Color ExtraColor { get; private set; }
public bool Ledder { get; private set; }
public bool SecondStage { get; private set; }
public EntityDDB(int speed, float height, Color bodyColor, Color extraColor, bool ledder, bool secondStage) : base(speed, height, bodyColor)
{
ExtraColor = extraColor;
Ledder = ledder;
SecondStage = secondStage;
}
}
}

View File

@ -38,6 +38,7 @@
this.buttonDown = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.pictureBoxBus = new System.Windows.Forms.PictureBox();
this.buttonСreateExtra = new System.Windows.Forms.Button();
this.statusStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxBus)).BeginInit();
this.SuspendLayout();
@ -49,36 +50,38 @@
this.toolStripStatusLabelSpeed,
this.toolStripStatusLabelWeight,
this.toolStripStatusLabelBodyColor});
this.statusStrip1.Location = new System.Drawing.Point(0, 425);
this.statusStrip1.Location = new System.Drawing.Point(0, 316);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(800, 26);
this.statusStrip1.Padding = new System.Windows.Forms.Padding(1, 0, 12, 0);
this.statusStrip1.Size = new System.Drawing.Size(700, 22);
this.statusStrip1.TabIndex = 1;
this.statusStrip1.Text = "statusStrip1";
//
// toolStripStatusLabelSpeed
//
this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed";
this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(80, 20);
this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(65, 17);
this.toolStripStatusLabelSpeed.Text = "Скорость: ";
//
// toolStripStatusLabelWeight
//
this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight";
this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(40, 20);
this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(32, 17);
this.toolStripStatusLabelWeight.Text = "Вес: ";
//
// toolStripStatusLabelBodyColor
//
this.toolStripStatusLabelBodyColor.Name = "toolStripStatusLabelBodyColor";
this.toolStripStatusLabelBodyColor.Size = new System.Drawing.Size(49, 20);
this.toolStripStatusLabelBodyColor.Size = new System.Drawing.Size(39, 17);
this.toolStripStatusLabelBodyColor.Text = "Цвет: ";
//
// buttonCreate
//
this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreate.Location = new System.Drawing.Point(0, 396);
this.buttonCreate.Location = new System.Drawing.Point(0, 297);
this.buttonCreate.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(99, 28);
this.buttonCreate.Size = new System.Drawing.Size(87, 21);
this.buttonCreate.TabIndex = 2;
this.buttonCreate.Text = "Создать";
this.buttonCreate.UseVisualStyleBackColor = true;
@ -89,9 +92,10 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.UpArrow;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(718, 337);
this.buttonUp.Location = new System.Drawing.Point(628, 253);
this.buttonUp.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 29);
this.buttonUp.Size = new System.Drawing.Size(26, 22);
this.buttonUp.TabIndex = 3;
this.buttonUp.Text = " ";
this.buttonUp.UseVisualStyleBackColor = true;
@ -102,9 +106,10 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.LeftArrow;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(693, 363);
this.buttonLeft.Location = new System.Drawing.Point(606, 272);
this.buttonLeft.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 29);
this.buttonLeft.Size = new System.Drawing.Size(26, 22);
this.buttonLeft.TabIndex = 4;
this.buttonLeft.Text = " ";
this.buttonLeft.UseVisualStyleBackColor = true;
@ -115,9 +120,10 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.DownArrow;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(719, 388);
this.buttonDown.Location = new System.Drawing.Point(629, 291);
this.buttonDown.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 29);
this.buttonDown.Size = new System.Drawing.Size(26, 22);
this.buttonDown.TabIndex = 5;
this.buttonDown.Text = " ";
this.buttonDown.UseVisualStyleBackColor = true;
@ -128,9 +134,10 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.RightArrow;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(745, 363);
this.buttonRight.Location = new System.Drawing.Point(652, 272);
this.buttonRight.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 29);
this.buttonRight.Size = new System.Drawing.Size(26, 22);
this.buttonRight.TabIndex = 6;
this.buttonRight.Text = " ";
this.buttonRight.UseVisualStyleBackColor = true;
@ -140,18 +147,30 @@
//
this.pictureBoxBus.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxBus.Location = new System.Drawing.Point(0, 0);
this.pictureBoxBus.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.pictureBoxBus.Name = "pictureBoxBus";
this.pictureBoxBus.Size = new System.Drawing.Size(800, 425);
this.pictureBoxBus.Size = new System.Drawing.Size(700, 316);
this.pictureBoxBus.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxBus.TabIndex = 7;
this.pictureBoxBus.TabStop = false;
this.pictureBoxBus.Resize += new System.EventHandler(this.PictureBoxBus_Resize);
//
// buttonСreateExtra
//
this.buttonСreateExtra.Location = new System.Drawing.Point(103, 297);
this.buttonСreateExtra.Name = "buttonСreateExtra";
this.buttonСreateExtra.Size = new System.Drawing.Size(99, 21);
this.buttonСreateExtra.TabIndex = 8;
this.buttonСreateExtra.Text = "Модификация";
this.buttonСreateExtra.UseVisualStyleBackColor = true;
this.buttonСreateExtra.Click += new System.EventHandler(this.ButtonСreateExtra_Click);
//
// FormBus
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 451);
this.ClientSize = new System.Drawing.Size(700, 338);
this.Controls.Add(this.buttonСreateExtra);
this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonLeft);
@ -159,6 +178,7 @@
this.Controls.Add(this.buttonCreate);
this.Controls.Add(this.pictureBoxBus);
this.Controls.Add(this.statusStrip1);
this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.Name = "FormBus";
this.Text = "Двухэтажный автобус";
this.Resize += new System.EventHandler(this.PictureBoxBus_Resize);
@ -181,5 +201,6 @@
private Button buttonDown;
private Button buttonRight;
private PictureBox pictureBoxBus;
private Button buttonСreateExtra;
}
}

View File

@ -2,7 +2,7 @@ namespace DoubleDeckerBus
{
public partial class FormBus : Form
{
private DrawningBus _bus;
private DrawingBus _bus;
public FormBus()
{
InitializeComponent();
@ -19,16 +19,12 @@ namespace DoubleDeckerBus
private void ButtonCreate_Click(object sender, EventArgs e)
{
Random rnd = new();
_bus = new DrawningBus(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
_bus.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxBus.Width, pictureBoxBus.Height);
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {_bus.Bus.Speed}";
toolStripStatusLabelWeight.Text = $"Âåñ: {_bus.Bus.Weight}";
toolStripStatusLabelBodyColor.Text = $"Öâåò: {_bus.Bus.BodyColor.Name}";
_bus = new DrawingBus(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
SetData();
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
private void ButtonMove_Click(object sender, EventArgs e) {
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
@ -48,10 +44,28 @@ namespace DoubleDeckerBus
Draw();
}
private void PictureBoxBus_Resize(object sender, EventArgs e)
{
public void SetData() {
Random rnd = new();
_bus.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxBus.Width, pictureBoxBus.Height);
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {_bus.Bus.Speed}";
toolStripStatusLabelWeight.Text = $"Âåñ: {_bus.Bus.Weight}";
toolStripStatusLabelBodyColor.Text = $"Öâåò: {_bus.Bus.BodyColor.Name}";
}
private void PictureBoxBus_Resize(object sender, EventArgs e) {
_bus?.ChangeBorders(pictureBoxBus.Width, pictureBoxBus.Height);
Draw();
}
private void ButtonÑreateExtra_Click(object sender, EventArgs e)
{
Random rnd = new();
_bus = new DrawingDDB(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();
}
}
}