LWUIT实现进度条代码如下:(本源代码改自SUN的工程师Shai Almog的BLOG里的技术文章)
import com.sun.lwuit.Component;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Graphics;
import com.sun.lwuit.Image;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.plaf.Style;
/**
* Simple progress indicator component that fills out the progress made.
* Progress is assumed to always be horizontal in this widget
*/
public class Progress extends Component {
public static final int PROGRESS_START = 0;
public static final int PROGRESS_PAUSE = 1;
private int colorPercent = 0;
private byte percent;
private Image unfilled;
private Image filled;
private String strPercent;
private int percentX;
private int percentY;
private int curWidth;
private int state;
/**
* The default constructor uses internal rendering to draw the progress
*/
public Progress() {
setFocusable(false);
getStyle().setBgTransparency(0);
}
/**
* Allows indicating the progress using a filled/unfilled images.
* The unfilled image is always drawn and the filled image is drawn on top with
* clipping to indicate the amount of progress made.
*
* @param unfilled an image containing the progress bar without any of its
* content being filled (with the progress color)
* @param filled an image identicall to unfilled in every way except that progress
* is completed in this bar.
*/
public Progress(Image unfilled, Image filled) {
this();
this.unfilled = unfilled;
this.filled = filled;
strPercent = 0 + "%";
Dimension dimension = calcPreferredSize();
setPreferredSize(dimension);
// test("WIDTH = " + dimension.getWidth());
// test("HEIGHT = " + dimension.getHeight());
// test("CUR_WIDTH = " + getPreferredW());
// test("CUR_HEIGHT = " + getPreferredH());
}
/**
* Indicate to LWUIT the component name for theming in this case "Progress"
*/
public String getUIID() {
return "Progress";
}
/**
* Indicates the percent of progress made
*/
public byte getProgress() {
return percent;
}
/**
* Indicates the percent of progress made, this method is thread safe and
* can be invoked from any thread although discression should still be kept
* so one thread doesn't regress progress made by another thread...
*/
public void setProgress(byte percent) {
this.percent = percent;
strPercent = percent + "%";
// curWidth = (int) ((((float) percent) / 100.0f) * getWidth());
// percentX = getX() + (getPreferredW() - Font.getDefaultFont().stringWidth(strPercent))/2;
// percentY = getY() + (getPreferredH() - Font.getDefaultFont().getHeight())/2;
// percentX = getX() + (filled.getWidth() - Font.getDefaultFont().stringWidth(strPercent))/2;
// percentY = getY() + (filled.getHeight() - Font.getDefaultFont().getHeight())/2;
// test("X = " + getX());
// test("Y = " + getY());
// test("Width = " + getWidth());
// test("Height = " + getHeight());
repaint();
}
public void changeBG(Image unfilled, Image filled) {
this.unfilled = unfilled;
this.filled = filled;
repaint();
}
/**
* Return the size we would generally like for the component
*/
protected Dimension calcPreferredSize() {
if (filled != null) {
return new Dimension(filled.getWidth(), filled.getHeight());
} else {
// we don't really need to be in the font height but this provides
// a generally good indication for size expectations
return new Dimension(Display.getInstance().getDisplayWidth(),
Font.getDefaultFont().getHeight());
}
}
/**
* Paint the progress indicator
*/
public void paint(Graphics g) {
if (filled != null) {
curWidth = (int) ((((float) percent) / 100.0f) * getWidth());
percentX = getX() + (getPreferredW() - Font.getDefaultFont().stringWidth(strPercent)) / 2;
percentY = getY() + (getPreferredH() - Font.getDefaultFont().getHeight()) / 2;
// draw based on two user supplied images
g.drawImage(unfilled, getX(), getY());
g.clipRect(getX(), getY(), curWidth, getHeight());
g.drawImage(filled, getX(), getY());
g.setClip(0, 0, Display.getInstance().getDisplayWidth(), Display.getInstance().getDisplayHeight());
g.setColor(colorPercent);
g.setFont(Font.getDefaultFont());
g.drawString(strPercent, percentX, percentY);
} else {
// draw based on simple graphics primitives
Style s = getStyle();
g.setColor(s.getBgColor());
int curve = getHeight() / 2 - 1;
g.fillRoundRect(getX(), getY(), getWidth() - 1, getHeight() - 1, curve, curve);
g.setColor(s.getFgColor());
g.drawRoundRect(getX(), getY(), getWidth() - 1, getHeight() - 1, curve, curve);
g.clipRect(getX(), getY(), curWidth - 1, getHeight() - 1);
g.setColor(s.getBgSelectionColor());
g.fillRoundRect(getX(), getY(), getWidth() - 1, getHeight() - 1, curve, curve);
g.setColor(s.getFgColor());
g.drawRoundRect(getX(), getY(), getWidth() - 1, getHeight() - 1, curve, curve);
}
}
final void test(String _info) {
System.out.println("***" + _info);
}
/**
* @return the state
*/
public int getState() {
return state;
}
/**
* @param state the state to set
*/
public void setState(int state) {
this.state = state;
}
}
使用时只需new 一个Progress(Image unfilled, Image filled) ,传入进度条的背景图片和前景图片,然后通过setProgress(byte percent)和getProgress()来设置和获得进度百分比.