The use of the two menu types

Source: Internet
Author: User

Yesterday, the supervisor asked me to make a menu. I understood the question wrong.

He started to create a command menu. Later, he said that he would use canvas to draw a canvas. Then he made a very ugly menu and was beaten back. After reading the menu style of other software, he wrote a new one.

 

1. command menu

The menu code is as follows:

Package karant. zhan;

Import javax. microedition. lcdui. Alert;
Import javax. microedition. lcdui. Command;
Import javax. microedition. lcdui. CommandListener;
Import javax. microedition. lcdui. Display;
Import javax. microedition. lcdui. Displayable;
Import javax. microedition. lcdui. Form;
Import javax. microedition. midlet. MIDlet;
Import javax. microedition. midlet. MIDletStateChangeException;

Public class MenuDemo extends MIDlet implements CommandListener {

Display display;
Form form;
Alert alert;
Private Command backCommand = new Command ("Back", Command. BACK, 1 );
Private Command exitCommand = new Command ("Exit", Command. EXIT, 60 );
Private Command ebestCommand = new Command ("Ebest", Command. SCREEN, 22 );
Private Command jordanCommand = new Command ("Jordan", Command. OK, 23 );
Private Command whutCommand = new Command ("Whut", Command. SCREEN, 24 );
Private Command y33Command = new Command ("ye33", Command. SCREEN, 25 );

Public MenuDemo (){
Display = Display. getDisplay (this );
Form form = new Form ("Menu Test ");
Form. addCommand (exitCommand );
Form. addCommand (ebestCommand );
Form. addCommand (backCommand );
Form. addCommand (jordanCommand );
Form. addCommand (whutCommand );
Form. addCommand (y33Command );
Form. setCommandListener (this );
Alert = new Alert ("");
Display. setCurrent (form );
}

Protected void destroyApp (boolean arg0) throws MIDletStateChangeException {

}

Protected void pauseApp (){
// TODO Auto-generated method stub

}

Protected void startApp () throws MIDletStateChangeException {
 

}

Public void commandAction (Command c, Displayable d ){

If (c = ebestCommand)
{
Alert. setString ("Hello Ebest ");
Display. setCurrent (alert );

}
Else if (c = whutCommand)
{
Alert. setString ("Hello Whut ");
Display. setCurrent (alert );
}
Else if (c = y33Command)
{
Alert. setString ("Love y33 ");
Display. setCurrent (alert );
}
Else if (c = jordanCommand)
{
Alert. setString ("My Ido Jordan ");
Display. setCurrent (alert );
}
Else if (c = exitCommand)
{
Try {
DestroyApp (false );
} Catch (MIDletStateChangeException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Yydestroyed ();
}

}

}


Images cannot be uploaded

 

2. The following is a canvas image. The focus is better.

 

The MIDlet class code is as follows:

Package karant. zhan;

Import javax. microedition. lcdui. Display;
Import javax. microedition. midlet. MIDlet;
Import javax. microedition. midlet. MIDletStateChangeException;

Public class DrawMenuMIDlet extends MIDlet {

MenuAction menuAction;
Display display;
 
Public DrawMenuMIDlet (){
Display = Display. getDisplay (this );
MenuAction = new MenuAction (this );
}

Protected void destroyApp (boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub

}

Protected void pauseApp (){
// TODO Auto-generated method stub

}

Protected void startApp () throws MIDletStateChangeException {
// TODO Auto-generated method stub

}

}

 

 

Below is the MenuCanvas class for drawing menus

Package karant. zhan;

Import javax. microedition. lcdui. Font;
Import javax. microedition. lcdui. Graphics;
Import javax. microedition. lcdui. game. GameCanvas;

Public class MenuCanvas {

Private String leftOption;
Private String rightOption;
Private String backOption = "back ";
Private String [] menuOptions;
Private int padding = 5; // The distance between the border and the screen

// Color settings
Private static final int defaulFontColor = 0x000000; // default font color
Private static final int selectedFontColor = 0XB0171F; // font color after selection
Private static final int bgColor = 0xfec6e9; // menu background color
Private static final int heighLight = 0xDA70D6; // The highlighted color of the selected menu

// Constructor Initialization
Public MenuCanvas (String leftOption, String rightOption,
String [] menuOptions ){
This. leftOption = leftOption;
This. rightOption = rightOption;
This. menuOptions = menuOptions;
}
 
 
// Draw the default display status
Public void drawInactiveMenu (GameCanvas canvas, Graphics g ){
Font font = Font. getFont (Font. FACE_SYSTEM, Font. STYLE_PLAIN, Font. SIZE_MEDIUM); // obtain the Font
Int fontHeignt = font. getHeight ();
Int width = canvas. getWidth ();
Int height = canvas. getHeight ();
G. setColor (bgColor); // sets the background.
G. fillRoundRect (0, height-fontHeignt-2 * padding, width, height, 10, 10); // draw the rectangular box of the initial left and right menu position
G. setFont (font); // set the font.
G. setColor (defaulFontColor); // you can specify the default font color.
G. drawString (leftOption, padding, height-padding, g. LEFT | g. BOTTOM); // draw the LEFT button
G. drawString (rightOption, width-padding, height-padding, g. RIGHT | g. BOTTOM); // draw the RIGHT button
Canvas. flushGraphics (); // refresh the set items



}

// Draw the status of the selected menu
Public void drawActiveMenu (GameCanvas canvas, Graphics g, int select ){
Font font = Font. getFont (Font. FACE_SYSTEM, Font. STYLE_PLAIN, Font. SIZE_MEDIUM); // set the Font
Int fontHeight = font. getHeight (); // font height
Int width = canvas. getWidth (); // screen width
Int height = canvas. getHeight (); // screen height
G. setColor (bgColor); // you can specify the background color of a menu.
G. fillRoundRect (0, height-fontHeight-2 * padding, width, height, 10, 10); // draw the rounded rectangle of the menu option

// Draw the default left and right options
G. setFont (font );
G. setColor (defaulFontColor );
G. drawString (leftOption, padding, height-padding, g. LEFT | g. BOTTOM );
// Draw "Cancel" option
G. drawString (backOption, width-padding, height-padding, g. RIGHT
| G. BOTTOM );
Canvas. flushGraphics (); // After the settings are complete, the screen is refreshed.

If (menuOptions! = Null) {// when the array of the storage menu option is not empty
Int menuMaxWidth = 0; // maximum menu width
Int menuMaxHeight = 0; // maximum menu height
Int currentWidth = 0; // current width

// Select the maximum width of the sub-menu
For (int I = 0; I <menuOptions. length; I ++ ){
CurrentWidth = font. stringWidth (menuOptions [I]);
If (currentWidth> menuMaxWidth)
MenuMaxWidth = currentWidth;

MenuMaxHeight + = fontHeight + padding; // The height of the menu. If no menu option is added, the font height and distance are added accordingly.
}

MenuMaxWidth + = 6 * padding; // you can specify the width and 6 padding values.
G. setColor (bgColor );
G. fillRoundRect (0, // x
Height-fontHeight-menuMaxHeight-2 * padding, // y
MenuMaxWidth, menuMaxHeight, 10, 10); // create a rounded corner dialog box

G. setFont (font );
Int menuOptionX = 3 * padding; // The initial abscissa of the menu option
Int menuOptionY = height-fontHeight-2 * padding-menuMaxHeight + padding; // The initial ordinate of the menu options

// Add a line loop for each menu
For (int I = 0; I <= menuOptions. length; I ++)
{
G. setColor (heighLight); // The selected box color.
Int lineOptionY = height-fontHeight-2 * padding-menuMaxHeight + I * (fontHeight + padding); // Add a line between menus
G. drawLine (0, lineOptionY, menuMaxWidth-1, lineOptionY );
}

// Draw a loop of menu items
For (int I = 0; I <menuOptions. length; I ++ ){
If (I! = Select) {// if not selected, it is displayed by default.
G. setColor (defaulFontColor );
G. drawString (menuOptions [I], menuOptionX, menuOptionY, g. LEFT | g. TOP );
}
Else {// color displayed when the menu is selected
G. setColor (heighLight); // The selected color
Int heightY = height-fontHeight-2 * padding-menuMaxHeight + I * (fontHeight + padding );//
G. fillRoundRect (0, heightY, menuMaxWidth, fontHeight + padding, 10, 10 );
G. setColor (selectedFontColor );
G. drawString (menuOptions [I], menuOptionX, menuOptionY, g. LEFT
| G. TOP );
}
MenuOptionY + = padding + fontHeight; // after a menu is drawn, the coordinates are moved down.
}
Canvas. flushGraphics (); // display all the settings to the screen.
}

}
}

 

The following is a class for controlling desktop display and drawing.

 

MenuAction class

Package karant. zhan;

Import javax. microedition. lcdui. Canvas;
Import javax. microedition. lcdui. Graphics;
Import javax. microedition. lcdui. game. GameCanvas;
Import javax. microedition. midlet. MIDletStateChangeException;

Public class MenuAction extends GameCanvas {

Static int width; // screen width
Static int height; // screen height
 
Static final String [] menuOptions = {"status Settings", "Chat History", "Friend Management", "system settings", "logout", "help ", "About"}; // menu options
Static int menuChoose; // used to record the menu position for pressing the OK key
Private int select = 0; // the index value of the currently selected key
Private boolean menuIsActive = false; // indicates whether the mobile phone is in the menu selected status. The value is initialized to "no ".
Private DrawMenuMIDlet menumidlet; // MIDlet object
Private String leftOption;
Private String rightOption;
Private Graphics g;
Private MenuCanvas menu;
Private int LEFT_KEY =-6; // left function key of the mobile phone
Private int RIGHT_KEY =-7; // function key on the right of the mobile phone
 
Public MenuAction (DrawMenuMIDlet menumidlet ){
Super (false );
This. menumidlet = menumidlet;

// Obtain the screen width and height
Width = getWidth ();
Height = getHeight ();

SetFullScreenMode (true); // sets the full screen mode.

MenuChoose = 0; // Initialization
G = getGraphics (); // obtain the paint brush
LeftOption = "menu"; // you can specify the menu displayed on the left.
RightOption = "quit"; // you can specify the exit option on the right.

Menu = new MenuCanvas (leftOption, rightOption, menuOptions); // initialization Interface

Start (); // initialize your content on the screen
Menumidlet. display. setCurrent (this );
}

Public void start (){
ClearScreen (); // clear screen
Menu. drawInactiveMenu (this, g); // menu initial template settings
}
 
Public void clearScreen (){
G. setColor (0 xedffff); // sets the background color.
G. fillRect (0, 0, width, height); // fill the screen
FlushGraphics (); // refresh the screen
}
 
Protected void keyPressed (int keyCode ){
If (menuIsActive) {// if it is true, the menu option is displayed.
If (keyCode = RIGHT_KEY) {// if you press
ClearScreen (); // clear the screen
Menu. drawInactiveMenu (this, g );
MenuIsActive = false;
}
KeyCode = getGameAction (keyCode );
If (keyCode = UP) // press the UP key
{
Select --;
If (select <0)
Select = 0;
Menu. drawActiveMenu (this, g, select );
}
Else if (keyCode = DOWN) // press the DOWN key
{
Select ++;
If (select> 6)
Select = 6;

Menu. drawActiveMenu (this, g, select); // re-draw the directory selection status
}
Else if (keyCode = FIRE) {// press the OK key
ClearScreen (); // clear screen
G. setColor (, 0); // sets the display color.
G. drawString ("You selected:" + menuOptions [select], 10, 15, g. LEFT | g. TOP );
Menu. drawInactiveMenu (this, g); // return to the initial interface
MenuIsActive = false;
}
}
Else {
If (keyCode = LEFT_KEY) {// when the menu key is pressed
Menu. drawActiveMenu (this, g, select );
MenuIsActive = true;
}
Else if (keyCode = RIGHT_KEY) {// exit
Try {
Menumidlet. destroyApp (false );
} Catch (MIDletStateChangeException e ){
E. printStackTrace ();
}
Menumidlet. policydestroyed ();
}
}
}
 
}


 

The uploaded photo cannot be used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.