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

📄 mmcanvas.java

📁 mobile to system control
💻 JAVA
字号:
/* *   This file is part of MobiMon. * *   MobiMon is free software; you can redistribute it and/or modify *   it under the terms of the GNU General Public License as published by *   the Free Software Foundation; either version 2 of the License, or *   (at your option) any later version. * *   MobiMon is distributed in the hope that it will be useful, *   but WITHOUT ANY WARRANTY; without even the implied warranty of *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *   GNU General Public License for more details. *   You should have received a copy of the GNU General Public License *   along with MobiMon; if not, write to the Free Software *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *//* * MMCanvas.java * * Created on February 4, 2003, 3:21 PM */package mobimon.midlet;import java.util.*;import javax.microedition.lcdui.*;/** * * @author  jan * @version */public class MMCanvas extends Canvas {        private String str;        private Font font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN,    Font.SIZE_SMALL);        private Vector lines = null;        private int firstLine = 0;        private int maxLength = 0;        private int firstChar = 0;        private int wrapThreshold = 250;        public MMCanvas(String toDisplay) {        str = toDisplay;        chopText();    }        /**     * paint     */    public void paint(Graphics g) {        int height = 0;                g.setColor(0x00ffffff);        g.fillRect(0, 0, getWidth(), getHeight());                g.setColor(0x00000000);        g.setFont(font);        int i = firstLine;        int sl = 0;        while ((height < getHeight()) && (i < lines.size())) {            Section s = (Section)lines.elementAt(i);            sl = s.start + firstChar;            if (sl < s.end)                g.drawString(str.substring(sl, s.end), 0,                height, Graphics.TOP|Graphics.LEFT);            height += font.getHeight();            i++;        }    }        /**     * Called when a key is pressed.     */    protected  void keyPressed(int keyCode) {        scrollText(keyCode);    }        /**     * Called when a key is released.     */    protected  void keyReleased(int keyCode) {    }        /**     * Called when a key is repeated (held down).     */    protected  void keyRepeated(int keyCode) {        scrollText(keyCode);    }        /**     * Called when the pointer is dragged.     */    protected  void pointerDragged(int x, int y) {    }        /**     * Called when the pointer is pressed.     */    protected  void pointerPressed(int x, int y) {    }        /**     * Called when the pointer is released.     */    protected  void pointerReleased(int x, int y) {    }        private void chopText() {        lines = new Vector();        str.trim();        int start = 0;        int end = 0;        if (str.length() > wrapThreshold) {            while ((end = str.indexOf('\n', start)) != -1) {                lines.addElement(new Section(start, end));                if ((end - start) > maxLength) maxLength = end - start;                start = ++end;            }            lines.addElement(new Section(start, str.length()));            if ((str.length() - start) > maxLength)                maxLength = str.length() - start;        } else {            while (start < str.length()) {                while (isWhiteSpace(str.charAt(start))) start++;                end = Math.min((start + 10), str.length());                while                (font.stringWidth(str.substring(start, end)) < getWidth()) {                    if (end == str.length()) break;                    end++;                }                if (end != str.length()) end--;                lines.addElement(new Section(start, end));                start = end;            }            maxLength = 0;        }    }        private void scrollText(int keyCode) {        switch (getGameAction(keyCode)) {            case Canvas.DOWN:                if ((firstLine + 2) < lines.size()) {                    firstLine++;                    repaint();                }                break;            case Canvas.UP:                if (firstLine > 0) {                    firstLine--;                    repaint();                }                break;            case Canvas.LEFT:                if (firstChar > 0) {                    firstChar--;                    repaint();                }                break;            case Canvas.RIGHT:                if (firstChar < maxLength) {                    firstChar++;                    repaint();                }                break;            case Canvas.GAME_A:                firstLine = 0;                firstChar = 0;                repaint();                break;            case Canvas.GAME_B:                firstLine = lines.size() - 2;                firstChar = 0;                repaint();                break;            case Canvas.GAME_C:                if ((firstChar - 10) > 0) {                    firstChar -= 10;                } else {                    firstChar = 0;                }                repaint();                break;            case Canvas.GAME_D:                Section s = (Section)lines.elementAt(firstLine);                if ((firstChar + 11) < s.end) {                    firstChar += 10;                    repaint();                }                break;        }    }        /** Getter for property wrapThreshold.     * @return Value of property wrapThreshold.     */    public int getWrapThreshold() {        return wrapThreshold;    }        /** Setter for property wrapThreshold.     * @param wrapThreshold New value of property wrapThreshold.     */    public void setWrapThreshold(int wrapThreshold) {        this.wrapThreshold = wrapThreshold;    }        private boolean isWhiteSpace(char c) {        return((c == ' ') || (c == '\t') || (c == '\n'));    }        private class Section {        int start;        int end;                public Section(int start, int end) {            this.start = start;            this.end = end;        }            }    }

⌨️ 快捷键说明

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