J2ME- Tạo menu lựa chọn game
15.07.2014/14:38
bài này tôi sẽ hướng dẫn các bạn tạo menu với các lựa chọn trong J2ME1. tạo lớp Point.java
package Zstar;
import javax.microedition.xml.rpc.Operation;
/**
*
* @author zstar
*/
public class Point {
private int X = 0;
private int Y = 0;
public Point() {
}
public Point(int x, int y) {
this.X = x;
this.Y = y;
}
public int getX() {
return this.X;
}
public int getY() {
return this.Y;
}
}
2. tạo lớp Menu.java kế thừa từ GameCanvaspackage Zstar.menu;
import Zstar.Point;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
/**
*
* @author ZSTAR
*/
public class Menu extends GameCanvas {
private String[] options = {"New Game", "Map Editor", "High Score", "About", "Exit"};
private Point locationMenu = new Point(30, 30);
private int itemWidth;
private int item_space = 15;
private Image itemSelect;
private Image itemUnselect;
private Graphics g;
private int itemHeight;
private int currentIndex = 0;
private int white = 0xffffff;
private int black = 0x000000;
private Font font;
public Menu() {
super(false);
setFullScreenMode(true);
try {
this.g = this.getGraphics();
itemSelect = Image.createImage("/Images/butoption_Press.png");
itemUnselect = Image.createImage("/Images/butoption_unPress.png");
this.itemHeight = itemSelect.getHeight();
this.itemWidth = itemUnselect.getWidth();
this.font = Font.getFont(Font.STYLE_ITALIC, Font.FACE_SYSTEM, Font.SIZE_SMALL);
this.locationMenu = new Point(this.getWidth() / 2 - this.itemWidth / 2, this.locationMenu.getY());
g.setFont(font);
} catch (Exception e) {
};
}
public void Draw() {
for (int i = 0; i < options.length; i++) {
if (currentIndex == i) {
g.drawImage(itemSelect, locationMenu.getX(), locationMenu.getY() + i * itemHeight + item_space, Graphics.LEFT | Graphics.TOP);
} else {
g.drawImage(itemUnselect, locationMenu.getX(), locationMenu.getY() + i * itemHeight + item_space, Graphics.LEFT | Graphics.TOP);
}
g.drawString((i + 1) + ". " + options[i], locationMenu.getX() + 15, locationMenu.getY() + i * itemHeight + item_space + 5, Graphics.TOP | Graphics.LEFT);
this.flushGraphics();
}
}
private void clear_Item(int i) {
g.setColor(this.white);
g.fillRect(locationMenu.getX(), locationMenu.getY() + i * itemHeight + item_space, itemWidth, itemHeight);
g.drawImage(itemUnselect, locationMenu.getX(), locationMenu.getY() + i * itemHeight + item_space, Graphics.LEFT | Graphics.TOP);
g.setColor(this.black);
g.drawString((i + 1) + ". " + options[i], locationMenu.getX() + 15, locationMenu.getY() + i * itemHeight + item_space + 5, Graphics.TOP | Graphics.LEFT);
flushGraphics();
}
private void Draw_nextItem(int i) {
clear_Item(i);
g.drawImage(itemSelect, locationMenu.getX(), locationMenu.getY() + i * itemHeight + item_space, Graphics.LEFT | Graphics.TOP);
g.drawString((i + 1) + ". " + options[i], locationMenu.getX() + 15, locationMenu.getY() + i * itemHeight + item_space + 5, Graphics.TOP | Graphics.LEFT);
flushGraphics();
}
public void keyPressed(int keyCode) {
switch (getGameAction(keyCode)) {
case UP:
clear_Item(currentIndex);
menuSelectChange(-1);
Draw_nextItem(currentIndex);
break;
case DOWN:
clear_Item(currentIndex);
menuSelectChange(1);
Draw_nextItem(currentIndex);
break;
case FIRE:
//xu ly khi chon
;
break;
}
}
private void menuSelectChange(int h) {
//viec lua chon menu se chay vong tron
currentIndex += h + options.length;
currentIndex = currentIndex % options.length;
}
}
3. Tạo 1 Midlet để test menu như saupackage Zstar;
import Zstar.menu.*;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.*;
/**
* @author ZSTAR
*/
public class MenuMidlet extends MIDlet {
private Menu menu;
public void startApp() {
menu=new Menu();
menu.Draw();
Display.getDisplay(this).setCurrent(menu);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
kết quả:menu sẽ được chọn khi bấm phím up,down
chúc các bạn vui vẻ
Tag:
Bạn đến từ:
Tool tiện ích admin,KhoGame360,Blog thủ thuật,kenh380
,truyen23h
,tai hinh nen naruto,kenhpro - wap hay,vmt - wap hay, Trần Phú Hiền Blog
,truyen23h
,tai hinh nen naruto,kenhpro - wap hay,vmt - wap hay, Trần Phú Hiền Blog