Share
Explore BrainMass

JAVA:An Applet with an animation thread

(See attached file for full problem description)

An Applet with an animation thread : java.awt classes
Juggler applet displayed by appletviewer

The Applet displays a simple animation of a juggler. The animation works with a thread that runs the following code:

forever
sleep a bit
update the display
change to next juggler in sequence
of images
move all balls one step around a
fixed path
request a repaint of the display

define a class JugglerApplet that:
1. extends the standard java.applet.Applet class
2. listens for action-events and item-selection events
3. is a "Runnable" i.e. has a public void run() method used by a thread
4. its init function will:
o construct the interface with a specialized subclass of java.awt.Canvas taking up the main part of the display, and a panel containing a label, a Choice control and an action button
 the Choice control offers a number of options for juggling skill, these control the speed and degree of elaboration of the animation.
 the action button (whose label will change from "start" to "stop" as approrpriate) is used to start/stop the animation. The control will be achieved using the resume and suspend methods of a Thread (these are "deprecated" because they are potentially sources of coordination bugs, however their use in this simple application is safe).
 your Applet will arrange to listen to its controls.
o load all images that it needs (it is simplest to use a series of photo or cartoon images - GIF or JPEG - to represent the figure of the juggler in different postures); the image files should be in the same directory as the .class file for the Applet or in a subdirectory of that directory
o create and start an animation thread (which should suspend itself once all animation variables have been initialized and wait for re-activation via the action button
5. have methods for handling events from the Choice and action button
o selection of different performance controls via the choice will change the speed of the animation and the complexity (i.e. number of items being juggled); the changes have immediate effect if the animation is running
o the action button suspends or resumes the animation
6. have a run function implementing the pseudo-code outlined above
7. have function members and data members that record the state of the animation and which can be called to draw a picture (optionally, these may be moved to another class; your applet will then work with an instance of that class)
o there will be an array of images of the juggler in various postures and a counter that defines the next to be drawn (working cyclically through the images)
o there will be one or more Point[] containing point coordinates that define the path of the items juggled
o there will be data defining the current path locations of the various items juggled
o there will be a function that advances the juggled items along their paths
o there will be a function that draws the overall image comprising juggler and juggled items
In addition to your Applet (and any auxiliary data class that you may wish to define) you will need a "MyCanvas" class. You should modify its update function to use double buffering (see examples in lecture notes) as otherwise the display will flicker in an unattractive way. Its paint function should invoke the function that draws the image of your juggler and juggled items.

Attachments

Solution Summary

An Applet with an animation thread for JAVA is examined.

$2.19