Blond Mobile


J2ME UI: Implementing a Progress Bar

first we create the Progress Bar object, the is is the simple part :)

public class Gauge {
    
    private int m_x, m_y, m_hight, m_width, m_Progress;
    
    public void set(int x, int y, int height, int width ) {
        m_x = x;
        m_y = y;
        m_height = height;
        m_width = width;
    }
    
    public void  setProgress(int progress){
     m_progress= progress;
    }
    
    public void paint(Graphics g){
        if(m_progress>=m_width)
            return;
        g.setColor(0);
        g.drawRect(m_x, m_y-1, m_width, m_height+1);
        
        g.setColor(GuiConstants.JAJAH_COLOR);
        g.fillRect(m_x+1, m_y, m_Progress, m_height);
        g.setColor(GuiConstants.LIST_BACK_COLOR);
        if(m_width-m_Progress>0)
            g.fillRect(m_Progress + m_x, m_y, m_width-m_Progress, m_height);
    }

now we need to use it:

in order to display the Progress Bar, we will display it using a canvas. the canvas will hold a Progress Bar as a member, and it will display it. on that canvas more info can be displayed. note that on each progress update  a repaint occurs. its sufficient to only repaint the progress bar or any updated UI.

public void run() {
        try{
            init();// init params
            m_paintSuper = true;
            repaint();
            boolean run = !connection.callAllowed();// condition for running the bar
            while(run){
                for(int i= 1;i<m_gauge.getWidth()&& run;i++){
                    m_gauge.setProgress(i);
                    Thread.sleep(GAUGE_SPEED); // to make gauge slower
                    repaint();
                    run = !connection.callAllowed();
                }
            }
        }
        catch (Exception e) {
            run();
        }
    }
    
    protected void paint(Graphics g) {
          // things that need to painted only once
          if(m_paintSuper){ 
            super.paint(g);
          }
           m_paintSuper = false;
        }
            
        m_gauge.paint(g);
    }
    

now that we have the canvas ready with the Progress bar and the update of the progress we are left to display the canvas

if(m_gauge == null)
     m_gauge = new GaugeView(null);
// sets the text for the canvas or any other thing to the canvas
m_Gauge.setText(str);         
Display.getDisplay(m_midlet).setCurrent(m_gauge);
Thread t = new Thread(m_gauge);
t.start();

Tags: , , , ,

Leave a Reply

Jajah is the VoIP player that brought you web-activated telephony.