`
deaboway
  • 浏览: 54196 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

LWUIT中的进度条实现(Progress Indicator & Threads In LWUIT by Shai Almog)

阅读更多
LWUIT doesn't ship with a pre-existing progress indicator, mostly because making something generic enough for all the common cases is not as simple as it might seem in the beginning. Especially when considering how easy it is to write your own progress indicator...



This is a simple example of how to create a custom component in LWUIT in this specific case a progress indicator that supports both drawing itself (as a filled round rectangle and as a couple of images overlayed one on top of the other. The progress indicator component is fully themeable and customizable and will accept all L&F settings seamlessly.
The screenshots show both an image based indicator (its ugliness is just a testament to my bad drawing skills) and an indicator drawn in graphics primitives (fill/drawRoundRect). These are the images used to draw the image progress:



As part of this I also wanted to create something else familiar to Swing developers, the SwingWorker. Generally I prefer the foxtrot approach implemented in LWUIT as invokeAndBlock in Display, however lots of people like the SwingWorker approach so I used it here as part of the explanations.

First lets create the Progress indicator component:

/**
* Simple progress indicator component that fills out the progress made.
* Progress is assumed to always be horizontal in this widget
*
* @author Shai Almog
*/
public class Progress extends Component {
private byte percent;
private Image unfilled;
private Image filled;

/**
* The default constructor uses internal rendering to draw the progress
*/
public Progress() {
setFocusable(false);
}

/**
* 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;
}

/**
* 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;
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) {
int width = (int)((((float)percent) / 100.0f) * getWidth());
if(filled != null) {
if(filled.getWidth() != getWidth()) {
filled = filled.scaled(getWidth(), getHeight());
unfilled = unfilled.scaled(getWidth(), getHeight());
}

// draw based on two user supplied images
g.drawImage(unfilled, getX(), getY());
g.clipRect(getX(), getY(), width, getHeight());
g.drawImage(filled, getX(), getY());
} 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(), width - 1, getHeight() - 1);
g.setColor(s.getBgSelectionColor());
g.fillRoundRect(getX(), getY(), getWidth() - 1, getHeight() - 1, curve, curve);
}
}
}



This code seems to me to be simple but obviously I'm not objective, if something is not clear or you think it might not be clear to others please let me know in the comments.

BackgroundTask is my equivalent to SwingWorker, its much simpler than SwingWorker:


/**
* A tool allowing to respond to an event in the background possibly with
* progress indication inspired by Swings "SwingWorker" tool. This class
* should be used from event dispatching code to prevent the UI from blocking.
* State can be stored in this class the separate thread and it can be used by
* the finish method which will be invoked after running.
*
* @author Shai Almog
*/
public abstract class BackgroundTask {
/**
* Start this task
*/
public final void start() {
if(Display.getInstance().isEdt()) {
taskStarted();
} else {
Display.getInstance().callSeriallyAndWait(new Runnable() {
public void run() {
taskStarted();
}
});
}
new Thread(new Runnable() {
public void run() {
if(Display.getInstance().isEdt()) {
taskFinished();
} else {
performTask();
Display.getInstance().callSerially(this);
}
}
}).start();
}

/**
* Invoked on the LWUIT EDT before spawning the background thread, this allows
* the developer to perform initialization easily.
*/
public void taskStarted() {
}

/**
* Invoked on a separate thread in the background, this task should not alter
* UI except to indicate progress.
*/
public abstract void performTask();

/**
* Invoked on the LWUIT EDT after the background thread completed its
* execution.
*/
public void taskFinished() {
}
}

And this is the code to display these two progress bars:

Form progressForm = new Form("Progress");
progressForm.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
Progress p1 = new Progress();
progressForm.addComponent(new Label("Drawn"));
progressForm.addComponent(p1);
Progress p2 = new Progress(Image.createImage("/unfilled.png"), Image.createImage("/filled.png"));
p2.getStyle().setBgTransparency(0);
progressForm.addComponent(new Label("Image Based"));
progressForm.addComponent(p2);
progressForm.show();

class ProgressCommand extends Command {
private Progress p;
public ProgressCommand(String name, Progress p) {
super(name);
this.p = p;
}
public void actionPerformed(ActionEvent ev) {
new BackgroundTask() {
public void performTask() {
for(byte b = 0 ; b <= 100 ; b++) {
try {
p.setProgress(b);
Thread.sleep(100);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}.start();
}
}

progressForm.addCommand(new ProgressCommand("Drawn", p1));
progressForm.addCommand(new ProgressCommand("Images", p2));

转自:http://lwuit.blogspot.com/2008/05/progress-indicator-threads-in-lwuit.html

分享到:
评论

相关推荐

    LWUIT.jar LWUIT.jar

    LWUIT.jarLWUIT.jarLWUIT.jarLWUIT.jarLWUIT.jarLWUIT.jar

    最新LWUIT_1_5

    LWUIT哦,最新的包,学习学习。非常好用哦

    LWUIT_3_1英文原版.part1

    The Lightweight UI Toolkit (LWUIT) 是一个轻量级JavaME UI工具包。LWUIT类似Swing 的MVC架构, 支持多种布局(Layouts), 皮肤更换, 字体, 触摸屏, 动画效果, Rich控件, 3D集成, Painter, 模式对画框, I18N/L10N等...

    Lwuit入门程序测试一下Demo

    Lwuit入门程序测试一下Demo 里面需要用到LWUIT的jar包

    lwuit-blackberry上移植的版本

    Lwuit在blackberry上的移植版本,使用subversion签下来的,我把这个从lwuit-incubator中提取出来的,里面有DOC和源码,不过它把4.2-4.7版本放在一起了,应用的时候需要根据自己项目实际进行裁剪和修改。

    lwuit.rar_J2ME lwuit_LWUIT_j2me_j2me LWU_九宫

    J2ME lwuit实现屏幕九宫图,功能十分强大

    lwuit Developer_Guide

    lwuit的开发文档 Hello World for MIDP import com.sun.lwuit.Display; import com.sun.lwuit.Form; import com.sun.lwuit.Label; import com.sun.lwuit.layouts.BorderLayout; import ...

    LWUIT的最新源代码(官方的LWUIT.jar反编译)

    在网上找了很久源代码,基本上都是缺胳膊少腿的,svn上1.3版的代码还处于测试阶段...官方的只通了LWUIT.jar和Demo的下载,没有源代码,我把这个LWUIT.jar反编译了一下,把反编译过后产生的错误修改好了,已经可以用了。

    lwuit.rar_J2ME ui_LWUIT_j2me

    LWUIT实现了MVC架构,是J2ME程序中UI设计的一个JAR包。该文档是LWUIT帮助文档的CHM版

    lwuit 1.2.1lwuit 最新

    The Lightweight UI Toolkit (LWUIT) 是一个轻量级JavaME UI工具包。LWUIT类似Swing 的MVC架构, 支持多种布局(Layouts), 皮肤更换, 字体, 触摸屏, 动画效果, Rich控件, 3D集成, Painter, 模式对画框, I18N/L10N等...

    LWUIT最新源代码

    Sun发布了LWUIT(Light-Weight UI Toolkit)的源代码。项目主页访问:LWUIT。 The Lightweight UI Toolkit (LWUIT) 是一个轻量级JavaME UI工具包。LWUIT类似Swing 的MVC架构, 支持多种布局(Layouts), 皮肤更换, ...

    lwuit_demo_src.rar_DEMO_J2ME lwuit de_LWUIT_lwuit demo

    lwuit demo 的源代码,基本重要的函数都在这里进行了展示

    LWUIT1.2(20090715)

    SUN公司发布了最新的J2ME平台的UI库:LWUIT1.2,主要有以下改动: * New Styling concept added selected and unselected styles. * New LWUIT Designer with support for new styles, gradients, exporting ant ...

    使用Lwuit中遇到的问题解决

    LWuit 在Eclipse环境下常遇到的问题。里面有解决方案。大家可以拿来参考。

    真正用lwuit实现调用调用谷歌天气预报

    网上还真的没有用lwuit这个包调用谷歌天气预报的程序,但我的事真正用到lwuit这个包!!

    LWUIT中ResourceEditor编辑res 工具

    经过几个月的学习 用Eclipse 工具来做lwuit 以来遇到了很多的问题。此资源针对下面的错误而发: java.io.IOException: Corrupt theme file unrecognized magic number: ff 这是因为设计器版本的问题。此编辑器就会能...

    j2me记事本lwuit高级界面

    本项目为j2me实现的记事本程序,包括新建 保存 读取 修改 等功能,内建lwuit类库,实现了aero效果。(本项目创建平台为NetBeans6.8)

    lwuit1.4介绍

    根据搜索翻译出来的lwuit1.4的介绍,有兴趣的朋友可以看看

    lwuit1.4 jar包

    lwuit1.4 jar包 lwuit1.4 jar包 lwuit1.4 jar包 lwuit1.4 jar包

    J2ME LWUIT 之九宫图

    使用Netbeans 利用LWUIT开源GUI实现手机上类似IPhone的九宫图,效果很不错,代码经过精简,建议使用!

Global site tag (gtag.js) - Google Analytics