// Return a random color.
private Random rand = new Random();
private Color[] Colors =
{
Color.Red,
Color.Green,
Color.Blue,
Color.Lime,
Color.Orange,
Color.Fuchsia,
Color.Yellow,
Color.LightGreen,
Color.LightBlue,
Color.Cyan,
};
private Color RandomColor()
{
return Colors[rand.Next(0, Colors.Length)];
}
This code defines a Random object that it later uses to generate random numbers and an array holding the colors that it should use. The RandomColor method uses the Random object to generate a random index in the array and returns the corresponding color.
The program uses the RandomColor method to create brushes for filling the circles that it draws. For example, the following code shows how the program draws the large enclosing circle.
// Draw the big circle.
using (Brush the_brush = new SolidBrush(RandomColor()))
{
big_circle.Draw(gr, the_brush);
}
Comments