import java.awt.*; // // // ProgressBar JavaBean v1.00 // // Written by : David Reilly // Release details : Public domain source/binary // Last modification date : Tuesday, October 28, 1997 // public class ProgressBar extends Canvas { protected int m_percentage; protected Color m_textColor; protected boolean m_displayText; public ProgressBar() { m_percentage = 0; } public ProgressBar( int percent ) { if ( ( percent <= 100 ) && ( percent >= 0) ) m_percentage = percent; else m_percentage = 0; } public int getPercent() { return m_percentage; } public void setPercent( int percent ) { if ( ( percent <= 100 ) && ( percent >= 0) ) m_percentage = percent; repaint(); } public boolean getDisplayText() { return m_displayText; } public boolean isDisplayText() { return m_displayText; } public void setDisplayText(boolean b) { m_displayText = b; } public Color getTextColor() { return m_textColor; } public void setTextColor(Color set_text) { m_textColor = set_text; } public void toggleDisplayText() { m_displayText = !m_displayText; } public void paint( Graphics g ) { // Local variables Color background; Color foreground; int width = getSize().width; int height = getSize().height; // Pixel per pecent int pixels; background = getBackground(); foreground = getForeground(); // Draw background g.setColor ( background ); g.fillRect ( 0, 0, width, height); // Draw top left border g.setColor ( Color.gray ); g.drawLine ( 0, 0, width, 0); // top left to top right g.drawLine ( 0, 0 , 0, height ); // top left to bottom left g.setColor ( Color.black.brighter() ); g.drawLine ( 1, 1, width-1, 1 ); // near top left to near top right g.drawLine ( 1, 1, 1, height-1 ); // near top left to near bottom left // Draw bottom right border g.setColor ( Color.lightGray.darker() ); g.drawLine ( 2, height-1, width-1, height-1); // bottom left to bottom right g.drawLine ( width-1, height-1, width-1, 1 ); // bottom right to top right g.setColor ( Color.lightGray ); g.drawLine ( 2, height-2, width-2, height-2); // near bottom left to near bottom right g.drawLine ( width-2, height-2, width-2, 2 ); // near bottom right to near top right // Calculate bar pixels pixels = (int) ( width / 100.00 * m_percentage); if (pixels > width-3) pixels = width-3; // Draw bar g.setColor ( foreground ); g.fillRect ( 2, 2, pixels , height - 3); // If display text is required if (m_displayText) { g.setColor (m_textColor); // Get color information FontMetrics f = g.getFontMetrics(); String percentString = String.valueOf(m_percentage); g.drawString ( percentString , (width - f.stringWidth (percentString) ) / 2, (height ) / 2 + f.getHeight() /2 ); } } // Returns minimum bean size public Dimension getMinimumSize() { return new Dimension(50, 15); } // Returns preferred bean size public Dimension getPreferredSize() { return new Dimension(200, 30); } }