HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Producing Animations with Java"
Vol. 1, Issue 2, P.32

	

Complete Listing

//-------------------------------------
//- Animate.java
//- Copyright 1996, All rights reserved
//- N-ARY Limited
//- Version: 1.0
//- Author: AR Williamson ( alan@n-ary.u-net.com )
//- Created: 2 June 1996
//- Web: http://www.u-net.com/~n-ary
//-------------------------------------

import	java.awt.*;
import	java.applet.*;
import	java.lang.*;

//-------

public class Animate extends Applet
{
	int SleepTime	= 200;

	public void init()
	{
		setLayout( new BorderLayout() );

		FlickBook	Fb	= new FlickBook( this, SleepTime );
		TimeControl	Tc	= new TimeControl( Fb, SleepTime );

		Fb.init();
		Fb.start();

		add( "Center", Fb );
      		add( "South", Tc );
	}

	//----

	public static void main( String args[] )
	{
		Frame	fram	= new Frame( "Animate" );
		Animate	am	= new Animate();

		am.init();
		am.start();

		fram.add( "Center", am );
		fram.resize( 200, 200 );
      		fram.show();
	}
}

//-------

class	FlickBook extends Panel implements Runnable
{
	Thread	Running = null;			//- Declare a handle to the current thread of execution
	int	SleepTime;						//- Time between image switches
	Applet		Parent;					//- Reference to the parent of class
	MediaTracker	TrackLoading;		//- Declare a reference to the class
	Image		Pictures[];					//- Somewhere to place the loaded images
	int		NoPictures = 10;			//- Lets assume we have 10 images named
												//- Img1.gif, Img2.gif, ... Img10.gif

	//----

	public FlickBook( Applet _Parent, int _SleepTime )
	{
		Parent		= _Parent;
		SleepTime	= _SleepTime;
	}

	//----

	public void init()
	{
		Pictures			= new Image[ NoPictures ];   		//- Declare an instance of the pictures
		TrackLoading	= new MediaTracker( Parent );		//- Declare an instance of the mediatracker
		vLoadImages();
		repaint();
	}

	//----

	public void	vLoadImages()
	{
		for ( int x=1; x <= NoPictures; x++ )
		{
			//- Initiate the loading process
			Parent.showStatus( "Loading the file: Img" + x + ".gif");
			Pictures[ x-1 ]	= Parent.getImage( Parent.getDocumentBase(), "Img" + x + ".gif" );

			//- Start the tracking of this image
			TrackLoading.addImage( Pictures[ x-1 ], 0 );

			try
			{
				TrackLoading.waitForAll();	//- Wait for the loading to be completed
			}
			catch ( InterruptedException e )
			{
				Parent.showStatus( "Img" + x + ".gif failed to load");
			}
		}
	}

	//----

	public void paint( Graphics g )
	{
		//- Draw the Image
		g.drawImage( Pictures[ CurrentImage ], 10, 10, this );
	}

	//----

	public void run()
	{
		Thread.currentThread().setPriority( Thread.NORM_PROIRITY - 1 );

		while ( true )
		{
			//- Increment the Image Index
			CurrentImage	= CurrentImage + 1;
			if ( CurrentImage == NoPictures )
				CurrentImage	= 0;

			//- Draw the image
			repaint();

			//- Sleep for a while
			try
			{
				Thread.sleep( SleepTime );
			}
			catch ( InterruptedException e )
			{
				break;
			}
		}
	}

	//----

	public void start()
	{
		if ( Running == null )
		{
			Running	= new Thread( this );
			Running.start();
		}
	}

	//----

	public void stop()
	{
		if ( Running != null )
		{
			Running.stop();
			Running	= null;
		}
	}

	//----

	public int IncSpeed()
	{
   	//- Use to control the speed of the animation
		SleepTime	= SleepTime - 50;
		if ( SleepTime < 0 )
		{
      	SleepTime = 20;
		}

		return SleepTime;
	}

	//----

	public int DecSpeed()
	{
		//- Use to control the speed of the animation
		SleepTime	= SleepTime + 50;

      return SleepTime;
	}
}

//-------

class TimeControl extends Panel
{
	Button		Slower;		//- Buttons for the faster and slower controls
	Button		Faster;
	Label			SpeedLabel;
	int			Speed;
	FlickBook	FBook;

	//----

	public	TimeControl( FlickBook _FBook, int _Speed )
	{
		FBook			= _FBook;
		Speed			= _Speed;

		setLayout( new FlowLayout() );

		Slower		= new Button( "<" );
		Faster		= new Button( ">" );
		SpeedLabel	= new Label( Speed );

		add( Slower );
		add( Faster );
		add( SpeedLabel );
	}

	//----

	public boolean action( Event _e, Object _arg )
	{
		//- This method handles the button presses
		if ( _e.target == Slower )
		{
			Speed	= FBook.DecSpeed();
		}
		else if ( _e.target == Faster )
		{
      	Speed	= FBook.IncSpeed();
		}

      SpeedLabel.setText( Speed );

		return true;
	}

}

//-------------
//- End of file
//------------


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: info@sys-con.com

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.