×
Namespaces

Variants
Actions
Revision as of 01:16, 3 September 2009 by warjan (Talk | contribs)

Simple Java ME Canvas loading bar

From Nokia Developer Wiki
Jump to: navigation, search
{{{width}}}

This is an example of a simple canvas based loading bar which you can use as a splash screen for you application. First we create the canvas for the application and get the width and height for the screen

height = getHeight();
width = getWidth();

We then have a factor for the incrementing the loading

factor = width / 4;

We also have a Timer Object

Timer timer = new Timer();

This code comes in the paint method of the canvas

g.setColor(0x00ffccff);
g.fillRect(0, 0, width, height);
g.setColor(0x00ffffff);
g.fillRect(25, height / 2, width - 50, 2);
g.setColor(0x003399ff);
g.drawString("Application name", 100, (height / 2) - 30, Graphics.TOP | Graphics.HCENTER);
g.fillRect(25, height / 2, current, 2);
g.drawString("Loading...", 100, (height / 2) + 20, Graphics.BOTTOM | Graphics.HCENTER);

We then create a class inside the canvas class which will extend the TimerTask class to handle the animation

     private class draw extends TimerTask {
public void run() {
current = current + factor;
if (current > width - 50) {
current = 0;
repaint();
} else {
repaint();
}
}
}

In the constructor of the midlet, we schedule the timer Object to run

repaint();
timer.schedule(new draw(), 2000, 2000);

We now need to create an object of the canvas in the midlet and display it.

81 page views in the last 30 days.
×