help with MIDP's setCurrent

ok, I have the simple program following this code. I had expected it to print "1", paint canvas1, print "2" and then paint canvas2. However, it just prints "1" and then hangs. It doesnt paint canvas1. Obviously I dont understand something about setCurrent(), but i've been scanning on the net and cant fingure out whats wrong.

Please CC me at dneiss@qualcomm.com Thanks.

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

public class test extends MIDlet implements Runnable

{

public void destroyApp(boolean b)

{System.out.println("destroyApp");}

public void pauseApp()

{System.out.println("pauseApp");}

public void startApp()

{

new Thread(this).start();

}

public void run()

{

Canvas c1,c2;

c1 = new canvas1();

c2 = new canvas2();

System.out.println("1");

Display.getDisplay(this).setCurrent(c1);

System.out.println("2");

Display.getDisplay(this).setCurrent(c2);

}

}

class canvas1 extends Canvas

{

public void paint(Graphics g)

{

g.drawLine(0,0,100,100);

}

}

class canvas2 extends Canvas

{

public void paint(Graphics g)

{

g.drawLine(0,100,100,0);

}

}

[1329 byte] By [dneiss] at [2007-9-26 10:26:36]
# 1

You forgot to implement the constructor to actually display the Canvas. Try to change your implementation of the canvas class to something similar like that:

...

class canvas1 extends Canvas {

canvas1(){

display.setCurrent( this );

}

protected void paint( Graphics g ){

g.drawLine( 0, 0, 100, 100);

}

...

MHartfiel at 2007-7-1 22:41:12 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2

Tried this suggestion and it didnt seem to help.

Also, by calling setCurrent() in the constructor, it couples the construction of the object with the time that it is set to the current display, which may not be desirable. I would prefer to precreate my objects and then at some later time make them the displated object.

Thanks though.

dneiss at 2007-7-1 22:41:12 > top of Java-index,Java Mobility Forums,Java ME Technologies...