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

本文介绍如何在LWUIT中创建自定义进度条组件,并通过示例代码展示了如何使用进度条来显示任务进度。此外,还提供了一个简单的后台任务类,用于模拟耗时操作并更新进度。
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

先展示下效果 https://pan.quark.cn/s/e81b877737c1 Node.js 是一种基于 Chrome V8 引擎的 JavaScript 执行环境,它使开发者能够在服务器端执行 JavaScript 编程,显著促进了全栈开发的应用普及。 在 Node.js 的开发流程中,`node_modules` 文件夹用于存储所有依赖的模块,随着项目的进展,该文件夹可能会变得异常庞大,其中包含了众多可能已不再需要的文件和文件夹,这不仅会消耗大量的硬盘空间,还可能减慢项目的加载时间。 `ModClean 2.0` 正是为了应对这一挑战而设计的工具。 `ModClean` 是一款用于清理 `node_modules` 的软件,其核心功能是移除那些不再被使用的文件和文件夹,从而确保项目的整洁性和运行效率。 `ModClean 2.0` 是此工具的改进版本,在原有功能上增加了更多特性,从而提高了清理工作的效率和精确度。 在 `ModClean 2.0` 中,用户可以设置清理规则,例如排除特定的模块或文件类型,以防止误删重要文件。 该工具通常会保留项目所依赖的核心模块,但会移除测试、文档、示例代码等非运行时必需的部分。 通过这种方式,`ModClean` 能够协助开发者优化项目结构,减少不必要的依赖,加快项目的构建速度。 使用 `ModClean` 的步骤大致如下:1. 需要先安装 `ModClean`,在项目的根目录中执行以下命令: ``` npm install modclean -g ```2. 创建配置文件 `.modcleanrc.json` 或 `.modcleanrc.js`,设定希望清理的规则。 比如,可能需要忽略 `LICENSE` 文件或整个 `docs`...
2026最新微信在线AI客服系统源码 微信客服AI系统是一款基于PHP开发的智能客服解决方案,完美集成企业微信客服,为企业提供7&times;24小时智能客服服务。系统支持文本对话、图片分析、视频分析等多种交互方式,并具备完善的对话管理、人工转接、咨询提醒等高级功能。 核心功能 ### 1.&nbsp; 智能AI客服 ####&nbsp;自动回复 - **上下文理解**:系统自动保存用户对话历史,AI能够理解上下文,提供连贯的对话体验 - **个性化配置**:可自定义系统提示词、最大输出长度等AI参数 #### 产品知识库集成 - **公司信息**:支持配置公司简介、官网、竞争对手等信息 - **产品列表**:可添加多个产品,包括产品名称、配置、价格、适用人群、特点等 - **常见问题FAQ**:预设常见问题及答案,AI优先使用知识库内容回答 - **促销活动**:支持配置当前优惠活动,AI会自动向用户推荐 ### 2. 多媒体支持 #### 图片分析 - 支持用户发送图片,AI自动分析图片内容 - 可结合文字描述,提供更精准的分析结果 - 支持常见图片格式:JPG、PNG、GIF、WebP等 #### 视频分析 - 支持用户发送视频,AI自动分析视频内容 - 视频文件自动保存到服务器,提供公网访问 - 支持常见视频格式:MP4、等 ### 3.&nbsp; 人工客服转接 #### 关键词触发 - **自定义关键词**:可配置多个转人工触发关键词(如:人工、客服、转人工等) - **自动转接**:用户消息包含关键词时,自动转接给指定人工客服 - **友好提示**:转接前向用户发送提示消息,提升用户体验 #### 一键介入功能 - **后台管理**:管理员可在对话管理页面查看所有对话记录 - **快速转接**:点击&quot;一键介入&quot;按钮,立即将用户转接给人工客服
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值