home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 2.8 KB | 161 lines |
-
- import java.applet.*;
- import java.awt.*;
- import java.net.*;
-
- /*
- * the applet class
- */
- public class BallSpin extends Applet implements Runnable {
-
- /*
- * the number of sub-images in the entire image
- */
- int nImages;
-
- /*
- * the full-size image
- */
- Image theImage;
-
- /*
- * the animation thread
- */
- Thread thread;
-
- /*
- * the position of the displayed image
- */
- int xOffset;
- int yOffset;
-
- /*
- * offscreen image for double-buffering
- */
- Image offImage;
-
- /*
- * graphics object associated with the offscreen image
- */
- Graphics offg;
-
- /*
- * dimension of the offscreen image
- */
- Dimension offSize;
-
- /*
- * the sub-image to be drawn
- */
- int whichImage;
-
- /*
- * the dimension of each sub-image
- */
- Dimension imageDim = new Dimension ();
-
- /*
- * Initializes the applet
- * Loads the entire image
- */
- public void init () {
-
- int i;
-
- thread = null;
-
- offSize = getSize ();
- offImage = createImage (offSize.width, offSize.height);
- offg = offImage.getGraphics ();
-
- nImages = 7;
- imageDim.width = 48;
- imageDim.height = 53;
-
- MediaTracker tracker = new MediaTracker (this);
-
- String name = "ballspin.gif";
- theImage = getImage (getDocumentBase(), name);
-
- tracker.addImage (theImage, 100);
- showStatus ("Getting image: "+name);
- try {
- tracker.waitForID (100);
- } catch (InterruptedException e) { }
-
- xOffset = nImages * 30;
- yOffset = 3 + offSize.height - imageDim.height;
- whichImage = nImages;
- repaint ();
- thread = new Thread (this);
- thread.start ();
- }
-
- /*
- * Start the animation
- */
- public void start () {
-
- xOffset = nImages * 30;
- yOffset = 3 + offSize.height - imageDim.height;
- whichImage = nImages;
- if (thread != null)
- thread = null;
- thread = new Thread (this);
- thread.start ();
- }
-
- /*
- * Stop the animation
- */
- public void stop ()
- {
- if (thread != null)
- thread = null;
- }
-
- /**
- * call update for efficiency
- * @param g - destination graphics object
- */
- public void paint (Graphics g) {
-
- update (g);
- }
-
- /**
- * draw the sub-image at xOffset, yOffset
- * @param g - destination graphics object
- */
- public void update (Graphics g) {
-
- Graphics offgClip;
-
- offg.setColor (Color.white);
- offg.fillRect (0, 0, offSize.width, offSize.height);
- offgClip = offg.create (xOffset, yOffset, imageDim.width,
- imageDim.height);
- offgClip.drawImage (theImage, 0 - (whichImage * imageDim.width),
- 0, this);
- g.drawImage (offImage, 0, 0, this);
- }
-
- /*
- * Animation thread forces repaint every 100ms
- */
- public void run () {
-
- int i;
-
- for (i=0; i<nImages; i+=1) {
- xOffset -= 30;
- whichImage -= 1;
- repaint ();
- try {
- Thread.sleep (100);
- } catch (InterruptedException e) { }
- }
- }
- }
-
-