• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to make canvas paint immediately?

 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

My midlet code does the following in this order:

1. constructs a MyCanvas, myCanvas
2. display.setCurrent(myCanvas)
3. myCanvas.setHighlightedItem(3)

So I was expecting the MyCanvas methods to be called in this order:

1. MyCanvas() constructor
2. paint()
3. setHighlightedItem()

...but setHighlightedItem is called first. This is a problem as the setHighlightedItem() method depends on variables that are set in the paint() method.

Does anyone know how I can make the setHighlightedItem() method be called last?

Hoping serviceRepaints() may be the solution, I have tried using the code:

myCanvas = new MyCanvas();
display.setCurrent(myCanvas);
myCanvas.repaint();
myCanvas.serviceRepaints();
myCanvas.setHighlightedItem();

...but it doesn't solve the problem.

Any ideas welcomed!

Thanks,
James
 
Michael Yuan
author
Ranch Hand
Posts: 1427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The paint() method is invoked by the UI thread which is busy until your method exits. So, paint() is always called after setHighlightItem() in your code.

You need to move the application logic out of the paint() method.
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael,

I will try to move the logic code out of the paint() method. If that isn't possible, then I suppose it's a case of having to put the above code in a new thread(?).

Cheers,
James
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put into timer call back.
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not familiar with the Timer class. Can you tell me what the advantage would be doing it that way rather than just creating a new Thread?

Thanks,
James
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic