LWUIT实现进度条提示的方法

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()来设置和获得进度百分比.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蜡台

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值