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

📄 textpolicy.java

📁 有关j2me的很好的例子可以研究一下
💻 JAVA
字号:
/* * @(#)TextPolicy.java	1.52 01/07/27 * Copyright (c) 1999-2001 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information").  You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */package javax.microedition.lcdui;/** * A special Policy subclass to handle text-only input */class TextPolicy extends Policy {    /** Array holding the linebreaks of the text */    private int lineBreaks[] = new int[4];    /** The number of lines of text */    private int numLines;    /** An index of the current line */    private int curLine = 0;    /**     * Construct a new TextPolicy     *     * @param f The Font to use when rendering this policy     */    TextPolicy(Font f) {        super(f);    }    /**     * Move the cursor in this text policy     *     * @param dir The direction to move     * @param curPos The current cursor position     * @param buf The array of characters in the buffer     * @param numChars The number of characters in the buffer     * @return int The new position of the cursor     */    int moveCursor(int dir, int curPos, char[] buf, int numChars) {	switch (dir) {	case Canvas.UP:	case Canvas.DOWN:            {                int x = cursorX;                 curLine += (dir == Canvas.UP ? -1 : 1);                 int t = numLines - 1;                if (curLine < 0) {                    curLine = 0;                }                if (curLine > t) {                    curLine = t;                }                // curLine = Math.min(numLines - 1, Math.max(0, curLine));                                int fc = curPos = (curLine == 0) ? 0 : lineBreaks[curLine - 1];                while ((x > 0) && (curPos < lineBreaks[curLine])) {                    int w = f.charWidth(buf[curPos++]);                    if ((w - x) > x) {                        // subtracting w from x would move us                        // further than leaving x alone.  We've                        // found the closest position.                        break;                    } else {                        x -= w;                    }                }                if (curPos > fc && curPos == lineBreaks[curLine]                            && curPos < numChars) {                    curPos--;                }                cursorX = f.charsWidth(buf, fc, curPos - fc);                cursorY = curLine*lineHeight;            }            break;        case Canvas.LEFT:            if (curPos > 0) {                if (curLine > 0 && curPos == lineBreaks[curLine-1]) {                    curLine--;                    int fc = curLine == 0 ? 0 : lineBreaks[curLine - 1];                    cursorY -= lineHeight;                    cursorX = f.charsWidth(buf, fc, curPos-1-fc);                 } else {                    cursorX -= f.charWidth(buf[curPos-1]);                }                --curPos;            }            break;        case Canvas.RIGHT:            if (curPos < numChars) {                if ((curLine < numLines-1) &&                       (curPos+1 == lineBreaks[curLine])) {                    curLine++;                    cursorY += lineHeight;                    cursorX = 0;                } else {                    cursorX += f.charWidth(buf[curPos]);                }                ++curPos;            }            break;        }        return curPos;    }    /**     * Paint the contents of this policy     *     * @param g The Graphics object to paint to     * @param buf The character buffer     * @param numChars The number of characters in the buffer     * @param cursorEnabled The enabled state of the cursor     * @param cursorPos The current position of the cursor     * @param noCharShow If True, do not show characters in plaintext     */    void paint(Graphics g, char[] buf, int numChars, boolean cursorEnabled,                int cursorPos, boolean noCharShow) {        int y = g.getClipY();        int yEnd = y + g.getClipHeight();        int from = 0;        int prev = 0;        if (y > 0) {            from = y / lineHeight;            y = from * lineHeight;	    if (from > 0) {		prev = lineBreaks[from-1];	    }        } else {            y = 0;        }        // We assume that background was cleared with erase color        g.setColor(Display.FG_COLOR);        g.setFont(f);        for (int i = from; i < numLines && y <= yEnd; ++i) {                int n = lineBreaks[i] - prev;                 if (buf[lineBreaks[i]-1] == '\n') {                  n--;                }                if (n > 0) {                  g.drawChars(buf, prev, n,                              0, y, Graphics.TOP | Graphics.LEFT);                }            y += lineHeight;            prev = lineBreaks[i];                    }        if (cursorEnabled) {            g.drawLine(cursorX, cursorY, cursorX, cursorY + lineHeight);        }    }    /**     * Add a line break at the given position     *     * @param spot The location of the new linebreak     */    void addBreak(int spot) {        if (numLines == lineBreaks.length) {            int newBreaks[] = new int[lineBreaks.length + 4];            System.arraycopy(lineBreaks, 0, newBreaks, 0, lineBreaks.length);            lineBreaks = newBreaks;        }        lineBreaks[numLines] = spot;             }    /**     * Notify this policy that its contents have changed     *     * @param buf The character buffer     * @param numChars The number of characters in the buffer     * @param startChar The start index of the change     * @param cursorPos The position of the cursor     * @return int The delta of the height changed     */    int contentChanged(char buf[], int numChars,                        int startChar, int cursorPos) {         if (width == -1) {            return 0;        }        int oldHeight = height;        if (startChar <= 0 || numLines <= 0) {            numLines = startChar = 0;        } else {            for (int i = 0; i < numLines; ++i) {                if (lineBreaks[i] > startChar) {                    numLines = i;                    break;                }            }            // always rebuild previous line : for moving word back            if (numLines > 0) {                --numLines;            }            startChar = numLines == 0 ? 0 : lineBreaks[numLines - 1];         }        int length = 0;        int spaceIndex = 0;        int wordWidth = 0;        for (int i = startChar; i < numChars; ++i) {            int charWidth = f.charWidth(buf[i]);            if (buf[i] == '\n') {                addBreak(i+1);                numLines++;                length = wordWidth = 0;                continue;            }             if (buf[i] == ' ') {                spaceIndex = i;                wordWidth = 0;            } else {                wordWidth += charWidth;            }            length += charWidth;            if (length  > width) {               // add a line break, for a very long word !                if (spaceIndex == 0) { // long word                    addBreak(i);                    length = charWidth;                } else { // do word wrap                    addBreak(spaceIndex + 1);                    length = i == spaceIndex ? 0 : wordWidth;		    // if the cursorPos it set before		    // and the cursorPos happens to be more than or exactly                    // at the point where the work break		    // has to be done, move the cursor on next line                    if (cursorPos >= spaceIndex + 1) {                        curLine = numLines + 1;                        cursorX = 0;		    }                }                numLines++;                spaceIndex = 0;            }             if (cursorPos == i) {                curLine = numLines;                cursorX = length == 0 ? 0 : length - charWidth;            }        }        if (cursorPos == numChars) {            curLine = numLines;            cursorX = length;        }        cursorY = lineHeight * curLine;        addBreak(numChars);     // always "end" the last line        if (numChars > 0) {            numLines++;        }        // System.out.println("At the end of rebuild numLines is "+numLines);        height = numLines * lineHeight;        // It should be at least one line high        if (height < lineHeight) {            height = lineHeight;        }        return height - oldHeight;    }}

⌨️ 快捷键说明

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