/** (C) 1999 World Xiangqi League, Confidential, All Rights Reserved */

import java.awt.*;

public class ProgressBar extends Frame {
  private BootApplet boot;
  private              int count;
  private              int max;
  private static final int FrameBottom = 24;
  private Font BARFONT = new Font("Helvetica",Font.BOLD,18);  

  public ProgressBar (String title, int ticks, BootApplet boot) {
    super(title);
    count = 0;
    max = ticks;
    this.boot = boot;
    setResizable(false);	// else too painful
    setLayout(null);
    addNotify();
    resize (insets().left + insets().right + 379,
	    insets().top + insets().bottom + FrameBottom);
  }

  public synchronized void show() {
    move(125, 150);
    super.show();
  }

  public void tick(String desc) {
    //boot.status("ProgressBar: "+desc);
    if (++count == max)
      dispose();
    else
      repaint();
  }

  public void paint (Graphics g) {
    Dimension dim  = size();
    double    PercentComplete = (double)count * 100.0 /(double)max;
    int       BarPixelWidth   = (dim.width * count)/ max;

    g.setColor(Color.red);
    g.fillRect(0, 0, BarPixelWidth, dim.height);

    String s = String.valueOf((int)PercentComplete) + " %";
    g.setColor(Color.black);
    //g.setFont(BARFONT);    

    FontMetrics fm = g.getFontMetrics(g.getFont());
    int stringPixelWidth = fm.stringWidth(s);
    //System.out.println(dim.height+" "+fm.getHeight());    
    //g.drawString(s, (dim.width - stringPixelWidth)/2,
    //(dim.height + fm.getHeight())/2);
    g.drawString(s, (dim.width -stringPixelWidth)/2, FrameBottom-4);
  }

  public boolean handleEvent(Event event) {
    if (event.id == Event.WINDOW_DESTROY) {
      dispose();
      return true;
    }
    return super.handleEvent(event);
  }
}
