Java Reference
In-Depth Information
to make use of an object of class Timer (a Swing class). Since the reader may be
unfamiliar with aspects of this technique, a little time will be taken to explain the
basic steps…
The Timer object takes two arguments:
an integer delay (in milliseconds);
a reference to an ActionListener .
The Timer object automatically calls method actionPerformed at intervals
prescribed by the above delay. The ActionListener can conveniently be the application
container itself, of course. Inside the actionPerformed method will simply be a call
to repaint , which automatically calls method paint (which cannot itself be called
directly). Each time that paint is called, it will display the next image from the
sequence of frames making up the animation. Inbuilt methods start and stop will be
called to start/stop the Timer .
The bean application class upon which the images will be displayed will extend
class JPanel . The images themselves will be held in ImageIcon s and paint will
display an individual image by calling ImageIcon method paintIcon on an individual
image. This method takes four arguments:
￿ a reference to the component upon which the image will be displayed (usually
this , for the application container);
￿ a reference to the Graphics object used to render this image (provided by the
argument to paint );
￿ the x-coordinate of the upper-left corner of the image's display position;
￿ the y-coordinate of the upper-left corner of the image's display position.
There is one fi nal point to note before we look at the code for this example:
the BeanBox takes the size of the bean from method getPreferredSize of class
Componen t, which takes a Dimension argument specifying the container's width
and height.
Now for the code…
package animBeans;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AnimBean1 extends JPanel
implements ActionListener
private ImageIcon[] image;
private String imageName = "juggler";
The above string forms the fi rst part of the name for
Search WWH ::

Custom Search