StopWatch MIDlet

To get a better handle on MIDP programming, I’m writing a quick and dirty stopwatch for my phone (the Nokia 3650, which didn’t come with a stopwatch, unlike the T68i I had before).

Basically, I’ve got a MIDlet class (container for the app), a Canvas class (displays the stopwatch) and a Thread class (updates the canvas every second). I’ve run into a couple interesting tidbits while trying to use the repaint() method of the Canvas class. At first I was having the Thread class update a public String object in the Canvas and then calling repaint() on the canvas, which according to this article on java.sun.com, should then clear the previous canvas and display the new canvas. Curiously, this resulted in each canvas repainting the display with the previous canvas showing through, which I’m guessing is what the above article was saying might happen when it said:

“… If you mark two or more areas for repainting within a short span of time, the system may combine the paint requests into a single request to paint the union of the two areas (the smallest rectangle containing both).”

For example, in my thread class, I’d update a property of the canvas:

canvas.setText(“this is the new time”);

and then call the repaint method:

canvas.repaint();

This resulted in “this is the new time” being written over whatever previously existed in the canvas. Do I have the wrong idea about how to update text in a canvas? Shouldn’t I just be able to update the text and call repaint()?

Anyways, I’ve ended up updating a public Image object in the Canvas from the Thread and then calling repaint(), which seems to work. Love to hear any suggestions about a better way!

Links:

Creating Low-Level GUI Components [onjava.com]

MIDP Canvas Repainting [java.sun.com]

Nokia Java Graphics Forum [discussion.forum.nokia.com]

3 thoughts on “StopWatch MIDlet”

  1. So where is the app? I’d like to have a stopwatch on my 3650 too. And how about some source code? I’d like to make a calculator app, and this source would probably be a great example to start from.

  2. Respected sir,
    Myself is Rekha,presently doing M.C.A ,i m working on J2ME.
    From your website i got the demo on stopwatch program.It is a excellent presentation on stopwatch.i request you to
    please send me the source code of it on my mail id
    rekha09@gmail.com.
    I will be very grateful to you for this.
    Thanks.
    Rekha

Leave a Reply

Your email address will not be published. Required fields are marked *