PIbd-14_Rachek_A.S._Base/lab_0/FormBus.cs

89 lines
2.5 KiB
C#
Raw Normal View History

2024-02-29 10:45:00 +04:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProjectBus
{
public partial class FormBus : Form
{
private DrawingBus? _drawingBus;
public FormBus()
{
InitializeComponent();
}
private void Draw()
{
if (_drawingBus == null)
{
return;
}
Bitmap bmp = new(pictureBoxBus.Width, pictureBoxBus.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawingBus.DrawTransport(gr);
pictureBoxBus.Image = bmp;
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
Random random = new();
_drawingBus = new DrawingBus();
_drawingBus.Init(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)));
_drawingBus.SetPictureSize(pictureBoxBus.Width, pictureBoxBus.Height);
_drawingBus.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawingBus == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result =
_drawingBus.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result =
_drawingBus.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result =
_drawingBus.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result =
_drawingBus.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
}
}
}