Переименованы кнопки по стандарту языка, добавлен метод для изменения размеров формы

This commit is contained in:
Nikita Potapov 2022-10-15 14:07:57 +04:00
parent 6023d417fb
commit 3eabfb5fd0
3 changed files with 166 additions and 121 deletions

View File

@ -16,7 +16,7 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
/// <summary>
/// Класс-сущность
/// </summary>
public EntityBoat Boat { protected set; get; }
public EntityBoat Boat { private set; get; }
/// <summary>
/// Левая координата отрисовки лодки
/// </summary>
@ -140,5 +140,29 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
g.DrawEllipse(Pens.Black, _startPosX + _boatWidth / 8, _startPosY + _boatHeight / 8,
_boatWidth / 2, _boatHeight - _boatHeight / 4);
}
/// <summary>
/// Смена границ формы отрисовки
/// </summary>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _boatWidth || _pictureHeight <= _boatHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _boatWidth > _pictureWidth)
{
_startPosX = _pictureWidth.Value - _boatWidth;
}
if (_startPosY + _boatHeight > _pictureHeight)
{
_startPosY = _pictureHeight.Value - _boatHeight;
}
}
}
}

177
FormBoat.Designer.cs generated
View File

@ -29,30 +29,31 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
/// </summary>
private void InitializeComponent()
{
this.btn_new = new System.Windows.Forms.Button();
this.ButtonCreate = new System.Windows.Forms.Button();
this.statusStrip = new System.Windows.Forms.StatusStrip();
this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelColor = new System.Windows.Forms.ToolStripStatusLabel();
this.groupBox = new System.Windows.Forms.GroupBox();
this.btn_up = new System.Windows.Forms.Button();
this.btn_down = new System.Windows.Forms.Button();
this.btn_left = new System.Windows.Forms.Button();
this.btn_right = new System.Windows.Forms.Button();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.ButtonUp = new System.Windows.Forms.Button();
this.ButtonDown = new System.Windows.Forms.Button();
this.ButtonLeft = new System.Windows.Forms.Button();
this.ButtonRight = new System.Windows.Forms.Button();
this.pictureBoxBoat = new System.Windows.Forms.PictureBox();
this.statusStrip.SuspendLayout();
this.groupBox.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxBoat)).BeginInit();
this.SuspendLayout();
//
// btn_new
// ButtonCreate
//
this.btn_new.Location = new System.Drawing.Point(12, 344);
this.btn_new.Name = "btn_new";
this.btn_new.Size = new System.Drawing.Size(75, 63);
this.btn_new.TabIndex = 10;
this.btn_new.Text = "Создать";
this.btn_new.Click += new System.EventHandler(this.btn_new_Click);
this.ButtonCreate.Location = new System.Drawing.Point(10, 277);
this.ButtonCreate.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.ButtonCreate.Name = "ButtonCreate";
this.ButtonCreate.Size = new System.Drawing.Size(118, 28);
this.ButtonCreate.TabIndex = 10;
this.ButtonCreate.Text = "Создать";
this.ButtonCreate.Click += new System.EventHandler(this.ButtonCreate_Click);
//
// statusStrip
//
@ -61,121 +62,115 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
this.toolStripStatusLabelSpeed,
this.toolStripStatusLabelWeight,
this.toolStripStatusLabelColor});
this.statusStrip.Location = new System.Drawing.Point(0, 415);
this.statusStrip.Location = new System.Drawing.Point(0, 309);
this.statusStrip.Name = "statusStrip";
this.statusStrip.Size = new System.Drawing.Size(1074, 26);
this.statusStrip.Padding = new System.Windows.Forms.Padding(1, 0, 12, 0);
this.statusStrip.Size = new System.Drawing.Size(940, 22);
this.statusStrip.TabIndex = 8;
this.statusStrip.Text = "statusStrip";
//
// 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 = "Вес: ";
//
// toolStripStatusLabelColor
//
this.toolStripStatusLabelColor.Name = "toolStripStatusLabelColor";
this.toolStripStatusLabelColor.Size = new System.Drawing.Size(49, 20);
this.toolStripStatusLabelColor.Size = new System.Drawing.Size(39, 17);
this.toolStripStatusLabelColor.Text = "Цвет: ";
//
// groupBox
//
this.groupBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.groupBox.Controls.Add(this.btn_up);
this.groupBox.Controls.Add(this.btn_down);
this.groupBox.Controls.Add(this.btn_left);
this.groupBox.Controls.Add(this.btn_right);
this.groupBox.Location = new System.Drawing.Point(923, 325);
this.groupBox.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.groupBox.Controls.Add(this.ButtonUp);
this.groupBox.Controls.Add(this.ButtonDown);
this.groupBox.Controls.Add(this.ButtonLeft);
this.groupBox.Controls.Add(this.ButtonRight);
this.groupBox.Location = new System.Drawing.Point(808, 244);
this.groupBox.Name = "groupBox";
this.groupBox.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.groupBox.Size = new System.Drawing.Size(139, 88);
this.groupBox.Size = new System.Drawing.Size(122, 66);
this.groupBox.TabIndex = 9;
this.groupBox.TabStop = false;
//
// btn_up
// ButtonUp
//
this.btn_up.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.btn_up.Image = global::PIbd_21_Potapov_N.S._Catamaran_Base.Properties.Resources.arrow_up1;
this.btn_up.Location = new System.Drawing.Point(51, 0);
this.btn_up.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.btn_up.Name = "btn_up";
this.btn_up.Size = new System.Drawing.Size(40, 40);
this.btn_up.TabIndex = 1;
this.btn_up.UseVisualStyleBackColor = true;
this.btn_up.Click += new System.EventHandler(this.btn_move_Click);
this.ButtonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ButtonUp.Image = global::PIbd_21_Potapov_N.S._Catamaran_Base.Properties.Resources.arrow_up1;
this.ButtonUp.Location = new System.Drawing.Point(45, 0);
this.ButtonUp.Name = "ButtonUp";
this.ButtonUp.Size = new System.Drawing.Size(35, 30);
this.ButtonUp.TabIndex = 1;
this.ButtonUp.UseVisualStyleBackColor = true;
this.ButtonUp.Click += new System.EventHandler(this.ButtonMove_Click);
//
// btn_down
// ButtonDown
//
this.btn_down.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.btn_down.Image = global::PIbd_21_Potapov_N.S._Catamaran_Base.Properties.Resources.arrow_down1;
this.btn_down.Location = new System.Drawing.Point(51, 42);
this.btn_down.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.btn_down.Name = "btn_down";
this.btn_down.Size = new System.Drawing.Size(40, 40);
this.btn_down.TabIndex = 2;
this.btn_down.Text = " ";
this.btn_down.UseVisualStyleBackColor = true;
this.btn_down.Click += new System.EventHandler(this.btn_move_Click);
this.ButtonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ButtonDown.Image = global::PIbd_21_Potapov_N.S._Catamaran_Base.Properties.Resources.arrow_down1;
this.ButtonDown.Location = new System.Drawing.Point(45, 32);
this.ButtonDown.Name = "ButtonDown";
this.ButtonDown.Size = new System.Drawing.Size(35, 30);
this.ButtonDown.TabIndex = 2;
this.ButtonDown.Text = " ";
this.ButtonDown.UseVisualStyleBackColor = true;
this.ButtonDown.Click += new System.EventHandler(this.ButtonMove_Click);
//
// btn_left
// ButtonLeft
//
this.btn_left.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.btn_left.Image = global::PIbd_21_Potapov_N.S._Catamaran_Base.Properties.Resources.arrow_left1;
this.btn_left.Location = new System.Drawing.Point(5, 42);
this.btn_left.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.btn_left.Name = "btn_left";
this.btn_left.Size = new System.Drawing.Size(40, 40);
this.btn_left.TabIndex = 3;
this.btn_left.UseVisualStyleBackColor = true;
this.btn_left.Click += new System.EventHandler(this.btn_move_Click);
this.ButtonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ButtonLeft.Image = global::PIbd_21_Potapov_N.S._Catamaran_Base.Properties.Resources.arrow_left1;
this.ButtonLeft.Location = new System.Drawing.Point(4, 32);
this.ButtonLeft.Name = "ButtonLeft";
this.ButtonLeft.Size = new System.Drawing.Size(35, 30);
this.ButtonLeft.TabIndex = 3;
this.ButtonLeft.UseVisualStyleBackColor = true;
this.ButtonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
//
// btn_right
// ButtonRight
//
this.btn_right.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.btn_right.Image = global::PIbd_21_Potapov_N.S._Catamaran_Base.Properties.Resources.arrow_right1;
this.btn_right.Location = new System.Drawing.Point(97, 44);
this.btn_right.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.btn_right.Name = "btn_right";
this.btn_right.Size = new System.Drawing.Size(40, 40);
this.btn_right.TabIndex = 4;
this.btn_right.UseVisualStyleBackColor = true;
this.btn_right.Click += new System.EventHandler(this.btn_move_Click);
this.ButtonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ButtonRight.Image = global::PIbd_21_Potapov_N.S._Catamaran_Base.Properties.Resources.arrow_right1;
this.ButtonRight.Location = new System.Drawing.Point(85, 33);
this.ButtonRight.Name = "ButtonRight";
this.ButtonRight.Size = new System.Drawing.Size(35, 30);
this.ButtonRight.TabIndex = 4;
this.ButtonRight.UseVisualStyleBackColor = true;
this.ButtonRight.Click += new System.EventHandler(this.ButtonMove_Click);
//
// pictureBox
// pictureBoxBoat
//
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(1074, 441);
this.pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox.TabIndex = 0;
this.pictureBox.TabStop = false;
this.pictureBoxBoat.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxBoat.Location = new System.Drawing.Point(0, 0);
this.pictureBoxBoat.Name = "pictureBoxBoat";
this.pictureBoxBoat.Size = new System.Drawing.Size(940, 331);
this.pictureBoxBoat.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxBoat.TabIndex = 0;
this.pictureBoxBoat.TabStop = false;
this.pictureBoxBoat.Resize += new System.EventHandler(this.PictureBoxBoat_Resize);
//
// FormCatamaran
// FormBoat
//
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(1074, 441);
this.ClientSize = new System.Drawing.Size(940, 331);
this.Controls.Add(this.groupBox);
this.Controls.Add(this.btn_new);
this.Controls.Add(this.ButtonCreate);
this.Controls.Add(this.statusStrip);
this.Controls.Add(this.pictureBox);
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.Name = "FormCatamaran";
this.Controls.Add(this.pictureBoxBoat);
this.Name = "FormBoat";
this.Text = "Лодка";
this.statusStrip.ResumeLayout(false);
this.statusStrip.PerformLayout();
this.groupBox.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxBoat)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
@ -183,12 +178,12 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
#endregion
private System.Windows.Forms.PictureBox pictureBox;
private System.Windows.Forms.Button btn_up;
private System.Windows.Forms.Button btn_down;
private System.Windows.Forms.Button btn_left;
private System.Windows.Forms.Button btn_right;
private System.Windows.Forms.Button btn_new;
private System.Windows.Forms.PictureBox pictureBoxBoat;
private System.Windows.Forms.Button ButtonUp;
private System.Windows.Forms.Button ButtonDown;
private System.Windows.Forms.Button ButtonLeft;
private System.Windows.Forms.Button ButtonRight;
private System.Windows.Forms.Button ButtonCreate;
private System.Windows.Forms.StatusStrip statusStrip;
private System.Windows.Forms.GroupBox groupBox;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelSpeed;

