/// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            TargetElapsedTime = TimeSpan.FromSeconds(1.0f / 60.0f);

            InitializeClient();

            // Initialize the random number generator
            random = new Random(DateTime.Now.Millisecond);

            connectForm.Show();

            // Allow the user to see their mouse
            IsMouseVisible = true;

            oldMouseState    = Mouse.GetState();
            oldKeyboardState = Keyboard.GetState();

            MainColorDelegate      = new ColorDelegate(theColor => { paintBrush.brushColor = theColor; });
            SecondaryColorDelegate = new ColorDelegate(theColor => { eraseBrush.brushColor = theColor; });
            ResetColorDelegate     = new ColorDelegate(InitializePaintImage);

            outputForm = new OutputForm();

            // Set the brush color to black
            paintBrush = new PaintBrush(Color.Black, 255, 1.0f, 1.0f, BrushType.circle);
            eraseBrush = new PaintBrush(Color.White, 255, 1.0f, 1.0f, BrushType.circle);

            // Initialize the paint image
            InitializePaintImage(Color.White);

            // Initialize the paint pallet
            InitializePaintPallet();

            base.Initialize();
        }
        // Output text to the console
        private void OutputText(string text)
        {
            if (outputForm == null || outputForm.IsDisposed)
            {
                outputForm = new OutputForm();
            }

            outputForm.OutputText(text);
        }
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            try
            {
                MouseState    currentMouseState    = Mouse.GetState();
                KeyboardState currentKeyboardState = Keyboard.GetState();

                HandleIM();

                // Make the brush more opaque
                if (currentKeyboardState.IsKeyDown(Keys.Add))
                {
                    paintBrush.SetAlpha((byte)(paintBrush.brushColor.A + 1));
                }

                // Make the brush more transparent
                if (currentKeyboardState.IsKeyDown(Keys.Subtract))
                {
                    paintBrush.SetAlpha((byte)(paintBrush.brushColor.A - 1));
                }

                // Set the alpha of the brush to 255 (opaque)
                if (currentKeyboardState.IsKeyDown(Keys.PageUp))
                {
                    paintBrush.SetAlpha(255);
                }

                // Set the alpha of the brush to 0 (transparent)
                if (currentKeyboardState.IsKeyDown(Keys.PageDown))
                {
                    paintBrush.SetAlpha(0);
                }

                // Set the brush color the the color of the pixel under the mouse
                if (currentKeyboardState.IsKeyDown(Keys.P) && oldKeyboardState.IsKeyUp(Keys.P))
                {
                    paintBrush.brushColor = paintImage.GetPixelColor(new Vector2(currentMouseState.Position.X, currentMouseState.Position.Y));
                }

                // Open color picker dialog for main brush
                if (currentKeyboardState.IsKeyDown(Keys.C) && oldKeyboardState.IsKeyUp(Keys.C))
                {
                    Program.ShowMainColorDialog();
                }

                // Open color picker dialog for eraser
                if (currentKeyboardState.IsKeyDown(Keys.E) && oldKeyboardState.IsKeyUp(Keys.E))
                {
                    Program.ShowSecondaryColorDialog();
                }

                // Toggle the output form
                if (currentKeyboardState.IsKeyDown(Keys.OemTilde) && oldKeyboardState.IsKeyUp(Keys.OemTilde))
                {
                    if (outputForm.IsDisposed || outputForm == null)
                    {
                        outputForm = new OutputForm();
                    }

                    if (!outputForm.Visible)
                    {
                        outputForm.Show();
                    }
                    else
                    {
                        outputForm.Hide();
                    }
                }

                // Change the brush size using the scroll wheel
                int dV = (currentMouseState.ScrollWheelValue - oldMouseState.ScrollWheelValue) / 100;
                paintBrush.brushSize = (paintBrush.brushSize + dV < 1.0f ? 1.0f : (paintBrush.brushSize + dV > 25.0f ? 25.0f : paintBrush.brushSize + dV));
                eraseBrush.brushSize = paintBrush.brushSize;

                // Change between square and circle brushes
                if (currentKeyboardState.IsKeyDown(Keys.B) && oldKeyboardState.IsKeyUp(Keys.B))
                {
                    if (paintBrush.brushType == BrushType.circle)
                    {
                        paintBrush.brushType = BrushType.square;
                        eraseBrush.brushType = BrushType.square;
                    }
                    else
                    {
                        paintBrush.brushType = BrushType.circle;
                        eraseBrush.brushType = BrushType.circle;
                    }
                }

                // If the user is clicking, draw stuff!
                if (currentMouseState.LeftButton == ButtonState.Pressed)
                {
                    paintPallet.PaintLine(oldMouseState.Position.ToVector2(), currentMouseState.Position.ToVector2(), paintBrush);
                    DrawLine(oldMouseState.Position.ToVector2(), currentMouseState.Position.ToVector2(), paintBrush);
                }
                else if (currentMouseState.RightButton == ButtonState.Pressed)
                {
                    paintPallet.PaintLine(oldMouseState.Position.ToVector2(), currentMouseState.Position.ToVector2(), eraseBrush);
                    DrawLine(oldMouseState.Position.ToVector2(), currentMouseState.Position.ToVector2(), eraseBrush);
                }

                // Draw some kind of strange circle or square
                if (currentKeyboardState.IsKeyDown(Keys.W) && oldKeyboardState.IsKeyUp(Keys.W))
                {
                    paintPallet.PaintStrangeShape(currentMouseState.Position.ToVector2(), paintBrush, eraseBrush, DateTime.Now.Millisecond, false);
                    DrawStrangeShape(currentMouseState.Position.ToVector2(), paintBrush, eraseBrush, DateTime.Now.Millisecond, false);
                }

                // Draw some kind of strange circle and square combo
                if (currentKeyboardState.IsKeyDown(Keys.S) && oldKeyboardState.IsKeyUp(Keys.S))
                {
                    paintPallet.PaintStrangeShape(currentMouseState.Position.ToVector2(), paintBrush, eraseBrush, DateTime.Now.Millisecond, true);
                    DrawStrangeShape(currentMouseState.Position.ToVector2(), paintBrush, eraseBrush, DateTime.Now.Millisecond, true);
                }

                // Update all of the buttons in the toolbar
                toolBar.Update(currentMouseState, oldMouseState);

                oldMouseState    = currentMouseState;
                oldKeyboardState = currentKeyboardState;
            }
            catch (Exception e)
            {
                OutputText("Exception: " + e.Message);
            }


            base.Update(gameTime);
        }