Wednesday, September 16, 2009

Adding a simple progress bar

A progress bar is a visual representation of a Real Number between zero and one or a percentage between 0% and 100%.

Below is a method for drawing a simple progress bar:


/**
* @param g Graphics
* @param x
* @param y
* @param w width
* @param h height
* @param p part between zero and total
* @param t total
*/
void fillProgressBar (Graphics g, int x, int y, int w, int h, int p, int t) {
g.drawRect(x, y, w, h);
// p will receive the pixel width of the proportion: part / total
p = (p * w) / t;
g.fillRect(x, y, p, h);
}

2 comments:

Felipe Manhães said...

Só por curiosidade... onde isso pode ser adicionado? =)

Telmo Pimentel Mota said...

For example, when your application have a "loading" state (drawn with canvas) and you know where you are at each repaint.
I use this on Books while the file is loading.