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

📄 passtextpolicy.java

📁 有关j2me的很好的例子可以研究一下
💻 JAVA
字号:
/* * @(#)PassTextPolicy.java	1.37 01/08/21 * 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 to handle password text */class PassTextPolicy extends Policy {    /** The echo character buffer */    private char[] echoCharLine;    /** The number of echo chararcters allowed per line */    private int numEchoCharsPerLine;    /** The number of lines */    private int numLines;    /** The character used to echo to the display '*' by default */    private final static char ECHO_CHAR = '*';    /** The echo character pixel width, and extra space width */    private int echoCharWidth, extraSpace;    /**     * Construct a new PassTextPolicy     *     * @param f The Font to use in this policy     */    PassTextPolicy(Font f) {        this(f, 'W');    }    /**     * Construct a new PassTextPolicy     *     * @param f The Font to use in this policy     * @param extraSpaceChar The character to use for extra space     */    PassTextPolicy(Font f, char extraSpaceChar) {        super(f);        echoCharWidth = f.charWidth(ECHO_CHAR);        extraSpace = f.charWidth(extraSpaceChar) - echoCharWidth;        if (extraSpace < 0) {            extraSpace = 0;        }    }    /**     * Set the width of this policy     *     * @param width     * @param buf     * @param numChars     * @param curPos     * @return int     */    int setWidth(int width, char[] buf, int numChars, int curPos) {        numEchoCharsPerLine = (width - extraSpace) / echoCharWidth;        echoCharLine = new char[numEchoCharsPerLine];        for (int i = 0; i < numEchoCharsPerLine; i++) {            echoCharLine[i] = ECHO_CHAR;        }        return super.setWidth(width, buf, numChars, curPos);    }    /**     * Signal this policy that its content has changed     *     * @param buf     * @param numChars     * @param fromPos     * @param curPos     * @return int     */    int contentChanged(char[] buf, int numChars, int fromPos, int curPos) {        if (width == -1) {            return 0;        }        int oldHeight = height;        numLines = (numChars+numEchoCharsPerLine-1)/numEchoCharsPerLine;        height = numLines * lineHeight;        if (height < lineHeight) {            height = lineHeight;        }                cursorX = curPos%numEchoCharsPerLine * echoCharWidth;        cursorY = curPos/numEchoCharsPerLine * lineHeight;                if (curPos == numChars && cursorX == 0 && cursorY > 0) {            cursorX = numEchoCharsPerLine * echoCharWidth;            cursorY -= lineHeight;        }        return (height - oldHeight);    }    /**     * Paint this policy     *     * @param g The Graphics object to paint to     * @param buf The data buffer     * @param numChars The number of characters in the buffer     * @param cursorEnabled A boolean flag indicating the enabled state     *                      of the cursor     * @param cursorPos The location of the cursor     * @param noCharShow A boolean flag to not display the characters on     *                   the screen     */    void paint(Graphics g, char[] buf, int numChars, boolean cursorEnabled,                int cursorPos, boolean noCharShow) {        // We assume that background was cleared with erase color        g.setColor(Display.FG_COLOR);        g.setFont(f);        if (numChars > 0) {            int echoCharSecWidth = 0;                    int y = g.getClipY();            int yEnd = y + g.getClipHeight();            int from = 0;            if (y > 0) {                 from = y / lineHeight;            }            y = from * lineHeight;            for (int i = from; i < numLines-1 && y < yEnd;                 ++i, y += lineHeight) {                g.drawChars(echoCharLine, 0, numEchoCharsPerLine,                            0, y, Graphics.TOP | Graphics.LEFT);            }            int n = numChars % numEchoCharsPerLine;            if (n == 0) {                n = numEchoCharsPerLine;            }            if (cursorEnabled || noCharShow) {                g.drawChars(echoCharLine, 0, n, 0, y,                             Graphics.TOP | Graphics.LEFT);            } else { // draw the one visible char                if (n > 1) {                    g.drawChars(echoCharLine, 0, n-1, 0, y,                                 Graphics.TOP | Graphics.LEFT);                }				if (cursorX > echoCharWidth) {		    g.drawChar(buf[numChars-1], cursorX-echoCharWidth, y,			       Graphics.TOP | Graphics.LEFT);		} else {		    g.drawChar(buf[numChars-1], 0, y,			       Graphics.TOP | Graphics.LEFT);		}            }        }        if (cursorEnabled) {            g.drawLine(cursorX, cursorY, cursorX, cursorY + lineHeight);        }    }    /**     * Get the maximum width for this policy     *     * @param allowedWidth The default allowed width     * @param maxSize ??     * @return int The allowed width of this policy     */    int getMaxWidth(int allowedWidth, int maxSize) {        // last character could be painted as other than         // an echo character during the input        int w = echoCharWidth*maxSize + extraSpace;        if (w < allowedWidth) {            return w;        }        return allowedWidth;    }}

⌨️ 快捷键说明

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