home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1999 February / CDW0299.iso / Demos / Cafe / VCSAMPL.BIN / Spinner.java < prev    next >
Encoding:
Java Source  |  1997-03-05  |  2.5 KB  |  125 lines

  1. /*
  2.  * @(#)Spinner.java    1.2 97/01/14 Jeff Dinkins
  3.  *
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  */
  7.  
  8. import java.applet.*;
  9. import java.lang.*;
  10. import java.util.*;
  11. import java.awt.*;
  12.  
  13. /**
  14.  * Spinner - a class that creates a lightweight component that
  15.  * shows a spinning wheel.
  16.  *
  17.  * Lightweight components can have "transparent" areas, meaning that
  18.  * you can see the background of the container behind these areas.
  19.  *
  20.  */
  21. public class Spinner extends Component {
  22.  
  23.   float percentDone = 0;
  24.   int totalTicks    = 60;
  25.   int currentTick   = 0;
  26.   
  27.   SpinnerThread spinnerThread;
  28.   
  29.   /**
  30.    * Constructs a Spinner
  31.    */
  32.   public Spinner() {
  33.       setForeground(Color.gray);
  34.       setForeground(Color.lightGray);
  35.   }
  36.   
  37.   /**
  38.    * paints the Spinner
  39.    */
  40.   public void paint(Graphics g) {
  41.       int start_angle = 90;
  42.       int done_angle = (int) (percentDone * 360);
  43.       
  44.       g.setColor(getBackground());
  45.       g.fillArc(3, 3, getSize().width-8, getSize().height-8, 0, 360);
  46.       
  47.       g.setColor(getForeground());
  48.       g.fillArc(3, 3, getSize().width-8, getSize().height-8, start_angle, done_angle);
  49.  
  50.       g.setColor(Color.black);
  51.       g.drawArc(3, 3, getSize().width-8, getSize().height-8, 0, 360);
  52.   }
  53.  
  54.   public void setCurrentTick(int tick) {
  55.       currentTick = tick;
  56.  
  57.       if(currentTick > totalTicks) {
  58.       percentDone = 1;
  59.       } else if(currentTick == 0) {
  60.       percentDone = 0;
  61.       } else {
  62.       percentDone = (float) currentTick / (float) totalTicks;
  63.       }
  64.       
  65.       // Repaint might flicker a bit. To avoid this, you can use
  66.       // double buffering (see the Gauge example).
  67.       repaint();
  68.   }
  69.  
  70.   public void startSpinning() {
  71.       spinnerThread = new SpinnerThread(this);
  72.       spinnerThread.start();
  73.   }
  74.  
  75.   public void stopSpinning() {
  76.       spinnerThread.stop();
  77.       spinnerThread = null;
  78.   }
  79.  
  80.   public void setTotalTicks(int tick) {
  81.       totalTicks = tick;
  82.   }
  83.  
  84.   public int getTotalTicks() {
  85.       return totalTicks;
  86.   }
  87.  
  88.   public int getCurrentTick() {
  89.       return currentTick;
  90.   }
  91.  
  92.  
  93. }
  94.  
  95.  
  96.  
  97. /**
  98.  * SpinnerThread: spins the wheel
  99.  */
  100. class SpinnerThread extends Thread {
  101.  
  102.   Spinner spinner;
  103.  
  104.   SpinnerThread(Spinner spinner) {
  105.       super("Spinner Thread");
  106.       this.spinner = spinner;
  107.   }
  108.  
  109.   public void run () {
  110.       int i = spinner.getCurrentTick();
  111.       while(true) {
  112.       try {
  113.           while (i-- > 0) {
  114.           spinner.setCurrentTick(i);
  115.           sleep(100);
  116.           }
  117.       } catch (java.lang.InterruptedException e) {
  118.           // don't care if we are interrupted
  119.       }
  120.       i = spinner.getTotalTicks();
  121.       }
  122.   }
  123. }
  124.  
  125.