1

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!

mdelolmo
  • 6,417
  • 3
  • 40
  • 58
Thomas
  • 13
  • 2

2 Answers2

1

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.

mdelolmo
  • 6,417
  • 3
  • 40
  • 58
  • I would have thought the display.setCurrent(thecanvas); would allow this... Anyhow, many examples seem to use this approach – Thomas Jul 26 '11 at 23:16
0

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();
}
gnat
  • 6,213
  • 108
  • 53
  • 73
  • Suggestion didn't alter the outcome, in fact this was my first approach and the posted code was an attempt at a work around (sorry about that). I will get back to you on how the emulator fares. – Thomas Jul 26 '11 at 23:20
  • very interesting - please post when you get an update. Also, did you somehow test/verify that re-installation happened indeed? I mean if there was some failure installing updated version then the outcome you observed might indeed be one of prior version? I'd check this with something like re-installing and launching "threadless" version (one that you mentioned worked fine) then re-installing most up-to-date again. Or better yet I'd create a 'splash screen' that displays first and shows some version identifier. Could be a simple form with textfield and a command 'Start' – gnat Jul 26 '11 at 23:47
  • Well this is embarrassing, I have a confession to make: I like to do things the 'manual' way - I was using the compile + add to jar + adjust jad, etc method... It turns out when using the SDK all works perfectly fine in the emulator and on the phone. I do intend to find out my mistake, and I will post back here if I do find out what went wrong. Credit to gnat though, for his suggestion of an emulator in the first place and generally helpful tips afterwards. – Thomas Jul 27 '11 at 00:39