Index
ProgressMeter.java


/*
 *  "COM.bensoft.widgets" Copyright (C) by Michael Benson - 7/27/97
 *
 *  "ProgressMeter" - a simple, but good enough progress meter.
 */

package COM.bensoft.widgets;

import java.awt.*;
import java.applet.Applet;
import java.util.*;
import java.io.*;
import java.net.*;


/**
 * ProgressMeter implements a simple, but effective, progress
 * meter (or "thermometer").
 *  
 * @author	Michael Benson
 */
public class ProgressMeter extends Canvas
{
	private static Color	_lowColor;
	private static Color	_hiColor;
	private static int		_minVal;
	private static int		_maxVal;
	private static int		_curVal;
	
	/**
	 * ProgressMeter constructor.
	 */
	public ProgressMeter()
	{
		_minVal = 0;
		_maxVal = 10;
		_curVal = 0;
		_lowColor = new Color(63, 63, 63);
		_hiColor = new Color(204, 204, 255);
	}

	/**
	 * Sets the upper and lower limits of the progress meter.
	 *
	 * @param	min		the lower limit.
	 * @param	max		the upper limit.
	 */
	public void setLimits (int min, int max)
	{
		_minVal = min;
		_maxVal = max;
		_curVal = _minVal;
		paint(getGraphics());
	}
	
	/**
	 * Sets the current value of the progress meter and redraws it.
	 * Insures the value is within the limits.
	 *
	 * @param	val		the current value.
	 */
	public void setValue (int val)
	{
		if (val >= _minVal && val <= _maxVal) {
			_curVal = val;
		} else {
			_curVal = _minVal;
		}
		paint(getGraphics());
	}
	
	/**
	 * Increments the current value of the progress meter and redraws it.
	 * Insures the value is within the limits.
	 *
	 * @param	i	the amount by which to increment.
	 */
	public void increment (int i)
	{
		_curVal += i;
		if (_curVal > _maxVal) {
			_curVal = _maxVal;
		} else if (_curVal < _minVal) {
			_curVal = _minVal;
		}
		paint(getGraphics());
	}
	
	/**
	 * Returns the current lower limit.
	 *
	 * @return	the lower limit.
	 */
	public int getMin()
	{
		return _minVal;
	}
	
	/**
	 * Returns the current upper limit.
	 *
	 * @return	the upper limit.
	 */
	public int getMax()
	{
		return _maxVal;
	}
	
	/**
	 * Returns the current value of the progress meter.
	 *
	 * @return	the current value.
	 */
	public int getValue()
	{
		return _curVal;
	}
	
	/**
	 * Overrides the action method in Canvas to redraw
	 * the progress meter.
	 *
	 * @param	g	the graphics context.
	 * @see		java.awt.Canvas#paint(Graphics g)
	 */
	public void paint (Graphics g)
	{
		int total = _maxVal - _minVal;
		int cursor = _curVal - _minVal;
		int midpoint = ((bounds().width) * cursor) / total;
		g.setColor(Color.black);
		g.drawRect(0, 0, bounds().width - 1, bounds().height - 1);
		g.setColor(_lowColor);
		g.fillRect(1, 1, midpoint + 1, bounds().height - 2);
		g.setColor(_hiColor);
		g.fillRect(midpoint + 1, 1, bounds().width - midpoint - 2, bounds().height - 2);
	}
}