【问题标题】:g.drawString fails on MIDP 2.0 phonesg.drawString 在 MIDP 2.0 手机上失败
【发布时间】:2014-02-05 07:10:17
【问题描述】:

我做了一个简单的(我想的)演示,它在背景中播放音乐,而动画 128x128 蜡烛 Sprite 在背景中闪烁,文本显示并使用 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;
        }
    }

}

    }

【问题讨论】:

    标签: java midp


    【解决方案1】:

    我怀疑这个错误是由您多次调用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;
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-28
      • 2010-10-11
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 2010-12-10
      • 1970-01-01
      相关资源
      最近更新 更多