// HelloMIDlet.java (c) Kari Laitinen // http://www.naturalprogramming.com // 2007-08-30 File created. // 2007-08-30 Last modification // This program is an example of a simple Java midlet that // shows two lines of text on the screen. // The text is drawn on a Canvas-based object that it attached // onto the display of the mobile phone. // See notes at the end of this file. import javax.microedition.lcdui.*; import javax.microedition.midlet.*; class HelloCanvas extends Canvas { int canvas_width = getWidth() ; int canvas_height = getHeight() ; public void paint( Graphics graphics ) { graphics.setColor( 255, 255, 255 ) ; // white color clears the canvas graphics.fillRect( 0, 0, canvas_width, canvas_height) ; graphics.setColor( 0, 0, 0 ) ; // black color is used for drawing graphics.drawString( "Hello. I am a Java midlet.", 20, 30, Graphics.TOP | Graphics.LEFT ) ; graphics.drawString( "The coordinates of this line are (20, 50)", 20, 50, Graphics.TOP | Graphics.LEFT ) ; } } public class HelloMIDlet extends MIDlet { Display display_of_this_midlet = Display.getDisplay( this ) ; Canvas canvas_of_this_midlet = new HelloCanvas() ; protected void startApp() throws MIDletStateChangeException { display_of_this_midlet.setCurrent( canvas_of_this_midlet ) ; } protected void pauseApp() { } protected void destroyApp( boolean unconditional_destruction_required ) { } } /**** NOTES: I have written the above classes without constructors as the data fields can be initialized with initializers. An alternative way to write the HelloCanvas class would be the following. class HelloCanvas extends Canvas { int canvas_width, canvas_height ; public HelloCanvas() { canvas_width = getWidth() ; canvas_height = getHeight() ; } ... It is usually mandatory to clear the screen at the beginning of the paint() method. See what happens if you comment out the first two statements of the paint() method. */