将canvas创建高级菜单(3)
}
}
}
3)Menu类
Java代码
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class Menu {
private String leftOption;
private String rightOption;
private String cancelOption = "Cancel";
private String[] menuOptions;
private int padding = 5;
//颜色设置
private static final int defaultFontColor = 0x000000; // 默认字体颜色
private static final int selectedFontColor=0xfff000; //选中后字体颜色
private static final int bgColor = 0xCCCCCC;// 菜单背景色
private static final int highLight=0x0000ff;//选中的菜单高亮显示颜色
public Menu(String leftOption, String rightOption, String[] menuOptions) {
this.leftOption = leftOption;
this.rightOption = rightOption;
this.menuOptions = menuOptions;
} // end constructor
public void drawInactiveMenu(GameCanvas canvas, Graphics g) {
// create inactive menu font
Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
Font.SIZE_MEDIUM);
int fontHeight = font.getHeight();
// clear inactive menu background
int width = canvas.getWidth();
int height = canvas.getHeight();
g.setColor(bgColor); // grey color
g.fillRect(0, height - fontHeight - 2 * padding, width, height);
// draw left and right menu options
g.setFont(font);
g.setColor(defaultFontColor); // black
g.drawString(leftOption, padding, height - padding, g.LEFT | g.BOTTOM);
g.drawString(rightOption, width - padding, height - padding, g.RIGHT
| g.BOTTOM);
canvas.flushGraphics();
} // end drawInactiveMenu
public void drawActiveMenu(GameCanvas canvas, Graphics g,
int selectedOptionIndex) {
Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
Font.SIZE_MEDIUM);
int fontHeight = font.getHeight();
int width = canvas.getWidth();
int height = canvas.getHeight();
g.setColor(bgColor);
g.fillRect(0, height - fontHeight - 2 * padding, width, height);
// draw default menu bar options
g.setFont(font);
g.setColor(0x000000); // black
g.drawString(leftOption, padding, height - padding, g.LEFT | g.BOTTOM);
// draw "Cancel" option
g.drawString(cancelOption, width - padding, height - padding, g.RIGHT
相关新闻>>
- 发表评论
-
- 最新评论 更多>>