Implement the game function Selection interface (with source code) in j2_m2)

Source: Internet
Author: User
Tags clear screen

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 ();

}

}

 

 

}

}

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.