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

📄 neuscale.java

📁 Java ME中对多个按键同时处理的例子。
💻 JAVA
字号:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

public class NeuScale extends KeyEventCanvas {

    /** 绘制的抬笔点 */
    private int penUpX, penUpY;

    /** 比例尺显示的宽和高 */
    private int scaleWidth = 20, scaleHeigth = 38;

    /** 间隙的大小 */
    private int space = 2;

    /** 比例尺总的高 */
    private int height;

    private int heightBar = 4;

    /** 当期的位置 */
    private int curx, cury;

    /** 比例尺显示的块的个数 */
    private int count = 5;

    private int index = 3;

    /** 是否显示 */
    private boolean isVisible = true;

    private int[] color = new int[] { 0x0000FF, 0x4e7ddc, 0x81afdc, 0xa3c8f6,
            0xffb40c };

    private int[][] pos = new int[2][5];

    public NeuScale() {
        // 设置抬笔点
        this.penUpX = this.getWidth() * 26 / 30;
        this.penUpY = this.getHeight() / (this.count + 2);
        // 设置大小
        this.height = this.penUpY + this.scaleHeigth * this.count
                + (this.count - 1) * this.space;
        // 设置当前的比例的位置
        this.curx = this.penUpX - 3;
        this.cury = this.penUpY + this.index * this.scaleHeigth
                + (this.index + 1) * (this.space + this.heightBar);

        pos[0][0] = this.penUpX;
        pos[1][0] = this.penUpY;
        for (int i = 0; i < 4; i++) {
            pos[0][i + 1] = pos[0][i];
            pos[1][i + 1] = pos[1][i] + this.scaleHeigth + 2;
        }
    }

    public void paint(Graphics g) {
        if (!this.isVisible) {
            return;
        }

        g.setColor(0xFFFFFF);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());

        Font oraFont = g.getFont();
        Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
                Font.SIZE_MEDIUM);
        g.setFont(font);

        int strLen = font.stringWidth("汉字");
        int width = 35;
        int height = 22;

        int xx = (width - strLen) / 2;
        int yy = (height - font.getHeight()) / 2;

        int topX = this.penUpX - 8;
        int topY = this.penUpY - 25;
        g.setColor(0xa09eaa);
        g.fillRoundRect(topX + 5, topY + 5, width, height, 5, 5);
        g.setColor(0x20fcfbfa);
        g.fillRoundRect(topX, topY, width, height, 5, 5);
        g.setColor(0x29297f);
        g.drawRoundRect(topX, topY, width, height, 5, 5);

        g.drawString("扩大", topX + xx, topY + yy, Graphics.LEFT | Graphics.TOP);

        int bottomX = this.penUpX - 8;
        int bottomY = this.height + 5;
        g.setColor(0xa09eaa);
        g.fillRoundRect(bottomX + 5, bottomY + 5, width, height, 5, 5);
        g.setColor(0x20fcfbfa);
        g.fillRoundRect(bottomX, bottomY, width, height, 5, 5);
        g.setColor(0x29297f);
        g.drawRoundRect(bottomX, bottomY, width, height, 5, 5);
        g.drawString("缩小", bottomX + xx, bottomY + yy, Graphics.LEFT
                | Graphics.TOP);

        // 绘制5个小框
        for (int i = 0, y = this.penUpY; i < this.count; i++) {
            if (i == 0) {
                g.setColor(0xa09eaa);
                g.fillRect(penUpX + 5, y + 5, scaleWidth, scaleHeigth);
                g.setColor(this.color[i]);
                g.fillRect(penUpX, y, scaleWidth, scaleHeigth);
                g.setColor(0x29297f);
                g.drawRect(penUpX, y, scaleWidth, scaleHeigth);
                g.drawRoundRect(penUpX, y, scaleWidth, scaleHeigth, 5, 5);

            }
            if (i < this.count - 1 && i > 0) {
                g.setColor(0xa09eaa);
                g.fillRect(penUpX + 5, y + 5, scaleWidth, scaleHeigth);
                g.setColor(this.color[i]);
                g.fillRect(penUpX, y, scaleWidth, scaleHeigth);
                g.setColor(0x29297f);
                g.drawRect(penUpX, y, scaleWidth, scaleHeigth);
            }
            if (i == this.count - 1) {
                g.setColor(0xa09eaa);
                g.fillRect(penUpX + 5, y + 5, scaleWidth, scaleHeigth);
                g.setColor(this.color[i]);
                g.fillRect(penUpX, y, scaleWidth, scaleHeigth);
                g.setColor(0x29297f);
                g.drawRect(penUpX, y, scaleWidth, scaleHeigth);
            }
            y += (this.space + scaleHeigth);
        }

        // 绘制选中的小框
        g.setColor(0xFF0000);
        g.fillRect(pos[0][this.index], pos[1][this.index], this.scaleWidth,
                this.scaleHeigth);
        g.setColor(0x29297f);
        g.drawRect(pos[0][this.index], pos[1][this.index], this.scaleWidth,
                this.scaleHeigth);
        g.drawRect(pos[0][this.index] + 1, pos[1][this.index] + 1,
                this.scaleWidth - 2, this.scaleHeigth - 2);

        // 绘制移动的比例
        this.drawProcess(g, this.curx, this.cury, this.scaleWidth + 10,
                heightBar);

        g.setFont(oraFont);
    }

    private void drawProcess(Graphics g, int x, int y, int width, int height) {

        // 绘制比例尺上的滚动条
        g.setColor(0x00FF00);
        g.fillRect(x, y, width, height);
        g.setColor(0x29297f);
        g.drawRect(x, y, width, height);

        x = x - 1;
        int lx = x - 12, ly = y, mx = x - 6, my = y - 6, rx = x, ry = y;

        // 绘制小三角
        g.setColor(0xff9f3f);
        g.fillTriangle(lx, ly, mx, my, rx, ry);
        g.setColor(0x29297f);
        g.drawLine(lx, ly, mx, my);
        g.drawLine(mx, my, rx, ry);
        g.drawLine(lx, ly, rx, ry);

        y = y + height;
        my = y + 6;
        ly = y;
        ry = y;
        g.setColor(0xff9f3f);
        g.fillTriangle(lx, ly, mx, my, rx, ry);
        g.setColor(0x29297f);
        g.drawLine(lx, ly, mx, my);
        g.drawLine(mx, my, rx, ry);
        g.drawLine(lx, ly, rx, ry);

    }

    public void keyPressed(int keyCode) {
        super.keyPressed(keyCode);
    }

    public void input1() {

        int keyState = this.getKeyStates();

        if (((keyState >> Canvas.UP) & 0x1) == 1) {
            if (this.cury >= this.penUpY) {
                this.cury--;
                if (this.cury <= this.pos[1][index]) {
                    if (this.index > 0) {
                        this.index--;
                    }
                }
            }
        }
        if (((keyState >> Canvas.DOWN) & 0x1) == 1) {
            if (this.cury <= this.height - heightBar) {
                this.cury++;
                if (this.cury >= this.pos[1][index] + this.scaleHeigth) {
                    if (this.index < this.count - 1) {
                        this.index++;
                    }
                }
            }
        }
    }

    public void keyReleased(int keyCode) {
        super.keyReleased(keyCode);
    }

    public boolean isVisible() {
        return isVisible;
    }

    public void setVisible(boolean isVisible) {
        this.isVisible = isVisible;
    }

    public void run() {
        while (true) {
            input1();
            repaint();
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }// end while(true)
    }// end function

}

⌨️ 快捷键说明

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