View File

@ -12,68 +12,94 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
{
public partial class FormBoat : System.Windows.Forms.Form
{
DrawingBoat boatDrawObj;
DrawingBoat _boat;
public FormBoat()
{
InitializeComponent();
pictureBox.Image = new Bitmap(pictureBox.Width, pictureBox.Height);
}
private void btn_new_Click(object sender, EventArgs e)
/// <summary>
/// Обработчик нажатия кнопки "Создать"
/// </summary>
private void ButtonCreate_Click(object sender, EventArgs e)
{
Random rnd = new Random();
boatDrawObj = new DrawingBoat();
boatDrawObj.Init(rnd.Next(50, 100), rnd.Next(50, 100), Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)));
boatDrawObj.SetPosition(rnd.Next(10, 50), rnd.Next(10, 50), pictureBox.Width, pictureBox.Height - statusStrip.Height);
toolStripStatusLabelSpeed.Text = $"Скорость: {boatDrawObj.Boat.Speed}";
toolStripStatusLabelWeight.Text = $"Вес: {boatDrawObj.Boat.Weight}";
toolStripStatusLabelColor.Text = $"Цвет: {boatDrawObj.Boat.BodyColor.Name}";
RedrawCatamaran();
_boat = new DrawingBoat();
_boat.Init(
rnd.Next(50, 100),
rnd.Next(50, 100),
Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255))
);
_boat.SetPosition(
rnd.Next(10, 50),
rnd.Next(10, 50),
pictureBoxBoat.Width,
pictureBoxBoat.Height - statusStrip.Height
);
toolStripStatusLabelSpeed.Text = $"Скорость: {_boat.Boat.Speed}";
toolStripStatusLabelWeight.Text = $"Вес: {_boat.Boat.Weight}";
toolStripStatusLabelColor.Text = $"Цвет: {_boat.Boat.BodyColor.Name}";
Draw();
}
private void btn_move_Click(object sender, EventArgs e)
/// <summary>
/// Обработчик нажатия кнопок передвижения
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (boatDrawObj == null)
if (_boat == null)
return;
string btnName = ((Button)sender).Name;
switch (btnName)
{
case "btn_up":
case "ButtonUp":
{
boatDrawObj.MoveTransport(Direction.Up);
_boat.MoveTransport(Direction.Up);
}
break;
case "btn_down":
case "ButtonDown":
{
boatDrawObj.MoveTransport(Direction.Down);
_boat.MoveTransport(Direction.Down);
}
break;
case "btn_left":
case "ButtonLeft":
{
boatDrawObj.MoveTransport(Direction.Left);
_boat.MoveTransport(Direction.Left);
}
break;
case "btn_right":
case "ButtonRight":
{
boatDrawObj.MoveTransport(Direction.Right);
_boat.MoveTransport(Direction.Right);
}
break;
default:
break;
}
RedrawCatamaran();
Draw();
}
private void RedrawCatamaran()
/// <summary>
/// Метод прорисовки лодки
/// </summary>
private void Draw()
{
Graphics g = Graphics.FromImage(pictureBox.Image);
g.Clear(Color.White);
boatDrawObj.DrawTransport(g);
pictureBox.Invalidate();
Bitmap bmp = new(pictureBoxBoat.Width, pictureBoxBoat.Height);
Graphics g = Graphics.FromImage(bmp);
_boat?.DrawTransport(g);
pictureBoxBoat.Image = bmp;
}
/// <summary>
/// Изменение размеров формы
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PictureBoxBoat_Resize(object sender, EventArgs e)
{
_boat?.ChangeBorders(pictureBoxBoat.Width, pictureBoxBoat.Height - statusStrip.Height);
Draw();
}
}
}