Progress bar implementation in lwuit (Progress indicator & threads in lwuit by Shai Almog)

Source: Internet
Author: User
Tags gety
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,
Swingworker. Generally I prefer the foxtrot approach implemented in lwuit
Invokeandblock in display, however lots of people like the swingworker Approach
So I used it here as part of the explanations.

First lets create
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

* Clipping to indicate the amount of SS 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 doesn't 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 shoshould 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 wowould 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)/100366f) * 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
Comments.

Backgroundtask is my equivalent to swingworker, its much
Simpler than swingworker:


/**

* A tool allowing to respond to an event in the background possibly

* Progress indication identified red by swings "swingworker" tool. This class

* Shoshould 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

* 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 shocould not alter

* UI progress t to indicate progress.

*/

Public abstract void merge mtask ();

/**

* 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 actionreceivmed (actionevent eV ){

New backgroundtask (){

Public void merge mtask (){

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 ));

 

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.