Please find the code below, that can print MainMenu on Canvas if you use it along the classes (codelets) that I gave in the wiki article on the this link: http://wiki.forum.nokia.com/index.ph...d_applications
Code:
import javax.microedition.lcdui.*;
/**
* MenuCanvas.java This class is for displaying the Main Menu
*/
public class MainCanvas implements CanvasScreen {
int selRectPos=0;
BaseCanvas bc;
CanvasScreen oldcs; //for back
public MainScreen() {
this.bc=BaseCanvas.getCanvas();
oldcs=bc.setCurrentScreen(this);
}
/**
* Renders the Canvas.
*@param g the Graphics object to be used for rendering the Canvas
*/
public void paint(Graphics g) {
showMainMenu(g);
}
public void keyRepeated(int keyCode){ }
public void repaint() {
bc.repaint();
}
/**
*Called when a key is pressed.
*@param keyCode - the key code of the key that was pressed
*/
public void keyPressed(int keyCode) {
switch(keyCode) {
case Canvas.UP: if(selRectPos>0) selRectPos--;
break;
case Canvas.DOWN: if(selRectPos<2) selRectPos++;
break;
case Canvas.RIGHT: // Exit
selRectPos=0;
bc.setCurrent(new ExitCanvas(this));
break;
case Canvas.LEFT: // Select
case Canvas.FIRE: // Fire
gotoModule();
break;
}
repaint();
}
/**
* It will go to the Particualr module depends upon swith case value
*/
private void gotoModule() {
switch(selRectPos) {
case 0: bc.setCurrent(new Module1Canvas(this)); // Module1
break;
case 1:bc.setCurrent(new Module2Canvas(this)); //Module2
break;
case 2:bc.setCurrent(new Module3Canvas(this)); //Module13
break;
}
}
//Menu Options.
String[] options={"Module1","Module2","Module3"};
int bgcolor=(255<<16)+(255<<8)+255; /** Background color - white. */
int fgcolor=(0<<16)+(0<<8)+0; /** Foreground color - black. */
int hilightcolor=(84<<16)+(171<<8)+67; /** Menu Highhilightcolor color. */
/** Gets the SMALL, BOLD Font used for displaying messages as bold*/
Font bldFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL);
/** Gets the SIZE_MEDIUM, BOLD Font used for displaying headers */
Font hedFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
/**
* Shows the Main Screen
*@param g Graphics object
*/
private void showMainMenu(Graphics g) {
int WIDTH=Device.WIDTH;
int HEIGHT=Device.HEIGHT;
int gap=20
g.setColor(bgcolor);
g.fillRect(0,0,WIDTH,HEIGHT);
g.setColor(fgcolor);
g.setFont(hedFont);
g.drawString("MainMenu",5+(WIDTH/2),5,g.TOP|g.HCENTER);
g.setFont(bldFont);
//Draw the Menu Options
for(int x=0;x<2;x++) {
g.drawString(options[x],60,65*gap,g.TOP|g.LEFT); // Print the Menu
}
//Highlight the menu
g.setColor(hilightcolor);
g.fillRoundRect(2, 55+(selRectPos)*gap, WIDTH-2, gap-3, 3, 3);
g.setColor(fgcolor);
g.drawString(options[selRectPos],60,65+(selRectPos)*gap,g.TOP|g.LEFT);
//Draw Line
g.drawLine(0,HEIGHT-35,WIDTH,HEIGHT-35);
g.drawString("Select", 2 , HEIGHT-31 , g.LEFT|g.TOP);
g.drawString("Fire", WIDTH/2 , HEIGHT-31 , g.HCENTER|g.TOP);
g.drawString("Exit", WIDTH - 2 , HEIGHT-31 , g.RIGHT|g.TOP);
}
}
The code Example also tell you about SplashCanvas.java
From your midlet you need to call this MainMenu or SplashCanvas and from this MainCanvas the control will go the following depening on the user action:
ExitCanvas
Module1Canvas
Module2Canvas
Module3Canvas
All these ModuleCanvas classes are to be made as the MainCanvas and they need to handle the user Inputs - keypress and control the flow of the app from within themselves.
thanks,
~Amitabh