home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / Javacup / IN231VFD.TAR / internet / IN231VFD / Sign.java < prev    next >
Encoding:
Java Source  |  1996-05-21  |  1.7 KB  |  75 lines

  1. //    Sign.java - Swinging sign object
  2. //
  3. //    Copyright (C) 1996 by Dale Gass
  4. //    Non-exclusive license granted to MKS, Inc.
  5. //
  6.  
  7. import java.lang.*;
  8. import java.util.*;
  9. import java.awt.*;
  10. import java.net.*;
  11. import java.applet.*;
  12.  
  13. // Sign - Swinging sign object
  14.  
  15. public class Sign extends Canvas implements Runnable {
  16.     Thread me;        // My thread
  17.     Image images[];    // Image list
  18.     int which, steps;    // Current image, number of images
  19.     Applet app;        // Parent applet
  20.     int dir;        // Current direction of swing (-1 or 1)
  21.     Dimension dim;    // Size
  22.  
  23.     // Constructor - load images, start thread
  24.  
  25.     public Sign(Applet a, Dimension d, String file, int nsteps) {
  26.         app = a;
  27.     dim = d;
  28.     images = new Image[nsteps];
  29.     for (int i = 0; i<nsteps; i++)
  30.         images[i] = app.getImage(app.getCodeBase(), file + i + ".gif");
  31.     (me = new Thread(this)).start();
  32.  
  33.     which = 0;
  34.     dir = 1;
  35.     steps = nsteps;
  36.     resize(dim);
  37.     }
  38.  
  39.     // Advisory for layout manager
  40.  
  41.     public Dimension preferredSize() { return dim; }
  42.     public Dimension minimumSize()   { return dim; }
  43.  
  44.     // Draw the current frame
  45.  
  46.     public void paint(Graphics g) {
  47.     g.drawImage(images[which], 0, 0, app);
  48.     }
  49.  
  50.     // Override update() method, to avoid painting to background colour
  51.     // (avoids flicker).
  52.  
  53.     public void update(Graphics g) {
  54.     paint(g);
  55.     }
  56.  
  57.     // Main thread; delay and update frame count, repainting
  58.  
  59.     public void run() {
  60.         while (true) {
  61.         try { me.sleep(100); } catch (Exception e) { }
  62.             which += dir;
  63.         if (which == steps) {
  64.             dir = -1;
  65.             which = steps-1;
  66.         } else if (which == -1) {
  67.             dir = 1;
  68.             which = 1;
  69.         }
  70.         repaint();
  71.     }
  72.     }
  73. }
  74.  
  75.