×

Creating your first LCDUI MIDlet using Canvas

This topic aims to help you create your first LCDUI MIDlet using Canvas. The following list of steps demonstrate how to create a simple HelloWorld LCDUI MIDlet using Canvas that displays the Hello World label on the LCDUI Canvas .

Assumption

This guide assumes that you are already usingNokia Asha SDK 1.2 or older. If not refer to, Nokia Asha overview page

Steps

  1. From the Start menu, select Nokia Asha SDK > Nokia IDE for Java ME (Eclipse) v3.1. The Workspace Launcher dialog appears.

    Figure 1. Selecting the workspace

  2. Click OK to use the default workspace. This launches Nokia IDE for Java ME (Eclipse).

  3. Create a new MIDlet project by selecting File > New > MIDlet Project. The New MIDlet project window appears.

  4. Specify HelloWorldLCDUICanvas in the Project name field and click Finish.

    Figure 2. Creating a New MIDlet project

    The newly created MIDlet project appears on Nokia IDE for Java ME (Eclipse).

    Figure 3. HelloWorldLCDUICanvas MIDlet project on Nokia IDE for Java ME (Eclipse)

  5. In the Package Explorer view, right-click on HelloWorldLCDUICanvas > src and select New > Java ME MIDlet. The New Java ME MIDlet dialog appears.

    Figure 4. Creating a Java ME MIDlet

  6. Specify the following details in the New Java ME MIDlet window and click Finish:

    • Package: A package name for your Java ME MIDlet, for example, com.example.helloworld.

    • Name: A name for your Java ME MIDlet, for example, HelloWorldMIDlet.

    Figure 5. Specifying the Java ME MIDlet details

    The HelloWorldMIDlet.java is loaded into the workspace.

    Figure 6. HelloWorldMIDlet.java is loaded into the workspace

  7. In the Package Explorer view, right-click on HelloWorldLCDUICanvas > src > com.example.helloworld and select New > Class. The New Java Class dialog appears.

    Figure 7. Creating the HelloScreen class

  8. In the New Java Class dialog, specify the following details and click Finish:

    • Package: A package name for your Java ME MIDlet, for example, com.example.helloworld.

    • Name: A name for your Java class, for example, HelloWorldCanvas.

    Figure 8. New Java Class dialog

    The HelloWorldCanvas class is loaded into your workspace.

    Figure 9. HelloWorldCanvas class loaded into your workspace

  9. Replace the existing code of HelloWorldCanvas.java with the following code and save your changes. This code implements the LCDUI Canvas by setting the background and foreground color. It also draws the Hello World string on the Canvas.

    package com.example.helloworld;
    
    import javax.microedition.lcdui.Canvas;
    import javax.microedition.lcdui.Graphics;
    
    public class HelloWorldCanvas extends Canvas {
    	
    	// Constants
    	
    	/* Background color to fill the canvas */
    	protected static final int BACKGROUND_COLOR = 0xABABAB;
    	
    	/* Foreground color for text*/
    	protected static final int FOREGROUND_COLOR = 0xFF0000;
    	
    	/**
    	* Platform asks a canvas to draw/paint itself from time to time. This
    	* method handles that completely.
    	* @param g Graphics where canvas contents should be drawn on.
    	* @see javax.microedition.lcdui.Canvas#paint(javax.microedition.lcdui.Graphics)
    	*/
    	public void paint(Graphics g) {
        	/* Set background color to fill the canvas*/
        	g.setColor(BACKGROUND_COLOR);
        	
        	/* Fill canvas with background color*/
            g.fillRect(0, 0, getWidth(), getHeight());
            
            /* Set foreground color for Hello World!!! text*/
            g.setColor(FOREGROUND_COLOR);
            
            /* Draw Hello World!!! string on canvas */
            g.drawString("Hello World!!!", 10, 10, Graphics.TOP | Graphics.LEFT);
        } 
        
    }

    Figure 10. Updated HelloWorldCanvas.java source code

  10. Add the following import statement after package com.example.helloworld; in HelloWorldMIDlet.java. This imports the LCDUI component required for the MIDlet.

    import javax.microedition.lcdui.Display;

    Figure 11. Importing the required LCDUI component

  11. Declare and create a new Canvas instance in HelloWorldMIDlet.java by adding the following code after public class HelloWorldMIDlet extends MIDlet {.

    	/* Canvas instance*/
    	HelloWorldCanvas canvas;
    	
    	public HelloWorldMIDlet() {
    		/* Create canvas instance*/
    		canvas = new HelloWorldCanvas();
    	}

    Figure 12. Creating a new Canvas instance

  12. Replace the existing code of startApp() method in HelloWorldMIDlet.java with the following code and save your changes. This code sets Canvas as the display and paints the Canvas with the text Hello World.

    	protected void startApp() throws MIDletStateChangeException {
    		/* Set canvas as the current displayable using Display instance*/
    		Display.getDisplay(this).setCurrent(canvas);
    
    	}

    Figure 13. Updating startApp() method code

    Figure 14. Updated HelloWorldMidlet.java source code

  13. Run the MIDlet project by right-clicking on the project and selecting Run As > Emulated Java ME MIDlet.

    Figure 15. Running the MIDlet project

    The MIDlet project output is displayed on the emulator as follows.

    Figure 16. HelloWorld output

Next steps


Last updated 5 March 2014

Back to top

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×