Я сделал программу, которая может рисовать светофоры.Я поместил один светофор в верхнем левом углу, а другой - примерно в середине окна.Но, похоже, в моей программе есть прямоугольное поле.Только там светофор можно нарисовать.Это поле начинается в верхнем левом углу и идет к центру окна.В результате можно увидеть один светофор, но другой светофор выключен в конце этого поля.Я не знаю, почему
Мой код:
public partial class Form1 : Form
{
private Timer timer;
private TrafficLight[] lights;
private Panel[] light_panels;
public Form1()
{
InitializeComponent();
timer = new Timer();
timer.Tick += timer_Tick;
timer.Interval = 2000;
timer.Enabled = true;
timer.Start();
lights = new TrafficLight[]
{
new TrafficLight(10, 10, 75, 150, 0),
new TrafficLight(50, 50, 75, 150, 2)
};
light_panels = new Panel[]
{
new Panel(),
new Panel()
};
for (int i = 0; i < light_panels.Length; ++i)
{
light_panels[i].Width = lights[i].Width;
light_panels[i].Height = lights[i].Height;
light_panels[i].Location = new Point(lights[i].X, lights[i].Y);
Controls.Add(light_panels[i]);
light_panels[i].Paint += PLight_Paint;
}
CenterToScreen();
}
private void PLight_Paint(object sender, PaintEventArgs e)
{
Panel p = sender as Panel;
int index = 0;
for (int i = 0; i < light_panels.Length; ++i)
{
if (Object.ReferenceEquals(light_panels[i], p))
{
index = i;
break;
}
}
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.FillRectangle(new SolidBrush(Color.Gray), lights[index].X, lights[index].Y, lights[index].Width, lights[index].Height);
switch (lights[index].State)
{
case 0:
e.Graphics.FillEllipse(new SolidBrush(Color.Red), lights[index].X + 10, lights[index].Y + 5, lights[index].Width - 30, lights[index].Height / 3 - 10);
e.Graphics.FillEllipse(new SolidBrush(Color.Black), lights[index].X + 10, lights[index].Y + lights[index].Height / 3, lights[index].Width - 30, lights[index].Height / 3 - 10);
e.Graphics.FillEllipse(new SolidBrush(Color.Black), lights[index].X + 10, lights[index].Y + lights[index].Height / 3 * 2 - 5, lights[index].Width - 30, lights[index].Height / 3 - 10);
break;
case 1:
e.Graphics.FillEllipse(new SolidBrush(Color.Red), lights[index].X + 10, lights[index].Y + 5, lights[index].Width - 30, lights[index].Height / 3 - 10);
e.Graphics.FillEllipse(new SolidBrush(Color.Orange), lights[index].X + 10, lights[index].Y + lights[index].Height / 3, lights[index].Width - 30, lights[index].Height / 3 - 10);
e.Graphics.FillEllipse(new SolidBrush(Color.Black), lights[index].X + 10, lights[index].Y + lights[index].Height / 3 * 2 - 5, lights[index].Width - 30, lights[index].Height / 3 - 10);
break;
case 2:
e.Graphics.FillEllipse(new SolidBrush(Color.Black), lights[index].X + 10, lights[index].Y + 5, lights[index].Width - 30, lights[index].Height / 3 - 10);
e.Graphics.FillEllipse(new SolidBrush(Color.Black), lights[index].X + 10, lights[index].Y + lights[index].Height / 3, lights[index].Width - 30, lights[index].Height / 3 - 10);
e.Graphics.FillEllipse(new SolidBrush(Color.Green), lights[index].X + 10, lights[index].Y + lights[index].Height / 3 * 2 - 5, lights[index].Width - 30, lights[index].Height / 3 - 10);
break;
case 3:
e.Graphics.FillEllipse(new SolidBrush(Color.Black), lights[index].X + 10, lights[index].Y + 5, lights[index].Width - 30, lights[index].Height / 3 - 10);
e.Graphics.FillEllipse(new SolidBrush(Color.Orange), lights[index].X + 10, lights[index].Y + lights[index].Height / 3, lights[index].Width - 30, lights[index].Height / 3 - 10);
e.Graphics.FillEllipse(new SolidBrush(Color.Black), lights[index].X + 10, lights[index].Y + lights[index].Height / 3 * 2 - 5, lights[index].Width - 30, lights[index].Height / 3 - 10);
break;
}
}
private void timer_Tick(object sender, EventArgs e)
{
for (int i = 0; i < lights.Length; ++i)
{
lights[i].Switch();
light_panels[i].Invalidate();
light_panels[i].Update();
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
Что я могу сделать, чтобы решить эту проблему?Спасибо за любую помощь