Home J2me Creating Menu Using Canvas Class

Related Tutorials


 
 

Share on Google+Share on Google+

Creating Menu Using Canvas Class

Advertisement
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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 24, 2008

Related Tutorials

Discuss: Creating Menu Using Canvas Class  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
mani
October 11, 2012
library

tell me about library nd canvas menu
moni
October 11, 2012
library

tell me about library nd meanin of library and meaning of "MANHI".....
DMCA.com