import java.awt.*;
//import gjt.*;

/**
 * A bargauge which can be filled (wholly or partially) with a 
 * client-specified color.  Fill color is specified at 
 * construction time; both fill color and fill percent may be 
 * set after construction time.<p>
 *
 * @version 1.0, Apr 1 1996
 * @author  David Geary
 * @see     ThreeDRectangle
 * @see     gjt.test.BargaugeTest
 */
public class Bargauge extends Canvas {
    double          percentFill = 0;
    ThreeDRectangle border = new ThreeDRectangle(this);
    Color           fillColor;

    public Bargauge(Color fillColor) {
        setFillColor(fillColor);
    }
    public void setFillColor(Color fillColor) {
        this.fillColor = fillColor;
    }
    public void setFillPercent(double percentage) {
        MAssert.notFalse(percentage >= 0 && percentage <= 100);
        percentFill = percentage;
    }
    public void resize(int w, int h) { 
        reshape(location().x, location().y, w, h);
    }
    public void reshape(int x, int y, int w, int h) { 
        super.reshape(x,y,w,h);
        border.resize(w,h); 
    }
    public Dimension minimumSize() { return preferredSize(); }

    public Dimension preferredSize() {
        int w = border.getThickness() ;
        return new Dimension(w*30, w*30);
    }
    public void paint(Graphics g) {
        border.raise();
        border.paint();
        fill();
    }
    public void fill() {
        Graphics g = getGraphics();

        if((g != null) && (percentFill > 0)) {
            Rectangle b       = border.getInnerBounds();
            int       fillw   = b.width; 
            int       fillh   = b.height;

            if(b.width > b.height) fillw *= (int)percentFill/100;
            else                   fillh *= (int)percentFill/100;

            g.setColor(fillColor);
            border.clearInterior();

            if(b.width > b.height) 
                g.fillRect(b.x, b.y, fillw, b.height);
            else                   
                g.fillRect(b.x, b.y + b.height - fillh, 
                           b.width, fillh);
        }
    }
    protected String paramString() {
        Dimension size = size();
        Orientation orient = size.width > size.height ? 
                             Orientation.HORIZONTAL :
                             Orientation.VERTICAL;
        String    str  = "fill percent=" + percentFill + "," + 
                         "orientation="  + orient      + "," +
                         "color"         + fillColor;
        return str;
    }
}

