⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 menu.java

📁 anyview手机小说阅览器的开源代码
💻 JAVA
字号:
package com.ismyway.anyview;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * <p>Title: AnyView</p>
 *
 * <p>Description: E680(I) Reader</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: www.ismyway.com</p>
 *
 * @author ZhangJian
 * @version 1.0
 */
public class Menu extends AbstractWindow {
    public final static int ANCHOR = Graphics.LEFT | Graphics.TOP;
    private String[] items;
    private Image menu;
    private int itemIndex = 0; //选项停留时的索引
    private Image cube;
    private Image windowImage;

    final static int HorPad = 10;
    final static int VecPad = 10;
    final static int LinePad = 5;

    public Menu(String[] items) {
        this.items = items;

        if (items == null || items.length < 1) {
            return;
        }

        int max = 0;
        for (int i = 0; i < items.length; i++) {
            int c = AnyView.cf.stringWidth(items[i]);
            if (c >= max) {
                max = c;
            }
        }

        this.width = max + (HorPad << 1);
        this.height = items.length * (AnyView.cf.FontHeight + LinePad) -
                      LinePad + (VecPad << 1);
        if (AnyView.readType == 1) { //横屏
            this.LEFT = (AnyView.ScreenWidth - this.height) >> 1;
            this.TOP = (AnyView.ScreenHeight + this.width) >> 1;
        } else {
            this.LEFT = (AnyView.ScreenWidth - this.width) >> 1;
            this.TOP = (AnyView.ScreenHeight - this.height) >> 1;
        }
        this.LEFT = this.LEFT < 0 ? 0 : this.LEFT;
        this.TOP = this.TOP < 0 ? 0 : this.TOP;

        int[] c = new int[(width - 4) * (AnyView.cf.FontHeight + 2)];
        for (int i = 0; i < c.length; i++) {
            c[i] = 0x7FFFFFFF;
        }
        cube = Image.createRGBImage(c, (width - 4), CustomFont.FontHeight + 2, true);
        c = null;

        prepare();
    }


    public Menu(String[] items, int width, int height) {
        this.items = items;
        this.width = width;
        this.height = height;

        int[] c = new int[(width - 4) * AnyView.cf.FontHeight];
        for (int i = 0; i < c.length; i++) {
            c[i] = 0x7FFFFFFF;
        }
        cube = Image.createRGBImage(c, (width - 4), CustomFont.FontHeight, true);

        prepare();
    }

    private void prepare() {
        int blocks = width * height;
        if (blocks < 400) {
            return;
        }

        menu = Image.createImage(width, height);
        Graphics g = menu.getGraphics();
        g.setColor(AnyView.background);
        g.fillRect(0, 0, width, height);
        g.setColor(AnyView.bordorcolor);
        g.drawRect(0, 0, width - 1, height - 1);

        int y = VecPad;
        AnyView.cf.setColor(AnyView.fontcolor);
        for (int i = 0; i < items.length; i++) {
            Image img = AnyView.cf.buildImage(items[i]);
            g.drawImage(img, HorPad, y, ANCHOR);
            y += AnyView.cf.FontHeight + LinePad;
        }
    }

    public Image getWindow() {
        if (items == null || items.length < 1) {
            return null;
        }

        if (cube != null && itemIndex > -1) {
            windowImage = null;
            windowImage = Image.createImage(width, height);
            Graphics g = windowImage.getGraphics();
            g.drawImage(menu, 0, 0, ANCHOR);
            int y = VecPad +
                    itemIndex * (CustomFont.FontHeight + LinePad) - 1;
            g.drawImage(cube, 2, y, ANCHOR);

            g.setColor(0xffffff);
            g.setStrokeStyle(Graphics.DOTTED);
            g.drawRoundRect(2, y, width - 4, CustomFont.FontHeight + 2,
                            8, 8);
            g.setStrokeStyle(Graphics.SOLID);

            return windowImage;
        }
        return menu;
    }

    public void destroy() {
        items = null;
        menu = null;
        cube = null;
        windowImage = null;
    }

    public void move(int step) {
        if (items == null || items.length < 1) {
            return;
        }

        itemIndex += step;
        itemIndex = itemIndex < 0 ? items.length - 1 : itemIndex;
        itemIndex = itemIndex > items.length - 1 ? 0 : itemIndex;
    }

    public int getSelectedIndex() {
        return itemIndex;
    }

    public int getItemIndex() {
        return itemIndex;
    }

    public boolean bof() {
        return true;
    }

    public void keyUp() {
        move( -1);
    }

    public void keyDown() {
        move(1);
    }

    public void keyFire() {
        WINDOW_STATE = CLOSED;
    }

    protected void pointerPressed(int x, int y) {
        super.pointerPressed(x, y);
        if (PRESSED_X < 0 || PRESSED_X > width) {
            return;
        }

        //判断用户当前的选择项
        int find = -1;
        for (int i = 0; i < items.length && find == -1; i++) {
            int top = VecPad + i * (CustomFont.FontHeight + LinePad) - 1;
            if (PRESSED_Y > top && PRESSED_Y < top + CustomFont.FontHeight) {
                find = i;
                itemIndex = i;
                //System.out.println("pressed: itemIndex = " + itemIndex);
            }
        }
    }

    protected void pointerDragged(int x, int y) {

    }

    protected void pointerReleased(int x, int y) {
        TOUCH_EVENT = false;
        super.pointerReleased(x, y);
        if (RELEASE_X < 0 || RELEASE_X > width) {
            return;
        }

        //判断用户当前的选择项
        int find = -1;
        for (int i = 0; i < items.length && find == -1; i++) {
            int top = VecPad + i * (CustomFont.FontHeight + LinePad) - 1;
            if (RELEASE_Y > top && RELEASE_Y < top + CustomFont.FontHeight) {
                find = i;
                //System.out.println("released: find = " + find);
            }
        }

        if (find == -1) {
            return;
        }

        if (find == itemIndex) { //用户确认选择功能
            TOUCH_EVENT = true;
            keyFire();
        }

        else {
            itemIndex = find;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -