import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Graphics; /** */ /** * 模块功能:J2ME进度条 * @author midi * midi13@gmail.com * 2007-7-14 15:20:21 * @version 1.0 * @since 2007 * */ public class ProgressBar implements Runnable ... { /** *//** 进度条宽度 */ private int barWidth; /** *//** 进度条高度 */ private int barHeight; /** *//** 步长 */ private int barStep; /** *//** 最大步长(格子数)=barWidth/barStep */ private int barStepMax; /** *//** 进度条的游标 */ private int cursor = 0; /** *//** 进度条的x */ private int barX; /** *//** 进度条的y */ private int barY; /** *//** 背景色 */ private int bgColor; /** *//** 前景色 */ private int fgColor; /** *//** 是否完成进度 */ private boolean done = false; /** *//** Graphics对象 */ private Graphics g; /** *//** Display对象 */ private Display display; /** *//** 目标屏幕 */ private Displayable aim; public ProgressBar(Display display, Displayable aim, Graphics g) ...{ this.display = display; this.aim = aim; this.g = g; } /** *//** * 初始化其他参数 * * @param barWidth * @param barHeight * @param barX * @param barY * @param bgColor * @param fgColor */ public void initialize(int barWidth, int barHeight, int barX, int barY, int bgColor, int fgColor) ...{ this.barWidth = barWidth; this.barHeight = barHeight; this.barX = barX; this.barY = barY; this.bgColor = bgColor; this.fgColor = fgColor; barStepMax = 10; barStep = barWidth / barStepMax; } /** *//** * 绘制进度条 * * @param g */ public void draw(Graphics g) ...{ g.setColor(this.bgColor); g.fillRect(this.barX, this.barY, this.barWidth, this.barHeight); g.setColor(this.fgColor); g.fillRect(this.barX, this.barY, cursor * this.barStep, this.barHeight); } public void run() ...{ while (!done) ...{ draw(g); cursor++; if (cursor >= barStepMax) ...{ done = true; display.setCurrent(aim); } } }}