// // ProgressBar class // part of the set of documents known as Java no sugar. // Copyright (c) 1996,1997 Sunil Gupta, sunil@magnetic.demon.co.uk // placed into the public domain by the author // import java.awt.*; public class ProgressBar extends Canvas { //################################################################## // //################################################################## public static final int HORIZONTAL = 0; public static final int VERTICAL = 1; public static final int SOLID = 2; public static final int SLATTED = 3; private int percentage = 0; private int style = SOLID; private int orientation = HORIZONTAL; private Color bar_colour = Color.blue; private Color border_colour = Color.gray; private int slat_gap = 2; // pixels; private int slat_width = 10; // pixels; //################################################################## // //################################################################## public ProgressBar( int orientation) { this.orientation = orientation; } public ProgressBar( int orientation, int style) { this(orientation); this.style = style; } //****************************************************************** // initial size must be set in addNotify, not in constructor or // tharr be null pointers public void addNotify() { super.addNotify(); resize(this.preferredSize()); } //****************************************************************** public void paint (Graphics g) { Dimension size; int bar_width, bar_height; size = size(); //---------figure out the size of the bar------------------------------ if (orientation == HORIZONTAL) { bar_height = size.height; bar_width = (int) Math.round( (double)percentage/100.0 * (double)size.width); } else { bar_width = size.width; bar_height = (int) Math.round( (double)percentage/100.0 * (double)size.height); } //---------draw bar----------------------------------------------------- g.setColor( bar_colour); switch (style) { case SLATTED: if (orientation == HORIZONTAL) horizontal_slats(g, bar_width, bar_height); else vertical_slats(g, bar_width, bar_height, size.height); break; case SOLID: default: if (orientation == VERTICAL) g.fillRect(1, size.height-2-bar_height , bar_width-2, bar_height); else g.fillRect(1,1, bar_width-2, bar_height-2); break; } g.setColor( border_colour); g.draw3DRect(0,0,size.width-1, size.height-1, false); } //****************************************************************** public Dimension minimumSize() { if (orientation == HORIZONTAL) return ( new Dimension (100,40)); else return ( new Dimension (40,100)); } //****************************************************************** public Dimension preferredSize() { return minimumSize(); } //****************************************************************** public void randomize() { percentage = (int) Math.round(Math.random() * 100.0); } //****************************************************************** public void increment() { if (percentage < 100) { percentage++; repaint(); } } //****************************************************************** public void decrement() { if (percentage > 0) { percentage--; repaint(); } } //****************************************************************** private void horizontal_slats( Graphics g, int width, int height) { int x = width; while (x > 1) { g.fillRect( x-slat_width, 1, slat_width, height-2); x -= (slat_width + slat_gap); } } //****************************************************************** private void vertical_slats( Graphics g, int width, int height, int ymax) { int y = ymax-height; while (y < ymax) { g.fillRect( 1, y, width-2, slat_width); y += (slat_width + slat_gap); } } }
.