g.drawString fails on MIDP 2.0 phones

58 Views Asked by At

I made a simple ( I thought) demo that plays music in the background while an animated 128x128 candle Sprite flickers in the background and text is displayed and refreshed every 2 seconds with a TimerTask. This demo works fine in the emulator (MicroEmulator), but failed on all counts but the music on both my LG500G and Motorola EM326G phones. Because they both failed in the same way, I suspect I may be doing something wrong. Neither of the phones will even display any text whatsoever using g.drawString(). Either my phones are severely limited, or I am writing something horribly weirdly:(note I have commented out the code about the Sprite because only one frame displayed)

    import java.io.IOException;
    import java.util.Timer;
    import java.util.TimerTask;
    import javax.microedition.lcdui.Graphics;
    import javax.microedition.lcdui.Image;
    import javax.microedition.lcdui.game.*;
    import javax.microedition.media.*;

    public class GardenGameCanvas extends GameCanvas implements Runnable {
private Image bgImage;
private Sprite bgSprite;
private boolean stop;
private LayerManager manager;
private int a = 0;
private String[] list;
textTask aTextTask;
Player midiplayer = null;

public GardenGameCanvas() {
    super(false);
}

public void start() {
    list = new String[] { "As you watch this candle", "It flickers.",
            "Against the gale of Life", "It's flickering.", "Persistently" };
    try {
        midiplayer = Manager.createPlayer(
                getClass().getResourceAsStream("/pavane_defunte_mb.mid"),
                "audio/midi");
        midiplayer.prefetch();
        midiplayer.start();
    } catch (Exception e) {
    }
    //try {

        //bgImage = Image.createImage("/flame.png");
        //bgSprite = new Sprite(bgImage, 128, 128);
        //manager = new LayerManager();
        //manager.append(bgSprite);


    //} catch (IOException ioex) {
    //  System.err.println(ioex);
    //}
    stop = false;
    Thread runner = new Thread(this);
    runner.start();

}

public void run() {
    aTextTask = new textTask();
    new Timer().schedule(aTextTask, 0, 2000);
    while (!stop) {

        update(getGraphics());
        try {
            Thread.currentThread().sleep(30);
        } catch (Exception e) {
        }

    }
}

private void update(Graphics g) {
    g.setColor(0xFFFFFF); // white
    g.fillRect(0, 0, getWidth(), getHeight());

    // bgSprite.setPosition(0, 0);
    //bgSprite.nextFrame();
    //bgSprite.paint(g);
    buildGame(g);
    //manager.paint(g, 0, 0);
    flushGraphics();
}

private void buildGame(Graphics g) {
    g.setColor(0xFFFFFF);
    g.fillRect(0, getHeight() / 2, getWidth(), 75);
    g.setColor(0x000000);
    g.drawString(list[a], 0, getHeight()/2, Graphics.LEFT);
    flushGraphics();
}

public class textTask extends TimerTask {
    public textTask() {
    }

    public void run() {
        a++;
        if (a > 4) {
            a = 0;
        }
    }

}

    }
1

There are 1 best solutions below

0
mr_lou On

I suspect the error is caused by your multiple calls to flushGraphics()

Your first call to flushGraphics will flush the graphics and display it (except it doesn't get a chance to display anything because of your second call to flushGraphics). Your second call to flushGraphics right after will flush nothing to the screen, resulting in nothing being displayed.

Try this instead: (Same code as above, simply with one of the calls to flushGraphics commented out).

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.*;
import javax.microedition.media.*;

public class GardenGameCanvas extends GameCanvas implements Runnable {

  private Image bgImage;
  private Sprite bgSprite;
  private boolean stop;
  private LayerManager manager;
  private int a = 0;
  private String[] list;
  textTask aTextTask;
  Player midiplayer = null;

  public GardenGameCanvas() {
    super(false);
  }

  public void start() {
    list = new String[]{"As you watch this candle", "It flickers.",
      "Against the gale of Life", "It's flickering.", "Persistently"};
    try {
      midiplayer = Manager.createPlayer(
              getClass().getResourceAsStream("/pavane_defunte_mb.mid"),
              "audio/midi");
      midiplayer.prefetch();
      midiplayer.start();
    } catch (Exception e) {
    }
    //try {

    //bgImage = Image.createImage("/flame.png");
    //bgSprite = new Sprite(bgImage, 128, 128);
    //manager = new LayerManager();
    //manager.append(bgSprite);


    //} catch (IOException ioex) {
    //  System.err.println(ioex);
    //}
    stop = false;
    Thread runner = new Thread(this);
    runner.start();

  }

  public void run() {
    aTextTask = new textTask();
    new Timer().schedule(aTextTask, 0, 2000);
    while (!stop) {

      update(getGraphics());
      try {
        Thread.currentThread().sleep(30);
      } catch (Exception e) {
      }

    }
  }

  private void update(Graphics g) {
    g.setColor(0xFFFFFF); // white
    g.fillRect(0, 0, getWidth(), getHeight());

    // bgSprite.setPosition(0, 0);
    //bgSprite.nextFrame();
    //bgSprite.paint(g);
    buildGame(g);
    //manager.paint(g, 0, 0);
    flushGraphics();
  }

  private void buildGame(Graphics g) {
    g.setColor(0xFFFFFF);
    g.fillRect(0, getHeight() / 2, getWidth(), 75);
    g.setColor(0x000000);
    g.drawString(list[a], 0, getHeight() / 2, Graphics.LEFT);
    //flushGraphics(); // Don't call flushGraphics here, because it'll be called twice then.
  }

  public class textTask extends TimerTask {

    public textTask() {
    }

    public void run() {
      a++;
      if (a > 4) {
        a = 0;
      }
    }
  }
}