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
分享到:
相关推荐
3. **动画和过渡效果**:LWUIT支持动画和过渡效果,可以轻松实现组件的滑动、淡入淡出等动态效果,从而增强用户界面的吸引力。 4. **资源管理**:LWUIT包含一个资源管理器,用于处理图像、音频、视频等媒体资源,...
在"LWUIT1.3源码"中,你可以深入研究其内部实现,理解以下几个关键部分: - **Component类**:这是LWUIT的基础组件类,其他所有可视组件都是继承自它。了解它的属性和方法,有助于理解LWUIT的基本操作。 - **...
在描述中提到,“基本重要的函数都在这里进行了展示”,这意味着源代码中包含了LWUIT库的核心功能和常见操作的实现。开发者可以通过研究这些源代码,学习如何初始化LWUIT环境,创建和布局UI组件,处理用户事件,以及...
在“Lwuit入门程序测试一下Demo”中,我们将探讨如何使用LWUIT库来创建简单的应用程序,并通过“LocationDemo1”文件了解其实际应用。 **LWUIT的特点:** 1. **轻量级**:LWUIT设计时考虑了资源有限的移动设备,...
在LWUIT中实现九宫图特效,主要涉及以下几个关键点: 1. **布局管理**:LWUIT支持多种布局管理器,如GridLayout、FlowLayout等。对于九宫图,我们可以选择GridLayout,因为它可以方便地创建出3行3列的布局结构。 2...
标题中的“lwuit实例 lwuit j2me 界面”指的是使用LWUIT库在J2ME(Java 2 Micro Edition)环境中创建的实际应用案例,展示了如何通过LWUIT来设计和实现用户界面。J2ME是Java平台的一个子集,主要用于嵌入式系统和...
LWUIT.jarLWUIT.jarLWUIT.jarLWUIT.jarLWUIT.jarLWUIT.jar
标题中的"lwuit.rar_J2ME lwuit_LWUIT_j2me_j2me LWU_九宫"表明这是一个与LWUIT相关的压缩包,内容可能包含了实现J2ME平台上九宫图功能的代码或资源。 九宫图是一种常见的界面布局方式,通常用于显示多个小视图,如...
这使得LWUIT应用程序能实现高度的定制化,满足不同平台和品牌的需求。 4. 兼容性和跨平台性: 作为Java ME的一部分,LWUIT旨在提供广泛的设备兼容性,支持多种Java ME兼容的手机和嵌入式设备。它的跨平台特性使得...
《Hello LWUIT——LWUIT开发指南2》 LWUIT(Lightweight User Interface Toolkit)是Java ME平台上的一个开源用户界面库,它为开发者提供了丰富的UI组件和强大的设计工具,使得在移动设备上创建美观、交互性强的...
LWUIT的核心优势在于其可定制性,开发者可以通过自定义主题来改变应用的整体外观和感觉,实现与设备原生界面风格的统一。LWUIT支持多种设备和操作系统,包括J2ME、Symbian、Blackberry等,这使得开发者可以编写一次...
LWUIT( Lightweight UI Toolkit)是Java ME平台上用于构建用户界面的一个库,它...对于复杂的UI设计和功能实现,学习和理解LWUIT的底层工作原理也是必要的。记得在解决问题后,分享经验可以帮助其他开发者,共同进步。
**LWUIT (Lightweight User Interface Toolkit)** 是Java ME(J2ME)平台上的一款轻量级用户界面库,主要用于创建富客户端应用,尤其适合在移动设备上构建吸引人的、功能丰富的用户界面。LWUIT 提供了丰富的组件、...
LWUIT( Lightweight UI Toolkit)是Java ME平台上的一款开源用户界面框架,主要用于开发移动设备上的...通过理解和修改源代码,你可以在项目中实现更具创新性和个性化的功能,同时加深对移动设备UI设计原则的理解。
压缩包中的“lwuit源码”很可能包含了LWUIT库的源代码,这将对开发者非常有价值。通过阅读和学习源码,开发者可以深入理解LWUIT的工作原理,自定义组件,甚至根据需求进行扩展和优化。 5. **使用LWUIT进行手机应用...
**LWUIT(Lightweight User Interface Toolkit)**是Java ME平台上的一个开源库,它提供了一套丰富的组件和...在学习过程中,遇到问题不要气馁,多参考社区资源和在线论坛,你会发现LWUIT是一个强大且有趣的开发工具。
在“LWUIT的一个例子”中,我们将探讨如何利用LWUIT来实现一个模仿销售系统的应用。** 首先,LWUIT提供了一系列组件,如按钮、文本框、列表视图等,这些组件可以帮助开发者快速构建UI。在这个销售系统示例中,可能...
LWUIT,全称Lightweight User Interface Toolkit,是Java ME(J2ME)平台上的一款开源UI框架,专门用于创建富用户界面。它弥补了J2ME标准库在图形用户界面设计上的不足,提供了丰富的组件、动画效果和主题支持,使得...
LWUIT(Lightweight UI Toolkit)是为移动设备提供的一个轻量级用户界面工具包,广泛应用于Java ME(Java Platform, Micro Edition)开发中。LWUIT开发文档是指导开发者如何利用LWUIT进行应用程序开发的官方指南或...