Home J2me Creating Menu Using Canvas Class



Creating Menu Using Canvas Class
Posted on: October 24, 2008 at 12:00 AM
This example shows how to create the menu and call the canvas class to show the toggle message. The Toggle message will appear when user perform some action like click on a button ("Show").

Creating Menu Using Canvas Class

     

This example shows how to create the menu and call the canvas class to show the toggle message. The Toggle message will appear when user perform some action like click on a button ("Show"). In this example we have used the following method:

  • setColor()
  • fillRect()
  • getWidth()
  • getHeight()
  • getFont()
  • fontHeight()
  • fontWidth()
  • setFont()
  • drawString()
  • repaint()

The repaint() method is used to appear the string on Canvas form.

The application look like as follows:

 

Source Code For CanvasMenu.java

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

public class CanvasMenu extends MIDlet implements CommandListener{
  CanvasString canvas;
  private Command exit;
  private Command toggle;

  public CanvasMenu() {
  canvas = new CanvasString();
  }

  public void startApp() throws MIDletStateChangeException {
  Display.getDisplay(this).setCurrent(canvas);
  exit = new Command("Exit", Command.EXIT, 7);
  toggle = new Command("Show", Command.SCREEN, 1);
  canvas.addCommand(exit);
  canvas.addCommand(toggle);
  canvas.setCommandListener(this);
  canvas.repaint();
  }

  public void destroyApp(boolean unconditional){
  notifyDestroyed();
  }

  public void pauseApp(){}

  public void commandAction(Command c, Displayable s){
  String label = c.getLabel();
  if(label.equals("Show")){
  canvas.toggleString();
  } else if(label.equals("Exit")) {
  destroyApp(false);
  }
  }
}

class CanvasString extends Canvas {
  boolean string = true;
  void toggleString() {
  string = !string;
  repaint();
  }

  public void paint(Graphics g) {  
  g.setColor(0xccff66);
  g.fillRect(0, 0, getWidth(), getHeight());
  if(string) {
  Font font = g.getFont();
  int fontHeight = font.getHeight();
  int fontWidth = font.stringWidth("This is the Toggle Message");
  g.setColor(223, 0, 112);
  g.setFont(font);
  g.drawString("This is the Toggle Message", (getWidth()-fontWidth)/2,
  (getHeight()-fontHeight)/2, g.TOP|g.LEFT);
  }
  }
} 

Download Source Code

Related Tags for Creating Menu Using Canvas Class:
cclassormformbuttoniomenucanvasuserclickclithismessageactionpeartoggleappcreatelikeshowforexamplecalltoexamwssheareilcanliusepermcalasmsageoggcaoggclesallagemehowppxawhenxampsssasoessatkisllmpleaandaractsavattssthshoappleplndonomo


More Tutorials from this section

Ask Questions?    Discuss: Creating Menu Using Canvas Class  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.