×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    23

    Can anybody help me with this code?

    I am a beginner to J2ME. The following is a small program I wrote just for test.
    Theoretically, in my opinion, I shall see in the screen a number increase from 1 to 29999 rapidly after execute the code. But instead, it displays nothing but only displays 29999 after several seconds . Can some body point out what's wrong with this code? Thanks.



    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;

    public class Test extends MIDlet{

    int x=1;
    String b="";

    private Display display;

    public Test() {
    display = Display.getDisplay(this);

    }


    public void startApp() {

    MyCanvas mc = new MyCanvas() ;
    display.setCurrent(mc) ;


    while(x<30000){
    b=String.valueOf(x);
    mc.repaint();
    x++;
    }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    class MyCanvas extends Canvas{
    public void paint(Graphics g){
    g.drawString(b,10,10,0);
    }
    }


    }
    Contact me at [email]qcw@hotmail.com[/email]

    Visit my space at [url]http://spaces.msn.com/members/wangc[/url]

  2. #2
    Regular Contributor
    Join Date
    Mar 2003
    Posts
    393

    RE: Can anybody help me with this code?

    The updates to the screen are too fast to be reflected. You need to draw in a different thread to keep the UI responsive and also make sure that the next number is drawn ONLY after the one before. Another thing to take care is that provising a small wait between the repaints allows commands such as the Exit to be processed.

    Here's the code. I tested on the Series 60 MIDP Concept SDK Emulator Beta 0.2
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;

    //Counter
    //Need to know when a number has been painted and need to wait a for a fraction of a sec before
    //painting the next number. This wait also allows other events like "Exit" to be processed
    //Use double buffering to avoid flicker
    //Paint in a different thread to keep the UI responsive

    public class MyMIDlet extends MIDlet{

    int x=1;
    String b="";
    private Display display;
    Thread t = null;
    MyCanvas mc = null;

    public MyMIDlet() {
    display = Display.getDisplay(this);
    }

    public void startApp() {
    mc = new MyCanvas();
    t = new Thread(mc) ;
    display.setCurrent(mc) ;
    b=String.valueOf(x);
    t.start();
    }

    public void callbackPaintDone(){
    try{
    //provides the delay between every repaint and also time to react to Exit
    synchronized(this){
    wait(1);
    }
    }catch(Exception e){}

    paintNextNumber();
    }
    private void paintNextNumber(){

    if(x<30000){
    x++;
    b=String.valueOf(x);
    mc.doPaint = true; //signals that next number can be painted
    }
    }
    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    class MyCanvas extends Canvas implements Runnable{
    Image buf = null;
    Graphics bg = null;
    public boolean doPaint = true;
    public MyCanvas(){
    buf = Image.createImage(getWidth(),getHeight());
    bg = buf.getGraphics();
    }
    public void paint(Graphics g){
    bg.setColor(255,255,255);
    bg.fillRect(0,0,getWidth(),getHeight());
    bg.setColor(0);
    bg.drawString(b,10,10,0);
    g.drawImage(buf,10,10,Graphics.TOP|Graphics.LEFT);
    // Callback for next number to be painted
    MyMIDlet.this.callbackPaintDone();
    }

    public void run(){
    while(true){
    if(doPaint){ //only paints when a number has been painted, and next one is ready to be painted
    doPaint = false;
    repaint();
    serviceRepaints();
    }
    }
    }
    }


    }
    Cheers,
    Nitin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •