Use canvas to make simple game menus in J2ME

Source: Internet
Author: User
Tags clear screen

We know that 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 of the entire menu, width and (x,y)
int rectwidth = preferwidth;
int rectheight = preferheight * LABELS.LENGTH;
int x = (GetWidth ()-rectwidth)/2;
int y = (getheight ()-rectheight)/2;
//Draw 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 the menu option, and judge the Focus
for (int j = 0;j<labels.length;j++) {
if (selected = j) {
G.setcolor, based on the value of selected ( 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){
//根据用户输入更新selected的值,并重新绘制屏幕
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();
}

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.