Java ME 中的奇怪错误

发布于 2024-11-26 14:16:15 字数 1339 浏览 1 评论 0原文

我刚刚开始深入研究 Java ME 的奇迹,但在尝试创建线程时感到沮丧......

下面是编译得非常好的代码。然而,当我在 G600 上安装并运行它时,就会弹出“Java 游戏错误”。

我将其放入 jar 文件并安装的方法是有效的,因为我创建了一个没有线程的游戏并且运行良好。

import java.util.Random;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.midlet.*;

public class CanvasTest extends MIDlet {
Display display;

public CanvasTest() {

}

public void startApp() {
  TestCanvas thecanvas = new TestCanvas();
  display = Display.getDisplay(this);
  display.setCurrent(thecanvas);
}

public void pauseApp() {}

public void destroyApp(boolean unconditional) {}
}
class TestCanvas extends GameCanvas implements Runnable {
Font font;

int width;
int height;

boolean running = true;

public TestCanvas() {
    super(false);
    setFullScreenMode(true);
    width = getWidth();
    height = getHeight();
    Thread thisThread = new Thread(this);
    thisThread.start();

}
public void paint(Graphics g) {
    Random rand = new Random();
    g.setColor(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
    g.fillRect(0, 0, width, height);

}
public void run() {
  while(running) {
    paint(getGraphics());

    flushGraphics();

    try {
        Thread.sleep(50);
    } 
    catch(InterruptedException ex) {}
  }
}
};

注意:是的,这不是游戏,它只是演示了我面临的问题。

提前致谢!

I've just starting delving into the wonders of Java ME but have become frustrated when trying to create a thread...

Below is the code which compiles absolutely fine. However, as soon as I install it on my G600 and run it, 'Java Game Error' pops up.

My method of putting it in a jar file and installing it works, as I have created a game with no threads and that works fine.

import java.util.Random;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.midlet.*;

public class CanvasTest extends MIDlet {
Display display;

public CanvasTest() {

}

public void startApp() {
  TestCanvas thecanvas = new TestCanvas();
  display = Display.getDisplay(this);
  display.setCurrent(thecanvas);
}

public void pauseApp() {}

public void destroyApp(boolean unconditional) {}
}
class TestCanvas extends GameCanvas implements Runnable {
Font font;

int width;
int height;

boolean running = true;

public TestCanvas() {
    super(false);
    setFullScreenMode(true);
    width = getWidth();
    height = getHeight();
    Thread thisThread = new Thread(this);
    thisThread.start();

}
public void paint(Graphics g) {
    Random rand = new Random();
    g.setColor(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
    g.fillRect(0, 0, width, height);

}
public void run() {
  while(running) {
    paint(getGraphics());

    flushGraphics();

    try {
        Thread.sleep(50);
    } 
    catch(InterruptedException ex) {}
  }
}
};

Note: yes, this is not the game, it merely demonstrates the problem I am facing.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲爱恨情仇 2024-12-03 14:16:15

只是一个大胆的猜测,但 Java 中的一般规则是您不能将 UI 从主线程中“触摸”出来。嗯,这个解释有点粗略,但是有很多关于这个主题的文章。

我建议您避免从单独的线程调用 paint()flushGraphics() 等 UI 方法。

我希望它有帮助。

Just a wild guess, but a general rule in Java is that you can't "touch" the UI out of the main thread. Well, this a little bit roughly explained, but there are many articles about the topic.

I suggest you to avoid calling UI methods like paint() or flushGraphics() from a separate Thread.

I hope it helps.

凉栀 2024-12-03 14:16:15

你在手机之前在模拟器上测试过吗?如果不是——为什么?如果是的话 - 进展如何?

关于代码,对我来说看起来不错,除了从构造函数创建和启动线程的两行。我宁愿将这两行移到 startApp 的末尾

public void startApp() {
    TestCanvas theCanvas= new TestCanvas();
    display = Display.getDisplay(this);
    display.setCurrent(theCanvas);
    new Thread(theCanvas).start(); // add here and...
}
//...
public TestCanvas() {
    super(false);
    setFullScreenMode(true);
    width = getWidth();
    height = getHeight();
    // ...and remove here
    // Thread thisThread = new Thread(this);
    // thisThread.start();
}

did you test it at emulator prior to phone? if not - why? if yes - how did it go?

regarding the code it looks OK to me except for the slippery two lines where you create and start thread from constructor. I'd rather move these two lines at the end of startApp

public void startApp() {
    TestCanvas theCanvas= new TestCanvas();
    display = Display.getDisplay(this);
    display.setCurrent(theCanvas);
    new Thread(theCanvas).start(); // add here and...
}
//...
public TestCanvas() {
    super(false);
    setFullScreenMode(true);
    width = getWidth();
    height = getHeight();
    // ...and remove here
    // Thread thisThread = new Thread(this);
    // thisThread.start();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文