//sets the initial size of the bean on a canvas setSize(100, 25); }
public float getScaleSize() { return scaleSize; }
public void setScaleSize(float sSize) { //The scale size can never set to a value lower than //the current value this.scaleSize = Math.max(0.0f, sSize); if (this.scaleSize < this.currentValue) { this.scaleSize = this.currentValue; } }
public float getCurrentValue() { return currentValue; }
public void setCurrentValue(float cVal) { //The current value can not be set negative //nor can it be set greater than the scale size this.currentValue = Math.max(0.0f, cVal); if (this.currentValue > this.scaleSize) { this.currentValue = this.scaleSize; } }
//The paint method is called by the container public synchronized void paint(Graphics g) { int width = getSize().width; int height = getSize().height;
// ProgressBar.javapackage unleashed.ch19;//《java2技术内幕》上面现成的例子,不过他做的是javaBeanimport java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.Serializable;public class ProgressBar extends Canvas implements Serializable
{
private float scaleSize;
private float currentValue;
//Default constructor
public ProgressBar()
{
this(100, 50);
}
//Constructor
public ProgressBar(float scaleSize, float currentValue)
{
super();
this.scaleSize = scaleSize;
this.currentValue = currentValue;
setBackground(Color.lightGray);
setForeground(Color.magenta);
//sets the initial size of the bean on a canvas
setSize(100, 25);
}
public float getScaleSize()
{
return scaleSize;
}
public void setScaleSize(float sSize)
{
//The scale size can never set to a value lower than
//the current value
this.scaleSize = Math.max(0.0f, sSize);
if (this.scaleSize < this.currentValue)
{
this.scaleSize = this.currentValue;
}
}
public float getCurrentValue()
{
return currentValue;
}
public void setCurrentValue(float cVal)
{
//The current value can not be set negative
//nor can it be set greater than the scale size
this.currentValue = Math.max(0.0f, cVal);
if (this.currentValue > this.scaleSize)
{
this.currentValue = this.scaleSize;
}
}
//The paint method is called by the container
public synchronized void paint(Graphics g)
{
int width = getSize().width;
int height = getSize().height;
g.setColor(getBackground());
g.fillRect(1, 1, width-2, height-2);
g.draw3DRect(0,0, width-1, height-1, true);
g.setColor(getForeground());
g.fillRect(3,3,(int)((currentValue * (width-6))/scaleSize),
height-6);
}
//The grow method makes the current value larger
public void grow()
{
setCurrentValue( this.currentValue + 1.0f);
}
//The shrink method makes the current value smaller
public void shrink()
{
setCurrentValue( this.currentValue - 1.0f);
}
}//class