home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / VCSAMPL.BIN / Gauge.java < prev    next >
Text File  |  1997-03-05  |  2KB  |  80 lines

  1. /*
  2.  * @(#)Gauge.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.awt.*;
  9. import java.applet.*;
  10.  
  11. /*
  12.  * Gauge - a class that implements a lightweight component
  13.  * that can be used, for example, as a performance meter.
  14.  *
  15.  * Lightweight components can have "transparent" areas, meaning that
  16.  * you can see the background of the container behind these areas.
  17.  *
  18.  */
  19. public class Gauge extends Component {
  20.     
  21.   // the current and total amounts that the gauge reperesents
  22.   int current = 0;
  23.   int total = 100;
  24.  
  25.   // The preferred size of the gauge
  26.   int Height = 18;   // looks good
  27.   int Width  = 250;  // arbitrary 
  28.  
  29.   /**
  30.    * Constructs a Gauge
  31.    */
  32.   public Gauge() {
  33.       this(Color.lightGray);
  34.   }
  35.  
  36.   /**
  37.    * Constructs a that will be drawn uses the
  38.    * specified color.
  39.    *
  40.    * @gaugeColor the color of this Gauge
  41.    */
  42.   public Gauge(Color gaugeColor) {
  43.       setBackground(gaugeColor);
  44.   }
  45.  
  46.   public void paint(Graphics g) {
  47.       int barWidth = (int) (((float)current/(float)total) * getSize().width);
  48.       g.setColor(getBackground());
  49.       g.fill3DRect(0, 0, barWidth, getSize().height-2, true);
  50.   }
  51.  
  52.   public void setCurrentAmount(int Amount) {
  53.       current = Amount; 
  54.  
  55.       // make sure we don't go over total
  56.       if(current > 100)
  57.        current = 100;
  58.  
  59.       repaint();
  60.   }
  61.  
  62.   public int getCurrentAmount() {
  63.       return current;
  64.   }
  65.  
  66.   public int getTotalAmount() {
  67.       return total;
  68.   }
  69.  
  70.   public Dimension getPreferredSize() {
  71.       return new Dimension(Width, Height);
  72.   }
  73.  
  74.   public Dimension getMinimumSize() {
  75.       return new Dimension(Width, Height);
  76.   }
  77.  
  78. }
  79.  
  80.