J2ME repaint

I'm having problems updating the display.

I have created a BusyCanvas which uses a timer and a task to display to the screen 'Busy.....' in an animated way.

Within a function in my MIDlet I call a fuction to download an image from the web. While downloading the 'Busy.....' must be displayed. The current display is set to BusyCanvas and the thread is running (tested it), but it wont call the paint method of BusyCanvas.

Here is the code sections...

//-

public class ImageViewer extends MIDlet {

//some code ....

//-

public void viewMap(String aFilename) throws Exception {

InambaCanvas canvas;

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

fBusyCanvas.start();

fImage = InambaCanvas.getImage( aFilename );

canvas = new InambaCanvas();

canvas.setImage( fImage );

canvas.addCommand( new Command( "Back", Command.CANCEL, 1 ) );

canvas.setCommandListener(this);

fBusyCanvas.stop();

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

}

}

//-

public class BusyCanvas extends Canvas {

static String fBusyMessage = "Busy";

static String fLoadingMessage = "Loading";

boolean fPainting;

String fDotted;

Timer fTimer;

TimerTask fTask;

//--

public BusyCanvas() throws Exception {

super();

fPainting = false;

fTimer = new Timer();

fTask = new BusyTask();

fDotted = "";

}

//--

protected void paint(Graphics g) {

if (!fPainting) {

fPainting = true;

g.setColor( 0xFFFFFF );

g.fillRect( 0, 0, getWidth(), getHeight() );

g.setColor( 0 );

g.drawString( fBusyMessage + fDotted, getWidth()/2, getHeight()/2, Graphics.HCENTER | Graphics.TOP );

fPainting = false;

}

}

//--

public void start() throws Exception {

fTimer.scheduleAtFixedRate( fTask, 0, 500 );

}

//--

public void stop() throws Exception {

fTimer.cancel();

}

//--

class BusyTask extends TimerTask {

//--

public BusyTask() throws Exception {

super();

}

//--

public void run() {

if (fDotted.length() > 5) {

fDotted = "";

} else {

fDotted = fDotted.concat( "." );

}

repaint();

}

//--

}

//--

}

[2401 byte] By [johnlw] at [2007-9-26 13:39:56]
# 1
Hi there,I got same problem.I tried to display "loading & waiting" canvas while processing other task, but it didn't work.If you already got answer, please tell me.Thanks
zack327 at 2007-7-2 14:22:49 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2

I eventually solved the problem.

Create a TimerTask as a child class within the MIDlet class. Instead of calling the function directly, let the TimerTask call the function you wish to execute.

This viewMap would look something like this

public void viewMap() throws Exception {

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

fBusyCanvas.start(BusyCanvas.BUSY_MSG);

fTimer.schedule( new DrawImageTask( this ), 100 );

}

which gives the MIDlet enough time to set the new Display.

public class DrawImageTask extends TimerTask {

MIDlet fParent;

//--

public DrawImageTask( MIDlet aParent ) throws Exception {

super();

fParent = aParent;

}

//--

public void run() {

try {

if (fImage != null) {

fInambaCanvas.setImage( fImage );

}

fBusyCanvas.stop();

Display.getDisplay(fParent).setCurrent(fInambaCanvas);

} catch (Exception e) {

System.out.println( e );

}

}

//--

johnlw at 2007-7-2 14:22:49 > top of Java-index,Java Mobility Forums,Java ME Technologies...