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

📄 font.java

📁 有关j2me的很好的例子可以研究一下
💻 JAVA
字号:
/* * @(#)Font.java	1.23 01/07/12 * 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;/** * <p> The Font class represents fonts and font metrics. Fonts cannot be * created by applications. Instead, applications query for fonts based on * font attributes and the system will attempt to provide a font that matches * the requested attributes as closely as possible. </p> * * <p> A Font's attributes are style, size, and face. Values for * attributes must be specified in terms of symbolic constants. Values for * the style attribute may be combined using the logical OR operator, * whereas values for the other attributes may not be combined. For example, * the value </p> * * <p> <code> * STYLE_BOLD | STYLE_ITALIC * </code> </p> * * <p> may be used to specify a bold-italic font; however </p> * * <p> <code> * SIZE_LARGE | SIZE_SMALL * </code> </p> * * <p> is illegal. </p> * * <p> The values of these constants are arranged so that zero is valid for * each attribute and can be used to specify a reasonable default font * for the system. For clarity of programming, the following symbolic * constants are provided and are defined to have values of zero: </p> * * <p> <ul> * <li> <code> STYLE_PLAIN </code> </li> * <li> <code> SIZE_MEDIUM </code> </li> * <li> <code> FACE_SYSTEM </code> </li> * </ul> </p> * * <p> Values for other attributes are arranged to have disjoint bit patterns * in order to raise errors if they are inadvertently misused (for example, * using FACE_PROPORTIONAL where a style is required). However, the values * for the different attributes are not intended to be combined with each * other. </p> */public final class Font {    /**     * <P>The plain style constant. This may be combined with the     * other style constants for mixed styles. </p>     *     * <P>Value 0 is assigned to STYLE_PLAIN.</P>     */    public static final int STYLE_PLAIN       = 0;    /**     * <P>The bold style constant. This may be combined with the     * other style constants for mixed styles.</P>     *     * <P>Value 1 is assigned to STYLE_BOLD.</P>     */    public static final int STYLE_BOLD        = 1;    /**     * <P>The italicized style constant. This may be combined with     * the other style constants for mixed styles.</P>     *     * <P>Value 2 is assigned to STYLE_ITALIC.</P>     */    public static final int STYLE_ITALIC      = 2;    /**     * <P>The underlined style constant. This may be combined with     * the other style constants for mixed styles.</P>     *     * <P>Value 4 is assigned to STYLE_UNDERLINED.</P>     */    public static final int STYLE_UNDERLINED  = 4;    /**     * <P>The "small" system-dependent font size.</P>     *     * <P>Value 8 is assigned to STYLE_SMALL.</P>     */    public static final int SIZE_SMALL        = 8;    /**     * The "medium" system-dependent font size.     *     * <P>Value 0 is assigned to STYLE_MEDIUM.</P>     */    public static final int SIZE_MEDIUM       = 0;    /**     * <P>The "large" system-dependent font size.</P>     *     * <P>Value 16 is assigned to SIZE_LARGE.</P>     */    public static final int SIZE_LARGE        = 16;    /**     * <P>The "system" font face.</P>     *     * <P>Value 0 is assigned to FACE_SYSTEM.</P>     */    public static final int FACE_SYSTEM       = 0;    /**     * <P>The "monospace" font face.</P>     *     * <P>Value 32 is assigned to FACE_MONOSPACE.</P>     */    public static final int FACE_MONOSPACE    = 32;    /**     * <P>The "proportional" font face.</P>     *     * <P>Value 64 is assigned to FACE_PROPORTIONAL.</P>     */    public static final int FACE_PROPORTIONAL = 64;    /**     * Construct a new Font object     *     * @param face The face to use to construct the Font     * @param style The style to use to construct the Font     * @param size The point size to use to construct the Font     */    private Font(int face, int style, int size) {        this.face  = face;        this.style = style;        this.size  = size;        init(face, style, size);    }    /**     * Gets the default font of the system.     *     * @return Font The "default" font to use for the system     */    public static Font getDefaultFont() {        // SYNC NOTE: return of atomic value, no locking necessary        return DEFAULT_FONT;    }    /**     * <p> Obtains an object representing a font having the specified     * face, style,     * and size. If a matching font does not exist, the system will     * attempt to provide the closest match. This method <em>always</em> returns     * a valid font object, even if it is not a close match to the request. </p>     *     * @param face one of FACE_SYSTEM, FACE_MONOSPACE, or FACE_PROPORTIONAL     * @param style STYLE_PLAIN, or a combination of STYLE_BOLD,     * STYLE_ITALIC, and STYLE_UNDERLINED     * @param size one of SIZE_SMALL, SIZE_MEDIUM, or SIZE_LARGE     * @return instance the nearest font found     * @throws IllegalArgumentException if face, style, or size are not     * legal values     */    public static Font getFont(int face, int style, int size) {        if ((face != FACE_SYSTEM)             && (face != FACE_MONOSPACE)            && (face != FACE_PROPORTIONAL)) {            throw new IllegalArgumentException("Unsupported face");        }        if ((style & ((STYLE_UNDERLINED << 1) - 1)) != style) {            throw new IllegalArgumentException("Illegal style");        }        if ((size != SIZE_SMALL)             && (size != SIZE_MEDIUM)            && (size != SIZE_LARGE)) {            throw new IllegalArgumentException("Unsupported size");        }        synchronized (Display.LCDUILock) {            /* RFC: this makes garbage.  But hashtables need Object keys. */            Integer key = new Integer(face | style | size);            Font f = (Font)table.get(key);            if (f == null) {                f = new Font(face, style, size);                table.put(key, f);            }            return f;        }    }    /**     * Gets the style of the font. The value is an OR'ed combination of     * STYLE_BOLD, STYLE_ITALIC, and STYLE_UNDERLINED; or the value is     * zero (STYLE_PLAIN).     * @return style of the current font     *     * @see #isPlain()     * @see #isBold()     * @see #isItalic()     */    public int getStyle() {        // SYNC NOTE: return of atomic value, no locking necessary        return style;    };    /**     * Gets the size of the font.     *     * @return one of SIZE_SMALL, SIZE_MEDIUM, SIZE_LARGE     */    public int getSize() {        // SYNC NOTE: return of atomic value, no locking necessary        return size;    }    /**     * Gets the face of the font.     *     * @return one of FACE_SYSTEM, FACE_PROPORTIONAL, FACE_MONOSPACE     */    public int getFace() {         // SYNC NOTE: return of atomic value, no locking necessary        return face;    }    /**     * Returns true if the font is plain.     * @see #getStyle()     * @return true if font is plain     */    public boolean isPlain() {        // SYNC NOTE: return of atomic value, no locking necessary        return style == STYLE_PLAIN;    }    /**     * Returns true if the font is bold.     * @see #getStyle()     * @return true if font is bold     */    public boolean isBold() {        // SYNC NOTE: return of atomic value, no locking necessary        return (style & STYLE_BOLD) == STYLE_BOLD;    }      /**     * Returns true if the font is italic.     * @see #getStyle()     * @return true if font is italic     */    public boolean isItalic() {        // SYNC NOTE: return of atomic value, no locking necessary        return (style & STYLE_ITALIC) == STYLE_ITALIC;    }    /**     * Returns true if the font is underlined.     * @see #getStyle()     * @return true if font is underlined     */    public boolean isUnderlined() {        // SYNC NOTE: return of atomic value, no locking necessary        return (style & STYLE_UNDERLINED) == STYLE_UNDERLINED;    }            /**     * Gets the standard height of a line of text in this font. This value     * includes sufficient spacing to ensure that lines of text painted this     * distance from anchor point to anchor point are spaced as intended by the     * font designer and the device. This extra space (leading) occurs below the     * text.     * @return standard height of a line of text in this font (a non-negative     * value)     */    public int getHeight() {        // SYNC NOTE: return of atomic value, no locking necessary        return height;    }    /**     * Gets the distance in pixels from the top of the text to the text's     * baseline.     * @return the distance in pixels from the top of the text to the text's     * baseline     */    public int getBaselinePosition() {        // SYNC NOTE: return of atomic value, no locking necessary        return baseline;    }    /**     * Gets the advance width of the specified character in this Font.     * The advance width is the amount by which the current point is     * moved from one character to the next in a line of text, and thus includes     * proper inter-character spacing. This spacing occurs to the right of the     * character.     * @param ch the character to be measured     * @return the total advance width (a non-negative value)     */    public native int charWidth(char ch);    /**     * Returns the advance width of the characters in ch, starting at     * the specified offset and for the specified number of characters     * (length). The advance width is the amount by which the current     * point is moved from one character to the next in a line of text. <p>     *     * The offset and length parameters must specify a valid range of characters     * within the character array ch. The offset parameter must be within the     * range [0..(ch.length)]. The length parameter must be a non-negative     * integer such that (offset + length) <= ch.length.     *     * @param ch The array of characters     * @param offset The index of the first character to measure     * @param length The number of characters to measure     * @return the width of the character range     * @throws ArrayIndexOutOfBoundsException if offset and length specify an     * invalid range     * @throws NullPointerException if ch is null     */    public native int charsWidth(char[] ch, int offset, int length);    /**     * Gets the total advance width for showing the specified String     * in this Font.     * The advance width is the amount by which the current point is     * moved from one character to the next in a line of text.     * @param str the String to be measured.     * @return the total advance width     * @throws NullPointerException if str is null     */    public native int stringWidth(java.lang.String str);    /**     * Gets the total advance width for showing the specified substring in this     * Font. The advance width is the amount by which the current point is moved     * from one character to the next in a line of text. <p>     *     * The offset and length parameters must specify a valid range of characters     * within str. The offset parameter must be within the     * range [0..(str.length())]. The length parameter must be a non-negative     * integer such that (offset + length) <= str.length().     *     * @param str the String to be measured.     * @param offset zero-based index of first character in the substring     * @param len length of the substring.     * @return the total advance width     * @throws StringIndexOutOfBoundsException if offset and length specify an     * invalid range     * @throws NullPointerException if str is null     */    public native int substringWidth(String str, int offset, int len);    // private implementation //    /** The face of this Font */    private int face;    /** The style of this Font */    private int style;    /** The point size of this Font */    private int size;    /** The baseline of this Font */    private int baseline;    /** The height of this Font */    private int height;    /**     * The "default" font, constructed from the 'system' face,     * plain style, and 'medium' size point.     */    private static final Font DEFAULT_FONT = new Font(FACE_SYSTEM,                                                      STYLE_PLAIN,                                                      SIZE_MEDIUM);    /**     * A hashtable used to maintain a store of created Fonts     * so they are not re-created in the future     */    private static java.util.Hashtable table = new java.util.Hashtable(4);    /**     * Natively initialize this Font object's peer     *     * @param face The face to initialize the native Font     * @param style The style to initialize the native Font     * @param size The point size to initialize the native Font     */    private native void init(int face, int style, int size);}

⌨️ 快捷键说明

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