// AnimationDemoApplication.cs Copyright (c) Kari Laitinen // http://www.naturalprogramming.com/ // 2008-02-24 File created. // 2022-12-20 Tested in Developer Command Prompt for VS 2022. /* This C# GUI program demonstrates how a thread can be used to produce animation onto the screen. "Animation" in this program means that there is a blinking ball on the screen. There are calls to the Console.Write() method which produce text lines into the command prompt window when this program is run. By exploring these text lines you can find out how various methods are called when this program is executed. The OnDeactivate() method is called always when the window is minimized. The OnActivated() method is called always when the window is exposed after it has been minimized. Test also what happens when you let another window cover the window of this application. More notes at the end of this file. */ using System ; using System.Windows.Forms ; using System.Drawing ; using System.Threading ; class AnimationDemoForm : Form { const int WINDOW_WIDTH = 500 ; const int WINDOW_HEIGHT = 500 ; Thread animation_thread = null ; bool ball_must_be_shown = true ; int ball_position_x, ball_position_y ; Color ball_color = Color.Cyan ; public AnimationDemoForm() { Text = "DEMONSTRATING SIMPLE ANIMATION" ; Size = new Size( WINDOW_WIDTH, WINDOW_HEIGHT ) ; ball_position_x = WINDOW_WIDTH / 2 - 40 ; ball_position_y = WINDOW_HEIGHT / 2 - 40 ; Console.Write( "\n Constructor AnimationDemoForm() executed. " ) ; } protected override void OnActivated( EventArgs event_data ) { if ( animation_thread == null ) { ThreadStart method_to_run_animation = new ThreadStart( run_animation ) ; animation_thread = new Thread( method_to_run_animation ) ; animation_thread.Start() ; } Console.Write( "\n Method OnActivated() executed. " ) ; } protected override void OnDeactivate( EventArgs event_data ) { if ( animation_thread != null ) { animation_thread.Abort() ; animation_thread = null ; } Console.Write( "\n Method OnDeactivate() executed. " ) ; } public void run_animation() { Console.Write( "\n Method run_animation() started." ) ; // The following is an infinite loop that will be terminated // when the Abort() method is called for the Thread object. while ( true ) { Console.Write( " X " ) ; Thread.Sleep( 1000 ) ; // Delay of 1000 milliseconds. Invalidate() ; } } protected override void OnPaint( PaintEventArgs paint_data ) { Graphics graphics = paint_data.Graphics ; if ( ball_must_be_shown == true ) { graphics.FillEllipse( new SolidBrush( ball_color ), ball_position_x, ball_position_y, 80, 80 ) ; ball_must_be_shown = false ; } else { ball_must_be_shown = true ; } } } public class AnimationDemoApplication { static void Main() { Application.Run( new AnimationDemoForm() ) ; } } /* A NOTE RELATED TO HUMAN PHYSIOLOGY: This program can be used to demonstrate the strange behaviour of our sight system. If you watch the blinking ball from a short distance, you'll notice that when the ball disappears from the screen you see a kind of shadow of the ball. The color of the shadowish ball seems to be close to magenta, which is a kind of opposite color to cyan, the color of the 'real' ball. The reason for seeing this kind of shadow of the ball is in our eyes. Our sight system is such that the sight cells inside our eyes remember the object (i.e. the ball) that disappears, but they produce the opposite color for the disappeared object. (This nice feature in this program was discovered by professor Matti Weckström from the University of Oulu.) */