Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Developer
Posts: 19
Registered: ‎08-30-2009
My Device: Not Specified
Accepted Solution

How to paint to screen consitently using a timer...

Hi all,

 

I'm having an issue painting to the screen using a timer. The timer updates my vairable (or "counter" ) consistently, but fails to draw the vaule to the screen consitently. That is, when I try to "draw" the new value of the counter to the screen, it doesn't draw it consistently. It will, however, draw it consistently when I press and hold a key down or hold the trackball in a certain direction (since I use them in the app). I'm not sure why this is the case exactly. Perhaps I need some sort of "listener". Thank you in advance for your help!

 

Here is some code from the timer...

 

    public class MainTimerClass {

        public MainTimerClass(final Graphics graphics)
        {
       
        super();
           
            game_time = new Timer();
            countdown = new TimerTask(){
               
                public void run() {

                          timestop--;
                       
                          String time_out = "" + timestop;

                          graphics.setColor(Color.WHITE);
                          graphics.fillRect(230, 5, 50, 30);
                          graphics.setColor(Color.DODGERBLUE);
                          graphics.setFont(myFont_18);
                       
                          graphics.drawText(time_out, 230,10);

                          }

            };
           
           
            game_time.scheduleAtFixedRate(countdown, 500, 1000);

            }
       
        }

Developer
Developer
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook
My Carrier: Verizon

Re: How to paint to screen consitently using a timer...

I think the issue is with your use of the Graphics object. Graphics objects can are context sensitive which means the same one will not always be used so the reference to the Graphics object that is received when your MainTimerClass is created may not be the one that is currently being drawn to. It would probably be best to make your MainTimerClass an internal class then you can use a global variable that is set within the run method and then call invalidate inside the run method as well.

 

ie

 

 

public void run(){ timestop--; String time_out = ""+timestop; invalidate(); }

 

Then you can override your paint method and call drawText within it.

 

Developer
Posts: 19
Registered: ‎08-30-2009
My Device: Not Specified

Re: How to paint to screen consitently using a timer...

Thank you for your response! I really appreciate it. As far as I know, my MainTimerClass is an internal class. That is, it's nested inside of my "final class mainScreen extends MainScreen" class. Your idea of overriding the paint method to drawText after using invalidate is a good one, but something I was trying to avoid if possible (that is - avoid using invalidate for the timer update). I'll try it out though, I just dont want invalidate to clear the rest of my screen (despite using the more specific invalidate option i.e. - invalidate(x,y,w,h) ).

 

Thanks again!

 

- j

Developer
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: How to paint to screen consitently using a timer...

Try with calling updateDisplay() at the last line inside the run method of your class MainTimerClass.

 

Regards

Bikas

Developer
Posts: 19
Registered: ‎08-30-2009
My Device: Not Specified

Re: How to paint to screen consitently using a timer...

Haha! Thanks so much bikas (and CMY) - updateDisplay did the trick!!!

 

- j