Files
Traffic-Light/Form1.cs
2025-11-04 12:12:17 +01:00

84 lines
1.8 KiB
C#

namespace Traffic_Light
{
public partial class Form1 : Form
{
int state = 1;
bool reverse = false;
int borderSize = 10;
public Form1()
{
InitializeComponent();
}
public void Switch()
{
if (state == 3) reverse = true;
if (state == 1) reverse = false;
if (reverse)
{
state--;
}
else
{
state++;
}
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
var brush = new SolidBrush(Color.Black);
var pen = new Pen(Color.Black, borderSize);
g.DrawRectangle(pen,
50,
50,
50,
150
);
brush.Color = Color.Gray;
g.FillRectangle(brush,
50,
50,
50,
150
);
g.FillRectangle(brush, 50, 50, 50, 150);
if (state == 1)
{
brush.Color = Color.Green;
g.FillEllipse(brush, 50, 50, 50, 50);
return;
}
if (state == 2)
{
brush.Color = Color.Yellow;
g.FillEllipse(brush, 50, 100, 50, 50);
return;
}
if (state == 3)
{
brush.Color = Color.Red;
g.FillEllipse(brush, 50, 150, 50, 50);
return;
}
}
private void btn_switch_Click(object sender, EventArgs e)
{
Switch();
}
}
}