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

📄 bqfont.java

📁 J2me唆哈的代码
💻 JAVA
字号:
package a.a.a.midp.lcdui;

import java.awt.FontMetrics;
import java.awt.Toolkit;

import javax.microedition.lcdui.*;

import a.a.a.b.*;

public class BQFont {

	java.awt.Font instance;
	
	Font shell;
	/**
     * �����һЩ��Ϣ
     */
    private FontMetrics fm;

    /**
     *  ��ǰ������
     */
    private int face;

    /**
     *  ��ǰ����ĸ�ʽ
     */
    private int style;

    /**
     * ��ǰ����Ĵ�С 
     */
    private int size;

    /**
     * ģ��ϵͳ���������
     */
    public static final String face_system="DialogInput";
    /**
     * �������������
     */
    private static final String face_proportional="SansSerif";
    /**
     * ģ��ȿ����������
     */
    private static final String face_monospace="Monospaced";
    /**
     * ģ����ʵ���õ��������С
     */
    private static int _SIZE_SMALL;
    private static int _SIZE_MEDIUM;
    private static int _SIZE_LARGE;

    private static int _HEIGHT_SMALL;
    private static int _HEIGHT_MEDIUM;
    private static int _HEIGHT_LARGE;
    /**
     * Ϊ�˼��ٴ���������ٶ�,����ǰ����������屣�浽һ���ϣ����
     */
    private static java.util.Hashtable table = new java.util.Hashtable(4);
    
    /**
     * ͨ��һ��java.awt.Font�Ķ���,����Ӹ��,4ģ��j2me��Font��
     * 
     * @param name �������
     * @param style �����ʽ
     * @param size �����С
     */
    public BQFont(int face, int style, int size) {
        
        instance = new java.awt.Font(getAwtName(face), style, getAwtSize(size));
        init(face, style, size);
        //fm = EmulatorFrame.instance.getFontMetrics(instance);
        fm=Toolkit.getDefaultToolkit().getFontMetrics(instance);
    }
        
    private int getAwtSize(int size){
        switch(size){
        case Font.SIZE_LARGE:
            return _SIZE_LARGE;
        case Font.SIZE_MEDIUM:
            return _SIZE_MEDIUM;
        case Font.SIZE_SMALL:
            return _SIZE_SMALL;
        default:
        	throw new IllegalArgumentException("Illegal Font size");
        }
    }
    
    private String getAwtName(int face){
        if(face == Font.FACE_SYSTEM){
            return face_system;
        }else if(face==Font.FACE_MONOSPACE){
            return face_monospace;
        }else if(face==Font.FACE_PROPORTIONAL){
            return face_proportional;
        }
        return null;
    }

    private int getFontHeight(int size){
    	switch(size){
        case Font.SIZE_LARGE:
            return _HEIGHT_LARGE;
        case Font.SIZE_MEDIUM:
            return _HEIGHT_MEDIUM;
        case Font.SIZE_SMALL:
            return _HEIGHT_SMALL;
        default:
        	throw new IllegalArgumentException("Illegal Font size");
        }
    }
    /**
     * �����������Ϣ
     * @param face ����
     * @param style ��ʽ
     * @param size ��С
     */
    private void init(int face, int style, int size) {
        this.face = face;
        this.style = style;
        this.size = size;
    }
    
    public static void initFontSize(int ssize,int msize,int lsize,
    		int sheight,int mheight,int lheight){
    	_SIZE_SMALL=ssize;
    	_SIZE_MEDIUM=msize;
    	_SIZE_LARGE=lsize;
    	
    	_HEIGHT_SMALL=sheight;
    	_HEIGHT_MEDIUM=mheight;
    	_HEIGHT_LARGE=lheight;
    }
    
    public static BQFont getDefaultFont() {
        return  Font.getDefaultFont().getBQFont();
    }

    public static BQFont getFont(int face, int style, int size) {
        if ((face != Font.FACE_SYSTEM) && (face != Font.FACE_MONOSPACE)
                && (face != Font.FACE_PROPORTIONAL)) {
            throw new IllegalArgumentException("Unsupported face");
        }

        if ((style & ((Font.STYLE_UNDERLINED << 1) - 1)) != style) {
            throw new IllegalArgumentException("Illegal style");
        }

        if ((size != Font.SIZE_SMALL) && (size != Font.SIZE_MEDIUM)
                && (size != Font.SIZE_LARGE)) {
            throw new IllegalArgumentException("Unsupported size");
        }

        //synchronized (Display.LCDUILock) {
            Integer key = new Integer(face | style | size);
            BQFont f = (BQFont) table.get(key);
            if (f == null) {
                f = new BQFont(face, style, size);
                table.put(key, f);
            }
            return f;
        //}
    }
    
    /**
     * ���ص�ǰ����ĸ�ʽ
     * @return ��ǰ����ĸ�ʽ
     * @since MIDP1.0
     */
    public int getStyle() {
        return style;
    };
    /**
     * ���ص�ǰ����Ĵ�С
     * @return ��ǰ����Ĵ�С
     * @since MIDP1.0
     */
    public int getSize() {
        return size;
    }
    /**
     * ���ص�ǰ����
     * @return ��ǰ����
     * @since MIDP1.0
     */
    public int getFace() {
        return face;
    }
    /**
     * �жϵ�ǰ�����Ƿ�����ͨ����
     * @return �������ͨ����,����true,����false
     * @since MIDP1.0
     */
    public boolean isPlain() {
        return style == Font.STYLE_PLAIN;
    }
    /**
     * �жϵ�ǰ�����Ƿ����Ǵ���
     * @return ����Ǵ���,����true,����false
     * @since MIDP1.0
     */
    public boolean isBold() {
        
        return (style & Font.STYLE_BOLD) == Font.STYLE_BOLD;
    }
    /**
     * �жϵ�ǰ�����Ƿ���б��
     * @return ������,����true,����false
     * @since MIDP1.0
     */
    public boolean isItalic() {
        return (style & Font.STYLE_ITALIC) == Font.STYLE_ITALIC;
    }
    /**
     * �жϵ�ǰ�����Ƿ��Ǵ��»���
     * @return ����Ǵ��»���,����true,����false
     * @since MIDP1.0
     */
    public boolean isUnderlined() {
        return (style & Font.STYLE_UNDERLINED) == Font.STYLE_UNDERLINED;
    }
    /**
     * �õ�����ĸ߶� Ŀǰģ�ⲻ�ɹ�,ƫ��Ƚϴ�
     * @return ����ĸ߶�
     * @since MIDP1.0
     */
    public int getHeight() {
    	int h=fm.getHeight();
        return getFontHeight(size);//fm.getHeight();
    }
    /**
     * �õ�����Ļ��߾��� Ŀǰģ�ⲻ�ɹ�,ƫ��Ƚϴ�
     * @return ���߾���
     * @since MIDP1.0
     */
    public int getBaselinePosition() {
        return fm.getAscent()-(fm.getHeight()-getFontHeight(size))/2;
    }

    /**
     * �õ��ڵ�ǰ������,ch�ַ�Ŀ��
     * @param ch �ַ�
     * @return ch�ڵ�ǰ�����µĿ��
     * @since MIDP1.0
     */
    public int charWidth(char ch) {
        return fm.charWidth(ch);
    }

    /**
     * �õ��ڵ�ǰ������,ch������offset��ʼ,length���ȵ��ַ�Ŀ��
     * 
     * @param ch �ַ�����
     * @param offset ��ʼλ��
     * @param length �ַ���
     * @return ch������offset��ʼ,length�����ַ��ڵ�ǰ�����µĿ��
     * @since MIDP1.0
     */
    public int charsWidth(char[] ch, int offset, int length) {
        if (offset < 0 || offset > ch.length || offset + length > ch.length
                || length < 0)
            throw new StringIndexOutOfBoundsException();
        return fm.charsWidth(ch, offset, length);
    }

    /**
     * �õ���ǰ������,str�Ŀ��
     * 
     * @param str �ַ�
     * @return str�ڵ�ǰ�����µĿ��
     * @since MIDP1.0
     */
    public int stringWidth(java.lang.String str) {
        return fm.stringWidth(str);
    }

    /**
     * �õ���ǰ������,str�д�offset��ʼ,length���ȵ�һ��string�ij���
     * 
     * @param str �ַ�
     * @param offset ��ʼλ��
     * @param length ����
     * @return str�д�offset��ʼ,length�����Ӵ��ڵ�ǰ�����µĿ��
     * @since MIDP1.0
     */
    public int substringWidth(String str, int offset, int length) {
        if (offset < 0 || offset > str.length()
                || offset + length > str.length() || length < 0)
            throw new StringIndexOutOfBoundsException();

        return fm.stringWidth(str.substring(offset, offset + length));
    }

    java.awt.Font getTrueFont(){
    	return instance;
    }
    
    public void setShell(Font f){
    	shell=f;
    }
    
    public Font getShell(){
    	return shell;
    }
}

⌨️ 快捷键说明

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