MIDP 2.0手机上的g.drawString失败了

时间:2014-02-05 07:10:18

标签: java midp

我做了一个简单的(我认为)演示,在背景中播放音乐,而动画的128x128蜡烛精灵在背景中闪烁,文本显示并使用TimerTask每2秒刷新一次。这个演示在模拟器(MicroEmulator)中运行良好,但在我的LG500G和摩托罗拉EM326G手机上的音乐都失败了。因为他们都以同样的方式失败,我怀疑我可能做错了什么。这两款手机都不会使用g.drawString()显示任何文字。要么我的手机受到严重限制,要么我写的东西非常奇怪:(注意我已经注释了关于Sprite的代码,因为只显示了一个框架)

    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 个答案:

答案 0 :(得分:0)

我怀疑错误是由您多次调用flushGraphics()

引起的

您对flushGraphics的第一次调用将刷新图形并显示它(除非因为您第二次调用flushGraphics而无法显示任何内容)。 您之后第二次调用flushGraphics将不会刷新任何内容,导致无法显示任何内容。

试试这个: (与上面的代码相同,只需对flushGraphics的一个调用进行注释)。

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;
      }
    }
  }
}