tags:

views:

44

answers:

3

Hi please look this code , when i run it on nokia N97 its run very slow but i test it on samsung corby its run true , i think if i use game canvas problem be sloved . what should i do to solve this problem .

public class MIDPCanvas extends GameCanvas implements Runnable {

Graphics g;
Image img;
int x = getWidth() / 2;
Thread t = new Thread(this);

public MIDPCanvas() {
    super(true);

    try {
        img = Image.createImage("/pic.jpg");
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    g = getGraphics();
    g.setColor(150, 230, 200);
    g.fillRect(0, 0, getWidth(), getHeight());

    t.start();
}

public void run() {
    while (true) {
        g.drawImage(img, x, getHeight() / 2, Graphics.VCENTER | Graphics.HCENTER);
        x--;
        flushGraphics();
    }
   }
}

thanks

A: 

Since you have an infinite loop in run(), I would guess that one of the calls in the while loop blocks on the Corby, or that device is somehow throttling your process:

    while (true) {
        g.drawImage( ... );
        x--;
        flushGraphics();
    }
Justin Ethier
A: 

This is a very bad way of doing animation.

If you must use the infinite loop approach, at least include a yield() or Thread.sleep(100) on every iteration. But a much better approach is to use Display.callSerially(). This will ensure the device animates as fast as it can.

Google is your friend to explain all about how to use this method.

Personally, in many years of doing high-complexity J2ME apps with plenty of animation in, I have never used GameCanvas -- always Canvas, overriding the paint() method and animating using callSerially().

funkybro
i change my code - use canvas and callSerially() but now when i run it there is no move !
mahdi
A: 

this is new code but there is no move

public class MIDPCanvas extends Canvas implements Runnable {

Graphics g;
Image img;
int x = getWidth() / 2;
Thread t = new Thread(this);
Display display;

public MIDPCanvas(Display display) {

    this.display = display;
    try {
        img = Image.createImage("/pic.jpg");
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    t.start();
}

public void run() {
    while (true) {
        x--;
        repaint();
        display.callSerially(this);
    }
}

protected void paint(Graphics g) {
    g.drawImage(img, x, getHeight() / 2, Graphics.VCENTER | Graphics.HCENTER);
    display.callSerially(this);
}

}

mahdi
This is wrong; you don't understand how `callSerially()` works.The `Runnable` in `callSerially()` is not a regular Runnable that you use to instantiate a thread. Get rid of your `Thread t`, and get rid of the infinite loop in the `run()` method. The `run()` method should do just the animation logic (`x--`), and the `repaint()`. The MIDP lifecycle will call `run()` at an appropriate time, which will then trigger the repaint. Alternatively, you could do the animation logic in the `paint()` method.
funkybro
You'll probably find that this will cause much faster animation than desired, in which case you can measure the time between repaints to limit the rate.
funkybro
i found how to use callSerially() , and its work but still my problem not solved , its run on wtk very fast but in other emulator like LG or nokia its slow
mahdi