Menu.java
上传用户:szhnad
上传日期:2020-12-30
资源大小:2k
文件大小:3k
- package com.cpt;
- 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;
- public Menu(String leftOption, String rightOption, String[] menuOptions) {
- this.leftOption = leftOption;
- this.rightOption = rightOption;
- this.menuOptions = menuOptions;
- }
- public void drawInactiveMenu(GameCanvas canvas, Graphics g) {
- 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(0xcccccc);
- g.fillRect(0,height-fontHeight-2 * padding, width,height);
- g.setFont(font);
- g.setColor(0x000000);
- g.drawString(leftOption, padding, height - padding, Graphics.LEFT | Graphics.BOTTOM);
- g.drawString(rightOption, width - padding, height - padding, Graphics.RIGHT | Graphics.BOTTOM);
- canvas.flushGraphics();
- }
- 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(0xcccccc);
- g.fillRect(0, height - fontHeight - 2 * padding, width, height);
- // draw default menu bar options
- g.setFont(font);
- g.setColor(0x000000);
- g.drawString(cancelOption, width - padding, height - padding, Graphics.RIGHT | Graphics.BOTTOM);
- canvas.flushGraphics();
-
- if (menuOptions != null) {
- int menuMaxWidth = 0;
- int menuMaxHeight = 0;
- int currentWidth = 0;
- for (int i = 0; i < menuOptions.length; i++) {
- currentWidth = font.stringWidth(menuOptions[i]);
- if (currentWidth > menuMaxWidth) {
- menuMaxWidth = currentWidth;
- }
- menuMaxHeight += fontHeight + padding;
- }
- menuMaxWidth += 2 * padding;
- g.setColor(0xcccccc);
- g.fillRect(0, // x
- height - fontHeight - 2 * padding - menuMaxHeight,
- menuMaxWidth,
- menuMaxHeight);
- g.setFont(font);
- int menuOptionX = padding;
- int menuOptionY = height - fontHeight - 2 * padding - menuMaxHeight + padding;
- for (int i = 0; i < menuOptions.length; i++) {
- if (i != selectedOptionIndex) {
- g.setColor(0x000000);
- } else {
- g.setColor(0x0000ff);
- }
- g.drawString(menuOptions[i], menuOptionX, menuOptionY, Graphics.LEFT | Graphics.TOP);
- menuOptionY += padding + fontHeight;
- }
- canvas.flushGraphics();
- }
- }
- }