Implement the game function Selection interface (with source code) in j2_m2)
Author: Chen yuefeng
From: http://blog.csdn.net/mailbomb
After the welcome interface is displayed in the j2_player game, the function Selection interface of the game should be displayed. There are two ways to achieve this interface:
1. Use the list in the advanced user interface for implementation
2. Use the canvas class to draw a function Selection interface.
In order to achieve better game performance, the second method is often used in the development process to achieve the game function Selection interface. The following is an example of using the canvas rendering function to select the interface.
Implementation principle: draw various functional menus of the game on the background image, use a rectangular box to represent the effect selected by the user, and move the selection box up and down based on the user's selection. The specific code is as follows:
Package welcomecanvas;
Import javax. microedition. lcdui .*;
Public class maincanvas extends canvas {
Image image = NULL;
/** Indicates the index number of the selected project */
Int Index = 1;
Public maincanvas (){
Try {
Image = image. createimage ("/RES/menu.png ");
} Catch (exception e ){
}
}
Protected void paint (Graphics g ){
// Clear screen
G. setcolor (255,255,255 );
G. fillrect (0, 0, getwidth (), getheight ());
G. setcolor (0, 0, 0 );
// Draw an image
G. drawimage (image, 0, 0, graphics. Top | graphics. Left );
// Draw a rectangle
G. drawrect (30,100 + (index-1) *, 27 );
}
Protected void keypressed (INT keycode ){
// Press the number key 2.
If (keycode = canvas. key_num2 ){
// Determine whether to move to the first option
If (Index = 1 ){
Index = 4;
} Else {
Index --;
}
}
// Press the number key 8.
If (keycode = canvas. key_num8 ){
// Determine whether to move to the fourth option
If (Index = 4 ){
Index = 1;
} Else {
Index ++;
}
}
// Re-draw the screen
Repaint ();
// If you press the number key 5
If (keycode = canvas. key_num5 ){
// Display different interfaces based on the selected items.
Switch (INDEX ){
Case 1:
Break;
Case 2:
Break;
Case 3:
Break;
Case 4:
Canvasmidlet. quitapp ();
}
}
}
}