Drawing random colored Shapes in WPF.

You can quickly learn how to draw shapes in WPF. In the following article, let’s see how we can generate random shapes along with random colors and use Mouse Events to draw them on the XAML.

Create a Window. Let’s name it as ShapeWindow.xaml. Create a Canvas and name it 'OurCanvas'
Necessary comments have been provided for clear cut.

/// <summary>
/// Interaction logic for ShapeWindow.xaml
/// </summary>
public partial class ShapeWindow : Window
{



#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="ShapeWindow"/> class.
/// </summary>
public ShapeWindow()
{
      InitializeComponent();
}

#endregion Constructor

#region Members

private static Brush[] someBrushes;
private static Dictionary<Brush, string> brushCollection;
private static readonly Random rand = new Random();

#endregion Members


/// <summary>
/// Handles the MouseMove event of the Window control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Input.MouseEventArgs"/> instance containing the event data.</param>
private void Window_MouseMove(object sender, MouseEventArgs e)
{
   // Capture the Point
    Point pt = e.GetPosition(ourCanvas);
     //Clear if children of the canvas crosses a certain number
    if (ourCanvas.Children.Count > 100)
       ourCanvas.Children.Clear();
    //Generate a Random shape with a random color and add it to the Canvas.
    DrawShape(pt);
    //Force Capture the movement of the mouse
    CaptureMouse();
}

/// <summary>
/// Draws the shape.
/// </summary>
/// <param name="p">The p.</param>
private void DrawShape(Point p)
{
   Shape shape = new System.Windows.Shapes.Rectangle
    {
      Stroke              = SystemColors.WindowTextBrush,
      StrokeThickness     = 3,
      Fill                = GetRandomBrush(),
      //Remove the height to see horizantal shape
      Height    = 50,
      //Remove the width to see vertical shape
      Width   = 50
       //Remove both to see a point. :-)  Creates a Paint Program!
    };
    
      ourCanvas.Children.Add(shape);
      Canvas.SetLeft(shape, p.X - 25);
      Canvas.SetTop(shape, p.Y - 25);
  }

  /// <summary>
  /// Gets the random brush.
  /// </summary>
  /// <returns></returns>
  public static Brush GetRandomBrush()
  {
     brushCollection = new Dictionary<Brush, string> { { Brushes.Red, "Red" },
                                  { Brushes.Blue, "Blue" },
                                  { Brushes.Yellow, "Yellow" },
                                  { Brushes.Green, "Green" },
                                  { Brushes.Purple, "Purple" },
                                  { Brushes.Pink, "Pink" },
                                  { Brushes.Orange, "Orange" },
                                  { Brushes.Tan, "Tan" },
                                  { Brushes.Gray, "Gray" }
                                 };


      someBrushes     = new Brush[brushCollection.Count];
      brushCollection.Keys.CopyTo(someBrushes, 0);

      return someBrushes[rand.Next(0, someBrushes.Length)];
   }
    
}
By [)ia6l0 iii   Popularity  (3875 Views)