home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 May / comcd0502.iso / homepage / javaspecial / 03_01 / sitesearcher / AdvSiteSearcher / ProgressBar.java < prev    next >
Encoding:
Java Source  |  2000-08-18  |  1.8 KB  |  67 lines

  1. //AdvSiteSearcher c1999 The Gilbert Post by David Faden
  2. //The applet and code are distributed as linkware...
  3. //If you use this applet or a variant on its code,
  4. //include a link to The Gilbert Post, 
  5. //http://www.geocities.com/Athens/Parthenon/1911
  6. //The Gilbert Post and David Faden take no responsibility
  7. //for anything bad that happens as a result of using this applet
  8. //or a derivative based on its code. USE AT YOUR OWN RISK. (big letters)
  9. //Please send reports of problems to gilbertnews@hotmail.com, anyway, though.
  10.  
  11. //begin ProgressBar.java
  12. import java.awt.*;
  13.  
  14. //Displays the percent of a task done
  15. class ProgressBar extends Canvas {
  16.   private Color oncolor;
  17.   private Color offcolor;
  18.   private String onmessage;
  19.   private int width;
  20.   private int height;
  21.   private int max;
  22.   private int currtotal=0;
  23.  
  24.   public ProgressBar(Color oncolor,Color offcolor,int width,int height,int max,String onmessage) {
  25.     this.oncolor=oncolor;
  26.     this.offcolor=offcolor;
  27.     this.width=width;
  28.     this.height=height;
  29.     this.max=max;
  30.     this.onmessage=onmessage;
  31.     resize(width,height);
  32.   }
  33.   
  34.   //Assuming i is positive
  35.   public int plus(int i) {
  36.     if(currtotal<max) currtotal+=i;
  37.     repaint();
  38.     return currtotal;
  39.   }
  40.   
  41.   public void reset() {
  42.     currtotal=0;
  43.     repaint();
  44.   }
  45.   
  46.   public void setMax(int i) {
  47.     max=i;
  48.     repaint();
  49.   }
  50.   
  51.   public void paint(Graphics g) {
  52.     g.setColor(offcolor);
  53.     g.fillRect(0,0,width,height);
  54.     if(currtotal>0) {
  55.       double perc=(double)currtotal/(double)max;
  56.       if(perc>1.0D) perc=1.0D;
  57.       int drawwidth=(int)((double)(width-2)*perc);
  58.       g.setColor(oncolor);
  59.       g.fillRect(1,1,drawwidth,height-2);
  60.       int iperc=(int)(100.0*perc);
  61.       g.setColor(Color.black);
  62.       g.drawString(onmessage+" "+iperc+"%",4,height-5);
  63.     }
  64.   }
  65.   
  66. }//end ProgressBar.java
  67.