// AnimationDemoApplet.java (c) 2006 Kari Laitinen // http://www.naturalprogramming.com/ // 2006-10-04 File created. // 2008-02-03 Last modification. /* This Java applet 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 System.out.print() method which produce text lines into the command prompt window when this program is run with AppletViewer. By exploring these text lines you can find out how various methods are called when this program is executed. The init() method is called once when an applet starts executing. The stop() method is called always when the applet window is minimized. The start() method is called always when the applet window is exposed after it has been minimized. 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.) */ import java.awt.* ; import javax.swing.* ; public class AnimationDemoApplet extends JApplet implements Runnable { int applet_width, applet_height ; Thread animation_thread = null ; boolean thread_must_be_executed = false ; boolean ball_must_be_shown = true ; int ball_position_x, ball_position_y ; Color ball_color = Color.cyan ; public void init() { applet_width = getSize().width ; applet_height = getSize().height ; ball_position_x = applet_width / 2 - 40 ; ball_position_y = applet_height / 2 - 40 ; System.out.print( "\n Method init() executed. " ) ; } public void start() { if ( animation_thread == null ) { animation_thread = new Thread( this ) ; thread_must_be_executed = true ; animation_thread.start() ; } System.out.print( "\n Method start() executed. " ) ; } public void stop() { if ( animation_thread != null ) { animation_thread.interrupt() ; thread_must_be_executed = false ; animation_thread = null ; } System.out.print( "\n Method stop() executed. " ) ; } public void run() { System.out.print( "\n Method run() started." ) ; while ( thread_must_be_executed == true ) { System.out.print( " X " ) ; try { Thread.sleep( 1000 ) ; // Delay of 1000 milliseconds. } catch ( InterruptedException caught_exception ) { thread_must_be_executed = false ; } repaint() ; } System.out.print( "\n Method run() stopped. " ) ; } public void paint( Graphics graphics ) { super.paint( graphics ) ; if ( ball_must_be_shown == true ) { graphics.setColor( ball_color ) ; graphics.fillOval( ball_position_x, ball_position_y, 80, 80 ) ; ball_must_be_shown = false ; } else { ball_must_be_shown = true ; } } }