summaryrefslogtreecommitdiff
path: root/java/gjt/Bargauge.java
blob: 27be8afad7b7680fa1e6242848b6243b5de24d25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package gjt;

import java.awt.*;

/**
 * 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 {
    private double          percentFill = 0;
    private ThreeDRectangle border = new ThreeDRectangle(this);
    private Color           fillColor;

    public Bargauge(Color fillColor) {
        setFillColor(fillColor);
    }
    public void setFillColor(Color fillColor) {
        this.fillColor = fillColor;
    }
    public void setFillPercent(double percentage) {
        Assert.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() * 3;
        return new Dimension(w, w*4);
    }
    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 *= percentFill/100;
            else                   fillh *= 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;
    }
}