Java – Topic 32

(Animation)

 

 

LESSON NOTE

 

 

WHAT IS ANIMATION

 

Animation is simply the displaying of consecutive still images called frames.  Objects in each frame are slowly moving or changing.  The frames change quick enough that the eye is tricked into seeing movement as opposed to a series of still images.

 

In computer programs, animation is done in an animation loop.  Each iteration in the loop takes care of displaying one frame.

 

COMMON ERROR

 

In java, the paint method takes care of painting the screen.  A common error that many do (Mr. Campeau has done this too.) is to try to place a loop inside the paint method.  This does not work at all.

 

The update on the screen only occurs after the paint method is completely done.  Therefore, if you place a loop in it, it will simply slow down the painting.  If the loop is infinite, the program will hang.

 

So where do we place the animation loop?  Before answering that, we need to talk about threads and multi-threading.

 

THREADS & MULTI-THREADING

 

Before you worked with applets, all your programs were simply executed one statement at a time in a sequence.  However, sometimes it is necessary to have more than one sequence of code being executed at once.  Each sequence is called a thread. 

 

An examples of multithreading is seen in your operating system.  Your OS requires the use of multiple threads (multi-threading) to allow for multitasking.  In such a situation, each program has its own thread(s). 

 

When you started creating applets, you were using multiple threads without really knowing it.  You had your main thread that took care of your code but there was also a thread that took care of repainting your applet.

 

EXAMPLE 1

 

This program consists of two files.  It is not an applet and must therefore be executed without a web browser.

 

One file called CustomThread.java is used to create our very own class that extends the Thread class.  In the run method, the thread will continuously loop.  Inside the loop, the thread output a message and then sleeps for a random amount of time.

 

The second file is our tester program.  It creates three CustomThread objects and starts them all. The output shows how all three threads are running at once.

 

Files:

·        CustomThread.java

·        CustomThreadLauncher.java

·        Sample Output

·        All Files

 

EXAMPLE 2

 

This program consists of three files.  It is not an applet and must therefore be executed without a web browser.

 

The CustomThread2 class creates a thread that simply outputs when it is working or sleeping (same as previous example).  It also has a data field that is true when it is sleeping and false when it is awake.

 

The MonitorThread class monitors five CustomThread2 objects.  It simply continuously checks to see if all five are sleeping.

 

The CustomThreadLauncher class creates all the thread objects and starts them all.

 

Files:

·        CustomThread2.java

·        MonitorThread.java

·        CustomThreadLauncher.java

·        Sample Output

·        All files

 

THE RUNNABLE INTERFACE

 

In Example 2, you probably noticed that the MonitorThread needed the name of the five other threads so that it could monitor them (see constructor).  This sharing of information is important.  And, in big applications, this becomes a big job for the programmer.  In fact, in his university thesis, Mr. Campeau had to create his own special class to keep track of all the information that was required by all objects. 

 

The good news is that there is another way to share the information that is contained in a class (such as an applet) with threads.  This involves using the Runnable interface.  It makes a bit of a mess of the code but its very easy to do.  In fact, you get to go back to using a single file!

 

            The Runnable interface requires that we implement the method:

 

                        public void run()

 

We can then create a Thread and provide a Runnable object as an argument.  The thread will then use the run() method in the Runnable object as its own.  Of course, the run() method in the Runnable object has access to all other public data fields in that object. 

 

EXAMPLE OF ANIMATION

 

            This simple example of animation makes a circle move back and forth in an applet.  The delay in the run method specifies the amount of time in between the update of the animation frames.

 

            Files

·        HTML file (view the applet)

·        AnimationApplet.java

·        All files