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

This commit is contained in:
Дамир Нугаев 2022-10-09 21:08:29 +04:00
parent f6797078a8
commit 1b9308c712
5 changed files with 153 additions and 8 deletions

View File

@ -8,9 +8,9 @@ namespace Bus
{
internal class DrawingBus
{
public EntityBus Bus { get; private set; }
private float _startPosX;
private float _startPosY;
public EntityBus Bus { get; protected set; }
protected float _startPosX;
protected float _startPosY;
private int? _pictureWidth = null;
private int? _pictureHeight = null;
protected readonly int _busWidth = 157;
@ -21,6 +21,14 @@ namespace Bus
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)
{
if (x < 0 || y < 0 || width < x+_busWidth || height < y + _busHeight)
@ -71,7 +79,7 @@ namespace Bus
}
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,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bus
{
internal class DrawingSportBus : DrawingBus
{
public DrawingSportBus(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool wing, bool sportLine) :
base(speed, weight, bodyColor)
{
Bus = new EntitySportBus(speed, weight, bodyColor, dopColor, bodyKit, wing, sportLine);
}
public override void DrawTransport(Graphics g)
{
if (Bus is not EntitySportBus sportBus)
{
return;
}
Pen pen = new Pen(Color.Black);
Brush dopBrush = new SolidBrush(sportBus.DopColor);
if (sportBus.BodyKit)
{
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);
}
_startPosX += 10;
_startPosY += 5;
base.DrawTransport(g);
_startPosX -= 10;
_startPosY -= 5;
if (sportBus.Sportline)
{
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);
}
if (sportBus.Wing)
{
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);
}
}
}
}

25
Bus/Bus/EntitySportBus.cs Normal file
View File

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bus
{
internal class EntitySportBus : EntityBus
{
public Color DopColor { get; private set; }
public bool BodyKit { get; private set; }
public bool Wing { get; private set; }
public bool Sportline { get; private set; }
public EntitySportBus(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool wing, bool sportline) :
base(speed, weight, bodyColor)
{
DopColor = dopColor;
BodyKit = bodyKit;
Wing = wing;
Sportline = sportline;
}
}
}

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();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxBus)).BeginInit();
this.statusStrip1.SuspendLayout();
this.SuspendLayout();
@ -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(111, 384);
this.buttonCreateModif.Name = "buttonCreateModif";
this.buttonCreateModif.Size = new System.Drawing.Size(141, 29);
this.buttonCreateModif.TabIndex = 7;
this.buttonCreateModif.Text = "Модификация";
this.buttonCreateModif.UseVisualStyleBackColor = true;
this.buttonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click);
//
// FormBus
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(827, 455);
this.Controls.Add(this.buttonCreateModif);
this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonLeft);
@ -178,5 +190,6 @@
private Button buttonLeft;
private Button buttonDown;
private Button buttonRight;
private Button buttonCreateModif;
}
}

View File

@ -18,14 +18,21 @@ namespace Bus
pictureBoxBus.Image = bmp;
}
private 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 ButtonCreate_Click(object sender, EventArgs e)
{
Random rnd = new();
_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)));
_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}";
SetData();
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
@ -54,5 +61,16 @@ namespace Bus
_bus?.ChangeBorbers(pictureBoxBus.Width, pictureBoxBus.Height);
Draw();
}
private void ButtonCreateModif_Click(object sender, EventArgs e)
{
Random rnd = new();
_bus = new DrawingSportBus(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, 1)), Convert.ToBoolean(rnd.Next(0,1)), Convert.ToBoolean(rnd.Next(0,1)));
SetData();
Draw();
}
}
}