Namespaces
Variants
Actions
(Redirected from J2ME Canvas Loading Bar)

Canvas Loading Bar in Java ME

Jump to: navigation, search
Article Metadata

Article
Created: jappit (30 Apr 2008)
Last edited: hamishwillee (07 Feb 2012)

A simple loading bar to be used on Canvas when doing long operations. You can see a live preview here: Canvas Loading bar emulator

J2me loading bar.png

We'll start defining some instance variables, that we'll use within our code:

public long stepInterval = 250L;
 
public int width = 0;
public int height = 0;
 
int padding = 0;
 
int color = 0x000000;
 
int squares = 0;
int squareWidth = 0;
 
int currentSquares = 0;
 
Timer stepTimer = null;

Now the bar constructor, with its arguments:

  • bar size (width and height)
  • padding between squares
  • number of squares to be used
  • squares color
public LoadingBar(int width, int height, int padding, int squares, int color)
{
this.width = width;
this.height = height;
this.squares = squares;
 
this.color = color;
this.padding = padding;
 
this.squareWidth = (width - padding) / (squares) - padding;
}

Paint method is quite straightforward:

public void paint(Graphics g)
{
g.setColor(color);
 
for(int i = 0; i < currentSquares; i++)
{
g.fillRect(i * (squareWidth + padding), 0, squareWidth, height);
}
}

Finally, the animation logic, where we'll use a Timer, and expose start() and stop() methods to control Bar animation:

public void start()
{
stepTimer = new Timer();
 
stepTimer.schedule(new TimerTask()
{
public void run()
{
step();
}
},
stepInterval, stepInterval
);
}
public void stop()
{
stepTimer.cancel();
}
void step()
{
currentSquares = (currentSquares + 1) % (squares + 1);
}

Comments

Reviewer-approved.png
23 Sep
2009
Article Review by vkmunjpara (20090923)

If some task may take long time then if user will not see any message or instruction or loading bar, he/she will close the application. That’s why Loading bar is used that represents to wait for some time until loading bar is filling its portion completely.

Here loading bar is drawn in canvas display and for animation timer is used.

This article explains all the modules with example and also with snapshot.


This page was last modified on 7 February 2012, at 05:36.
137 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2012 All rights reserved