Category Archives: J2ME

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]