Get the latest Education e-news

Go Back   Game Career Guide Forums > Programming
Forum Home Register Members List Mark Forums Read

Thread Tools Display Modes
Prev Previous Post   Next Post Next
Old 10-28-2011, 12:54 AM   #1
Junior Member

Activity Longevity
0/20 11/20
Today Posts
0/11 ssssssss1
Default Animation in Java applets

Basic animation techniques

Many forms of animation are possible in Java. What all of them have in common is that they create some kind of motion on the screen by drawing successive frames at a relatively high speed (usually about 10-20 times per second).

We will start by creating a simple template applet for doing animations and slowly elaborate it until we arrive at a fairly complete applet.

Using a thread

To update the screen multiple times per second, you need to create a new Java thread that contains an animation loop. The animation loop is responsible for keeping track of the current frame and for requesting periodic screen updates. To implement a thread, you must either create a subclass of Thread or adhere to the Runnable interface.

A common mistake is to put the animation loop in the paint() method of an applet. Doing so will have strange side effects because it holds up the main AWT thread, which is in charge of all drawing and event handling.

As an example I have written a small template applet, called Example1Applet, that illustrates the general outline of an animation applet. Example1Applet shows how to create a thread and call the repaint() method at fixed intervals. The number of frames per second is specified by passing in an applet parameter. Here is an example of what you would put in your HTML document:

<applet code=Example1Applet.class width=200 height=200>
<param name=fps value=20>
julianak is offline  

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Powered by vBulletin® Version 3.6.9
Copyright ©2000 - 2016, Jelsoft Enterprises Ltd.
All times are GMT -8. The time now is 07:31 PM.

UBM Tech