/// <summary> /// When the array is updated, enqueue the data for that update in <c>animationQueue</c>. /// </summary> /// <param name="src">Object that raised the event.</param> /// <param name="args">Contains the array to be drawn, and array indexes being compared.</param> public void OnArrayModified(object src, ArrayEventArgs args) { lock (queueLock) { animationQueue.Enqueue(args); } }
/// <summary> /// On a timer tick, dequeue from <c>animationQueue</c> and update current image based on the <c>ArrayEventArgs</c>. /// Redraw <c>visualizerPicBox</c>. /// </summary> /// <param name="src">Object that raised the event.</param> /// <param name="args">Additional arguments with the event.</param> private void animationTimerTick(Object src, EventArgs args) { lock (queueLock) { if (animationQueue.Count > 0) { ArrayEventArgs arrayArgs = animationQueue.Dequeue(); int[] arrayToDraw = arrayArgs.array; Graphics visualizerGraphics = Graphics.FromImage(currentImage); visualizerGraphics.Clear(Color.Empty); int canvasWidth = currentImage.Width; float barWidth = (float)canvasWidth / arrayToDraw.Length; float offset = (canvasWidth - arrayToDraw.Length * barWidth) / 2; // Centers the bars in the middle of the visualizer Pen pen = new Pen(Color.White, barWidth); for (int i = 0; i < arrayToDraw.Length; i++) { if (i == arrayArgs.currIndex) { pen.Color = Color.FromArgb(236, 78, 79); } else if (i == arrayArgs.checkedIndex) { pen.Color = Color.FromArgb(39, 185, 152); } else { pen.Color = Color.White; } visualizerGraphics.DrawLine(pen, offset + i * barWidth, currentImage.Height, offset + i * barWidth, currentImage.Height - arrayToDraw[i]); } visualizerGraphics.Dispose(); picBoxVisualizer.Refresh(); } } }