home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-05-21 | 1.7 KB | 75 lines |
- // Sign.java - Swinging sign object
- //
- // Copyright (C) 1996 by Dale Gass
- // Non-exclusive license granted to MKS, Inc.
- //
-
- import java.lang.*;
- import java.util.*;
- import java.awt.*;
- import java.net.*;
- import java.applet.*;
-
- // Sign - Swinging sign object
-
- public class Sign extends Canvas implements Runnable {
- Thread me; // My thread
- Image images[]; // Image list
- int which, steps; // Current image, number of images
- Applet app; // Parent applet
- int dir; // Current direction of swing (-1 or 1)
- Dimension dim; // Size
-
- // Constructor - load images, start thread
-
- public Sign(Applet a, Dimension d, String file, int nsteps) {
- app = a;
- dim = d;
- images = new Image[nsteps];
- for (int i = 0; i<nsteps; i++)
- images[i] = app.getImage(app.getCodeBase(), file + i + ".gif");
- (me = new Thread(this)).start();
-
- which = 0;
- dir = 1;
- steps = nsteps;
- resize(dim);
- }
-
- // Advisory for layout manager
-
- public Dimension preferredSize() { return dim; }
- public Dimension minimumSize() { return dim; }
-
- // Draw the current frame
-
- public void paint(Graphics g) {
- g.drawImage(images[which], 0, 0, app);
- }
-
- // Override update() method, to avoid painting to background colour
- // (avoids flicker).
-
- public void update(Graphics g) {
- paint(g);
- }
-
- // Main thread; delay and update frame count, repainting
-
- public void run() {
- while (true) {
- try { me.sleep(100); } catch (Exception e) { }
- which += dir;
- if (which == steps) {
- dir = -1;
- which = steps-1;
- } else if (which == -1) {
- dir = 1;
- which = 1;
- }
- repaint();
- }
- }
- }
-
-