home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch09 / BallSpin.java next >
Encoding:
Java Source  |  1998-12-14  |  2.8 KB  |  161 lines

  1.  
  2. import java.applet.*;
  3. import java.awt.*;
  4. import java.net.*;
  5.  
  6. /*
  7.  * the applet class
  8.   */
  9. public class BallSpin extends Applet implements Runnable {
  10.  
  11. /*
  12.  * the number of sub-images in the entire image
  13.  */
  14. int nImages;
  15.  
  16. /*
  17.  * the full-size image
  18.  */
  19. Image theImage;
  20.  
  21. /*
  22.  * the animation thread
  23.  */
  24. Thread thread;
  25.  
  26. /*
  27.  * the position of the displayed image
  28.  */
  29. int xOffset;
  30. int yOffset;
  31.  
  32. /*
  33.  * offscreen image for double-buffering
  34.  */
  35. Image offImage;
  36.  
  37. /*
  38.  * graphics object associated with the offscreen image
  39.  */
  40. Graphics offg;
  41.  
  42. /*
  43.  * dimension of the offscreen image
  44.  */
  45. Dimension offSize;
  46.  
  47. /*
  48.  * the sub-image to be drawn
  49.  */
  50. int whichImage;
  51.  
  52. /*
  53.  * the dimension of each sub-image
  54.  */
  55. Dimension imageDim = new Dimension ();
  56.  
  57. /*
  58.  * Initializes the applet
  59.  * Loads the entire image
  60.  */
  61. public void init () {
  62.  
  63.     int i;
  64.  
  65.     thread = null;
  66.  
  67.     offSize = getSize ();
  68.     offImage = createImage (offSize.width, offSize.height);
  69.     offg = offImage.getGraphics ();
  70.  
  71.     nImages = 7;
  72.     imageDim.width = 48;
  73.     imageDim.height = 53;
  74.  
  75.     MediaTracker tracker = new MediaTracker (this);
  76.  
  77.         String name = "ballspin.gif";
  78.     theImage = getImage (getDocumentBase(), name);
  79.  
  80.     tracker.addImage (theImage, 100);
  81.     showStatus ("Getting image: "+name);
  82.     try {
  83.         tracker.waitForID (100);
  84.     } catch (InterruptedException e) { }
  85.  
  86.     xOffset = nImages * 30;
  87.     yOffset = 3 + offSize.height - imageDim.height;
  88.     whichImage = nImages; 
  89.     repaint ();
  90.     thread = new Thread (this);
  91.     thread.start ();
  92. }
  93.  
  94. /*
  95.  * Start the animation
  96.  */
  97. public void start () {
  98.  
  99.     xOffset = nImages * 30;
  100.     yOffset = 3 + offSize.height - imageDim.height;
  101.     whichImage = nImages;
  102.     if (thread != null)
  103.        thread = null;
  104.     thread = new Thread (this);
  105.     thread.start ();
  106. }
  107.  
  108. /*
  109.  * Stop the animation
  110.  */
  111. public void stop ()
  112. {
  113.     if (thread != null)
  114.        thread = null;
  115. }
  116.  
  117. /**
  118.  * call update for efficiency
  119.  * @param g - destination graphics object
  120.  */
  121. public void paint (Graphics g) {
  122.  
  123.     update (g);
  124. }
  125.  
  126. /**
  127.  * draw the sub-image at xOffset, yOffset
  128.  * @param g - destination graphics object
  129.  */
  130. public void update (Graphics g) {
  131.  
  132.     Graphics offgClip;
  133.  
  134.     offg.setColor (Color.white);
  135.     offg.fillRect (0, 0, offSize.width, offSize.height);
  136.     offgClip = offg.create (xOffset, yOffset, imageDim.width,
  137.         imageDim.height);
  138.     offgClip.drawImage (theImage, 0 - (whichImage * imageDim.width),
  139. 0, this);
  140.     g.drawImage (offImage, 0, 0, this);
  141. }
  142.  
  143. /*
  144.  * Animation thread forces repaint every 100ms
  145.  */
  146. public void run () {
  147.  
  148.     int i;
  149.  
  150.     for (i=0; i<nImages; i+=1) {
  151.         xOffset -= 30;
  152.         whichImage -= 1;
  153.         repaint ();
  154.         try {
  155.             Thread.sleep (100);
  156.         } catch (InterruptedException e) { }
  157.     }
  158. }
  159. }
  160.  
  161.