J2me Canvas Colorful Application – Lesson no. 7

Well, My previous tutorials on making mobile applications are enough for making the concepts on coding for mobile using java technology.  Today i want to let you know how to make colorful applications in j2me using J2me Canvas. After my previous lesson the question in your mind was, How to make good looking graphical user interface in j2me ? This is almost done from j2me canvas.

What is j2me Canvas?

As you created forms in last tutorials on Lesson no.6 (Simple Quiz Game in j2me). But the forms and the GUI (Graphical User Interface) doesn’t look so interesting. It looks boring. So the term CANVAS let us make good graphical user interface. We can see lots of applications with good graphics like different games, opera mini and etc. This all can be done via Jme Canvas. It is one of the API provided for J2me developers.

Some of the commonly used methods in J2me Canvas are :

  • getGameAction(int keyCode) 
  • getHeight()
  • getKeyCode(int gameAction) 
  • getKeyName(int keyCode)
  • getWidth()
  • hasPointerEvents()
  • hasPointerMotionEvents() 
  • hasRepeatEvents()
  • hideNotify() 
  • isDoubleBuffered()
  • keyPressed(int keyCode) 
  • keyReleased(int keyCode) 
  • keyRepeated(int keyCode) 
  • paint(Graphics g)
  • pointerDragged(int x, int y) 
  • pointerPressed(int x, int y) 
  • pointerReleased(int x, int y) 
  • repaint() 
  • repaint(int x, int y, int width, int height) 
  • serviceRepaints() 
  • showNotify()

Source :

The following codes is good example for j2me canvas.

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

 * This is the main class of the hello world demo.
 @author pradeep
public class Hello extends MIDlet implements CommandListener {

   * The canvas is the region of the screen that has been allotted 
   * to the game.
  HelloCanvas myCanvas;

   * The Command objects appear as buttons in this example.
  private Command exitCommand = new Command(“Exit”, Command.EXIT, 99);

   * The Command objects appear as buttons in this example.
  private Command toggleCommand = new Command(“Toggle Msg”, Command.SCREEN, 1);

   * Initialize the canvas and the commands.
  public Hello() {
    myCanvas = new HelloCanvas();
    // we set one command listener to listen to all 
    // of the commands on the canvas. Commandlistener is already explained in previous tutorials.

  //  implementation of MIDlet

   * Start the application.
  public void startApp() throws MIDletStateChangeException {
    // display my canvas on the screen:
   * If the MIDlet was using resources, it should release 
   * them in this method.
  public void destroyApp(boolean unconditional
      throws MIDletStateChangeException {

   * This method is called to notify the MIDlet to enter a paused 
   * state.  The MIDlet should use this opportunity to release 
   * shared resources.
  public void pauseApp() {

  //  implementation of CommandListener

   * Respond to a command issued on the Canvas.
   * (either reset or exit).
  public void commandAction(Command c, Displayable s) {
    if(c == toggleCommand) {
    else if(c == exitCommand) {
      try {
      catch (MIDletStateChangeException ex) {

 * This class represents the region of the screen that has been allotted 
 * to the game.
 @author pradeep
class HelloCanvas extends Canvas {

  //   fields

   * whether or not the screen should currently display the 
   * “hello world” message.
  boolean mySayHello = true;

  //    initialization and game state changes

   * toggle the hello message.
  void toggleHello() {
    mySayHello = !mySayHello;

  //  graphics methods

   * clear the screen and display the hello world message if appropriate.
  public void paint(Graphics g) {
    // get the dimensions of the screen:
    int width = getWidth();  //gets width of the screen
    int height = getHeight(); // gets height of the screen
    // clear the screen (paint it white):
    // The first two args give the coordinates of the top 
    // left corner of the rectangle.  (0,0) corresponds 
    // to the top left corner of the screen.
    g.fillRect(00, width, height);
    // display the hello world message if appropriate:.
    if(mySayHello) {
      Font font = g.getFont();
      int fontHeight = font.getHeight();
      int fontWidth = font.stringWidth(“Hello World!”);
      // set the text color to red:
      // write the string in the center of the screen
      g.drawString(“Hello World!”(width – fontWidth)/2
       (height – fontHeight)/2,



Well, Crack this code, Modify them and see for more developments. After you understood this first example of J2me Canvas then you are ready to make good graphics in j2me. Making custom buttons, textfields etc are  only possible from j2me canvas. Ahead we will learn more about creating buttons, textfields etc. You will probably want to learn more about following things. Follow them below they are probably available.

  1. For example Making login screen with textboxes, buttons, checkbox etc. (Custom better looking fields)
  2. Getting user key codes. ( Determining what button is user clicking)
  3. Switching between canvases
  4. Making simple canvas games.

Soon this lessons will be available here or they are already available. These things will make you perfect j2me good looking applications and game developer.



Author: pradeep

Share This Post On

Submit a Comment