简体   繁体   English

g.drawString在MIDP 2.0手机上失败

[英]g.drawString fails on MIDP 2.0 phones

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. 我做了一个简单的演示(我认为),该演示在后台播放音乐,而动画的128x128蜡烛Sprite在后台闪烁,并且使用TimerTask每2秒显示和刷新一次文本。 This demo works fine in the emulator (MicroEmulator), but failed on all counts but the music on both my LG500G and Motorola EM326G phones. 该演示程序可以在仿真器(MicroEmulator)上正常运行,但是除LG500G和摩托罗拉EM326G手机上的音乐外,其他所有方面都失败了。 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(). 使用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) 我的电话受到严重限制,或者我正在写一些奇怪的东西:(请注意,由于只显示了一帧,我注释掉了有关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;
        }
    }

}

    }

I suspect the error is caused by your multiple calls to flushGraphics() 我怀疑该错误是由您多次调用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). 第一次调用flushGraphics将刷新图形并显示它(除非由于第二次调用flushGraphics而没有机会显示任何内容)。 Your second call to flushGraphics right after will flush nothing to the screen, resulting in nothing being displayed. 紧随其后的第二次调用flushGraphics不会将任何内容刷新到屏幕上,从而不会显示任何内容。

Try this instead: (Same code as above, simply with one of the calls to flushGraphics commented out). 改为尝试以下操作:(与上面相同的代码,只是对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;
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM