import java.applet.Applet; import java.awt.*; public class Example8ProgressPanel extends Applet { private int _maxRecords; private int _currentRecord = 0; public Example8ProgressPanel (int maxRecords) { _maxRecords = maxRecords; } // end of constructor public void setCurrentRecord(int currentRecord) { _currentRecord = currentRecord; repaint(); } // end of setCurrentRecord public void paint(Graphics g) { // based on JavaWorld ProgressBar code by Mark Boulo Dimension panelDimension = size(); double percentComplete = (double)_currentRecord * 100.0 / (double)_maxRecords; int pixelWidth = (panelDimension.width * _currentRecord) / _maxRecords; // Fill the bar the appropriate percent full. g.setColor (Color.lightGray); g.fillRect (0, 0, pixelWidth, panelDimension.height); // Build a string showing the % completed as a numeric value. String s = String.valueOf((int)percentComplete) + " %"; // Set the color of the text. If we don't, it appears in the same color // as the rectangle making the text effectively invisible. g.setColor (Color.black); // Calculate the width of the string in pixels. We use this to center // the string in the progress bar window. FontMetrics fm = g.getFontMetrics(g.getFont()); g.drawString(s, (panelDimension.width - fm.stringWidth(s))/2, 24); } // end of paint } // end of ExampleProgressPanel