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;

using System.Drawing.Drawing2D;

namespace BouncingBalls
{
    public partial class frm28BouncingBalls : Form
    {
        public frm28BouncingBalls()
        {
            InitializeComponent();
        }

        // Used to generate random balls and colors.
        private Random Rand = new Random();

        // The balls.
        private List<Ball> Balls = null;

        // Make the balls and start animating them.
        private void goButton_Click(object sender, EventArgs e)
        {
            int width = pictureBox1.ClientSize.Width;
            int height = pictureBox1.ClientSize.Height;

            Balls = new List<Ball>();
            int numballs = int.Parse(numBallsTextBox.Text);
            Pen pen = new Pen(Color.Black, 3);
            for (int i = 0; i < numballs; i++)
            {
                int diameter = Rand.Next(10, 50);
                int x = Rand.Next(0, width - diameter);
                int y = Rand.Next(0, height - diameter);
                int vx = Rand.Next(-5, 5);
                int vy = Rand.Next(-5, 5);
                Brush brush = RandomBrush();
                Balls.Add(new Ball(x, y, diameter, diameter, vx, vy, brush, pen));
            }

            // Start the timer.
            moveBallTimer.Enabled = true;
        }

        // Return a random brush.
        private Brush RandomBrush()
        {
            Brush[] brushes = { Brushes.Red, Brushes.Orange, Brushes.Yellow,
                Brushes.Green, Brushes.Lime, Brushes.Blue, Brushes.White, Brushes.LightBlue,
                Brushes.LightGreen, Brushes.Purple, Brushes.Pink };
            return brushes[Rand.Next(0, brushes.Length)];
        }

        // Move the balls.
        private void moveBallTimer_Tick(object sender, EventArgs e)
        {
            // Update the ball positions.
            int xmax = pictureBox1.ClientSize.Width;
            int ymax = pictureBox1.ClientSize.Height;
            foreach (Ball ball in Balls) ball.Move(xmax, ymax);

            // Draw the balls.
            pictureBox1.Refresh();
        }

        // Draw the balls on this PictureBox.
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (Balls == null) return;

            e.Graphics.Clear(pictureBox1.BackColor);
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            foreach (Ball ball in Balls) ball.Draw(e.Graphics);
        }


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.label1 = new System.Windows.Forms.Label();
            this.numBallsTextBox = new System.Windows.Forms.TextBox();
            this.goButton = new System.Windows.Forms.Button();
            this.moveBallTimer = new System.Windows.Forms.Timer(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.pictureBox1.BackColor = System.Drawing.Color.White;
            this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.pictureBox1.Location = new System.Drawing.Point(12, 41);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(260, 208);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(9, 17);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(42, 13);
            this.label1.TabIndex = 1;
            this.label1.Text = "# Balls:";
            // 
            // numBallsTextBox
            // 
            this.numBallsTextBox.Location = new System.Drawing.Point(57, 14);
            this.numBallsTextBox.Name = "numBallsTextBox";
            this.numBallsTextBox.Size = new System.Drawing.Size(54, 20);
            this.numBallsTextBox.TabIndex = 2;
            this.numBallsTextBox.Text = "10";
            this.numBallsTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // goButton
            // 
            this.goButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.goButton.Location = new System.Drawing.Point(197, 12);
            this.goButton.Name = "goButton";
            this.goButton.Size = new System.Drawing.Size(75, 23);
            this.goButton.TabIndex = 3;
            this.goButton.Text = "Go";
            this.goButton.UseVisualStyleBackColor = true;
            this.goButton.Click += new System.EventHandler(this.goButton_Click);
            // 
            // moveBallTimer
            // 
            this.moveBallTimer.Interval = 50;
            this.moveBallTimer.Tick += new System.EventHandler(this.moveBallTimer_Tick);
            // 
            // frm28BouncingBalls
            // 
            this.AcceptButton = this.goButton;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.goButton);
            this.Controls.Add(this.numBallsTextBox);
            this.Controls.Add(this.label1);
            this.Name = "frm28BouncingBalls";
            this.Text = "BouncingBalls";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox numBallsTextBox;
        private System.Windows.Forms.Button goButton;
        private System.Windows.Forms.Timer moveBallTimer;
    }
}