Use canvas to make simple game menus in J2ME

Source: Internet
Author: User
Tags clear screen final getcolor interface variable
Menu We know MIDP's graphical user interface is divided into two categories, namely, the advanced graphical user interface and the low-level user interface. In general, the advanced graphical user interface class is easy to use, portability, but the programmer's control of his ability is also very low, because their interface performance is controlled by the bottom, rather than we control. Lower-level UI classes are more difficult to use than advanced UI classes, but have more control and can make the interface you need.

Canvas and graphics are the two classes that we have to be proficient at, representing the canvas and the brushes (in fact richer, metaphorically speaking). And we are painting people, and guide us how to write is the Java doc, coupled with hard work must be able to draw a good interface. such as tabbed menu, level two menu and so on. Here we describe a simple menu of how to make.

In painting the menu, you need to consider both sides of the problem, the first is to calculate the relative position, so that the menu can adapt to more models, as far as possible to use the absolute value. For example, when you draw the following menu

We should calculate the height of each item in the menu, which can be calculated with the height of the font, and of course you can leave some padding distance to the item. You should also calculate the widest value of the entry, after all, the number of words per item is not the same. This basically knows the entire menu occupies the space. Finally, you need to calculate the location of the menu on the screen. The menu is drawn as follows:

public void Paint (Graphics g) {
Clear Screen
int color = G.getcolor ();
G.setcolor (0xFFFFFF);
G.fillrect (0,0,getwidth (), getheight ());
G.setcolor (color);
Calculates the height, width, and (x,y) of the entire menu
int rectwidth = Preferwidth;
int rectheight = Preferheight * LABELS.LENGTH;
int x = (getwidth ()-rectwidth)/2;
int y = (getheight ()-rectheight)/2;
Draw a rectangle
G.drawrect (X,y,rectwidth,rectheight);
for (int i = 1;i<labels.length;i++) {
G.drawline (X,y+preferheight*i,x+rectwidth,y+preferheight*i);
}
Draw menu options and judge the focus based on selected values
for (int j = 0;j<labels.length;j++) {
if (selected = = j) {
G.setcolor (0x6699cc);
G.fillrect (x+1,y+j*preferheight+1,rectwidth-1,preferheight-1);
G.setcolor (color);
}
g.DrawString (labels[j],x+8,y+j*preferheight+4,graphics.left| Graphics.top);
}
}
The second important issue is the shift in focus, which is not required for us to handle in the advanced UI class. But using canvas to make the menu needs to handle the focus movement, here we define an int type variable selected to record the location of the menu item where the focus is located, which is the selected index. When the user presses the button, we in the keypressed () method to judge the user's direction of movement, selected for the associated subtraction operation, and then repaint () the entire screen.

public void keypressed (int keycode) {
Update the selected value based on user input and redraw the screen
int action = this.getgameaction (KeyCode);
Switch (action) {
Case Canvas.fire:
Printlabel (selected);
Break
Case Canvas.down:
Selected = (selected+1)%4;
Break
Case canvas.up:{
if (--selected < 0) {
selected+=4;
}
Break
}
Default
Break
}
Repaint ();
Servicerepaints ();
}
So we made a basic menu, you can also play the imagination to the selected menu to add animation effect. The Menucanvas code looks like this:

Package Com.j2medev.chapter3;

Import javax.microedition.lcdui.*;

public class Menucanvas extends canvas{

The selected variable marks the focus position
private int selected = 0;
private int preferwidth =-1;
private int preferheight =-1;
public static final int[] OPTIONS = {0,1,2,3};
public static final string[] labels={"New Game", "setttings", "High Scores", "Exit"};

Public Menucanvas () {
selected = Options[0];
Calculate the length and height values of menu options
Font f = font.getdefaultfont ();
for (int i = 0;i<labels.length;i++) {
int temp = F.stringwidth (labels[i]);
if (Temp > Preferwidth) {
Preferwidth = temp;
}
}
Preferwidth = Preferwidth + 2*8;
Preferheight = F.getheight () +2*4;
}

public void Paint (Graphics g) {
Clear Screen
int color = G.getcolor ();
G.setcolor (0xFFFFFF);
G.fillrect (0,0,getwidth (), getheight ());
G.setcolor (color);
Calculates the height, width, and (x,y) of the entire menu
int rectwidth = Preferwidth;
int rectheight = Preferheight * LABELS.LENGTH;
int x = (getwidth ()-rectwidth)/2;
int y = (getheight ()-rectheight)/2;
Draw a rectangle
G.drawrect (X,y,rectwidth,rectheight);
for (int i = 1;i<labels.length;i++) {
G.drawline (X,y+preferheight*i,x+rectwidth,y+preferheight*i);
}
Draw menu options and judge the focus based on selected values
for (int j = 0;j<labels.length;j++) {
if (selected = = j) {
G.setcolor (0x6699cc);
G.fillrect (x+1,y+j*preferheight+1,rectwidth-1,preferheight-1);
G.setcolor (color);
}
g.DrawString (labels[j],x+8,y+j*preferheight+4,graphics.left| Graphics.top);
}
}

public void keypressed (int keycode) {
Update the selected value based on user input and redraw the screen
int action = this.getgameaction (KeyCode);
Switch (action) {
Case Canvas.fire:
Printlabel (selected);
Break
Case Canvas.down:
Selected = (selected+1)%4;
Break
Case canvas.up:{
if (--selected < 0) {
selected+=4;
}
Break
}
Default
Break
}
Repaint ();
Servicerepaints ();
}
Shownotify () is called before paint ()
public void Shownotify () {
System.out.println ("Shownotify () is called");
}
private void Printlabel (int selected) {
System.out.println (labels[selected]);
}
}

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.