The first animation we will consider is a scrolling marquee. The key section of code that accomplishes the animation needs to do three things. Draw the string, wait for some length of time and then change the location where the string is to be drawn to the left one pixel. Repeating this process will appear to animate the string from right to left. The code below seems, at first glance, to accomplish the task.
void paint (Graphics g)
{
while (true)
{
sleep(10); // Wait between moves
g.drawstring ( theString, Xposition, Yposition); // Draw the
string
Xposition--; // Adjust the position one pixel to the left.
}
}
It would be nice if a simple program like the one above would create animations but unfortunately it won't work. Control is tied up in the paint method. It executes once and never returns. Even if the program above could be somehow made to work it would take up all the resources available to the run time package and prevent any other applets from executing. As you can see there are three applets running to produce animations on this page. They can all seemingly run at once by using the Java thread class. A thread object is created for each applet that holds the state of the system when the applet doesn't have control (while its 'sleep'ing). Each applet can then draw the graphic required and then go to 'sleep' and allow the other applet's to execute. The processor is executing one applet at a time but it occurs so quickly that as it cycles or multithreads through the applets they all seem to run simultaneously.
Java animations that call the paint( ) and repaint( ) methods will require the use of threads to control the execution and allow multiple animations to occur. Up to this point we have written fairly simple applets that demonstrate a single capability of the language. In order to create animations we will have to begin discussing Java classes and methods of overriding them. We will need to create a runnable multithreading application using the thread class. It really isn't as complicated as it sounds and it yields the results you will find on this page. Each animation runs independently in the Java run time module of your browser. Fortunately we can use the final code covered in this section as a template or 'boilerplate' for writing animations.
The applet below uses multithreading to animate the text. We will cover the code to demonstrate the techniques involved and gain an understanding of animation but, as you probably have noticed, there is a problem with it. It flickers badly. This is caused by the Java screen update method. There are times when the applet is in the middle of updating the graphic window and the monitor refreshes the screen. The background color is displayed for an instant. Your eye sees this as a flicker.
| The Code & how it Works |
The animation below is much better. All changes
(animations) are made to a copy of the graphic screen in memory.
This is copied from memory to the graphic area below. This
buffered version of creating animations will produce virtually
flicker free animations.
| The Code & how it Works |
The code for the buffered version can be used to produce animations of all types. By flipping through graphics files, either GIF's or JPG's, you can produce motion pictures. By drawing circles, lines or rectangles you can generate animations that are created 'on the fly' and don't require external files. Sounds can be incorporated by including the AU files covered in the last section. Using these techniques the Java programmer gains some very powerful capabilities not available in other web development tools.
Go to the next topic: Event Driven Concepts