participate


CLDC and MIDP - How to implement a "Please wait" message
<<   Back to Forum  |   Give us Feedback
This topic has 5 replies on 1 page.
Superluz76
Posts:2
Registered: 1/12/04
How to implement a "Please wait" message   
Jan 12, 2004 7:51 AM

 
Hi.

I'm writing a game that needs to do a heavy initializing routine before every level. Now it would be nice to show some "please wait..." message to the user so that he knows that the program hasn't crashed.

Surprisingly that was quite problematic thing to implement. As Display.setCurrent() doesn't necessarily have immediate effect this following code doesn't seem to work:

display.setCurrent(messageCanvas);
someTimeTakingRoutines();
display.setCurrent(gameCanvas);

The result is the same as if the first setCurrent() function wasn't called at all.

Can anyone give me a simple example how I could show a message screen while the program is loading something?

Thank you.
 
shmoove
Posts:1,442
Registered: 9/26/02
Re: How to implement a "Please wait" message   
Jan 12, 2004 8:32 AM (reply 1 of 5)  (In reply to original post )

 
display.setCurrent() doesn't take effect until you free up the UI thread (the thread that calls commandAction(), paint(), keyPressed(), etc.). I guess you are calling that code in one of the UI thread's methods so in fact you are performing all the lengthy initializing in the UI thread. That means that until you are finished (after display.setCurrent(gameCanvas)) the Displayable won't change.
What you need to do is move the lengthy code into a separate thread that calls display.setCurrent(gameCanvas) when it finishes:

public class Something implements Runnable {
 
  //...
 
  // I'm guessing it is in keyPressed:
    display.setCurrent(messageCanvas);
    Thread t = new Thread(this);
    t.start();
  // that's it for this method!
 
  public void run() {
    someTimeTakingRoutines();
    display.setCurrent(gameCanvas);
  }
 
  //....
 
}


shmoove
 
Superluz76
Posts:2
Registered: 1/12/04
Re: How to implement a "Please wait" message   
Jan 12, 2004 11:15 PM (reply 2 of 5)  (In reply to #1 )

 
thank you very much shmoove!!
 
Dooing
Posts:1
Registered: 6/14/05
Re: How to implement a "Please wait" message   
Jun 14, 2005 8:37 AM (reply 3 of 5)  (In reply to #2 )

 
Well, those examples were quite usefull -
however, I would like to create a "more dynamic" please wait screen.
One that also show a percentage rate of how much is finished loading.
( the percentage rate doesn't need to be exact, I decided just to update the
progress bar after creating a few things, with a new percentage rate that I guess.

From your tipps, this is my Code so far - it works to write "Loading"
and to print the progress bar - but it doesn't get updated, at least I can't see it.

This is the code of my ProgressBar
public class MyProgressBar extends GameCanvas implements Runnable {
 
    static int WIDTH; 
    static int HEIGHT;
    private int progress = 0;
    public boolean LOADING = true;
    private Graphics g;
    private int barHeight = 0;
    private int barLength = 0;
    private int fortschrittLaenge = 0;
 
    private Thread myThread;
 
    public MyProgressBar () {
        super(false);
        g = getGraphics();
        setFullScreenMode(true);
        this.HEIGHT = getHeight();
        this.WIDTH = getWidth();
        this.barHeight = this.HEIGHT / 10;
        this.barLength = (int)(this.WIDTH / 1.32);
        myThread = new Thread(this);
        myThread.start();
    }
 
    public void paint(Graphics g) {
        g.setColor(BLACK);
        g.drawRect(0, 0, WIDTH, HEIGHT);
        g.setFont(LARGEFONT);
        if(progress!=0) {
            progressLength = barLength / progress;
        }
        else {
            progressLength = 0;
        }
         g.setColor(GREY);
        g.fillRect((WIDTH-barLength)/2, (HEIGHT-barHeight)/2, barLength, barHeight);
         g.setColor(RED);
        g.fillRect((WIDTH-barLength)/2, (HEIGHT-barHeight)/2, progressLength, barHeight);
        g.drawString(" loading... ", 0, 0, 0);
    }
 
    public void setProgress(int progress) {
        this.progress = progress;
    }
 
    public void run() {
        while (LOADING) {
            paint(g);
        }
    }
 
}


And here is my code for my GameCanvas Class
public MyGameCanvas(MyMidlet midlet, Highscore highscore, MyProgressbar progressBar) {
        super(false);
 
        g = getGraphics();
        this.progressBar= progressBar;
        progressBar.setProgress(1);
        setFullScreenMode(true); 
        HEIGHT = getHeight(); 
        WIDTH = getWidth();
        this.midlet = midlet;
        this.highscore = highscore;
        myThread = new Thread(this);
        myThread .start();
        progressBar.setProgress(2);
    }
run() {
        someTimeTakingRoutines();
        midlet.showMyCanvas();
...
..
.
}
 
 public void someTimeTakingRoutines() { // init GameCanvas
 
        createStuff();
        progressBar.setProgress(10);
        createMoreStuff();
        progressBar.setProgress(30);
        createMoreStuff();
        progressBar.setProgress(50);
        createMoreStuff();
        progressBar.setProgress(70);
        createMoreStuff();
        progressBar.setProgress(100);
        
    }

In my midlet, I have:
    public void loadMyGame() {
 
        myProgressBar = new ProgressBar();
        display.setCurrent(progressBar);
        myGameCanvasd = new MyGameCanvasd(this, highscore, myProgressBar);
    }

And, after I loaded all game stuff in my GameCanvas Class I call this method in my midlet from my GameCanvasClass from its run method (see above)
    public void showMyGameCanvas() {
        display.setCurrent(myGameCanvas);
    }

It does show the progressBar, but it doesn't get updated -
at least I can't see the changes.

Why?
 
Lord-Atlan
Posts:19
Registered: 5/23/05
Re: How to implement a "Please wait" message   
Jun 15, 2005 12:23 AM (reply 4 of 5)  (In reply to #3 )

 
hi,
i'm not familiar with this progressbars, but i guess, that the problem is your bussy while-loop.
your thread is hanging in this loop and so there should be some problems with setting fields...
one simple solution, that runs well in my apps, is to implement your MyProgressBar as a inner class of your main-class.
so this provides the inner class with full access to the variables of the main-class. then just move the progress and loading fields to your main class.
if you create your progressbar, you can still change these two fields in your main-class and your progressbar will immediately use this new values...

hope that helps you...
mmkl
 
shmoove
Posts:1,442
Registered: 9/26/02
Re: How to implement a "Please wait" message   
Jun 15, 2005 3:10 PM (reply 5 of 5)  (In reply to #3 )

 
 
    public void run() {
        while (LOADING) {
            paint(g);
        }
    }

You don't call paint() yourself. You request a repaint(), and the system will call paint() for you (you can also use serviceRepaints() if you want to make sure the code to finish the repaint before it continues).

The thing about the thread hanging might also be true and it's better to put a slee() or wait() or at least yield() (but yield() still causes problems on phones that don't handle threads well).

shmoove

shmoove
 
This topic has 5 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums