Pham_loi
Chức vụ: 01:33:41, 27-05-2016 |
Tạo menu lựa chọn game
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 J2ME
tạo lớp Point.java
<?php
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;
}
}
?>
Copy code
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;
}
}
?>
Copy code
tạo lớp Menu.java kế thừa từ GameCanvas
<?php
package 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;
}
}
?>
Copy code
package 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;
}
}
?>
Copy code
Tạo 1 Midlet để test menu như sau
<?php
package 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) {
}
}
?>
Copy code
package 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) {
}
}
?>
Copy code
kết quả :
menu sẽ được chọn khi bấm phím up,down
chúc các bạn vui vẻ
: 0 ♥
Trực Tuyến:
Khách: 1