J2ME游戏开发之--字符串的绘制

package com.sliw.graphics;



import javax.microedition.lcdui.Canvas;

import javax.microedition.lcdui.Font;

import javax.microedition.lcdui.Graphics;





/**

 * 

 * @author 章伟

 *

 *

 

   1) face 为字体的外观,J2ME中提供了如下几种face:

       Font.FACE_SYSTEN

       Font.FACE_MONOSPACE

       Font.FACE_PROPORTIONAL

    

    2) style 为字体的风格,J2ME中提供了如下几种style:

       Font.STYLE_PLAIN

       Font.STYLE_BOLD

       Font.STYLE_ITALIC

       Font.STYLE_UNDERLINED

    其中,后三种可以混合使用,例:粗体加斜体的写法

    Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD|Font.STYLE_ITALIC, Font.SIZE_SMALL);

    

    3) size 为字体的大小,J2ME中提供了如下几种size:

       Font.SIZE_SMALL

       Font.SIZE_MEDIUM

       Font.SIZE_LARGE

 */

public class StringCanvas extends Canvas{



    private int topY = 20;

    private int left = 10;

    private String s = "hello zhangweia!";

    

    protected void paint(Graphics g) {

        // TODO Auto-generated method stub

        clear(g);

        

        drawString(g);

    }

    

    

    public void drawString(Graphics g){

        

        Font font ;

        // getFont(int face, int style, int size)

        font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);

        

        g.setFont(font);

        g.setColor(255,0,0);

        g.drawString(s, left, topY, Graphics.LEFT | Graphics.TOP);

        

        System.out.println("字符高度=" + font.getHeight() + "字符宽度=" + font.stringWidth(s));

        

        topY = topY + font.getHeight() + 20;

        g.drawSubstring(s, 0, 5, left, topY, Graphics.LEFT | Graphics.TOP);

        

        char c = 'a';

        topY = topY + 20;

        g.drawChar(c, left, topY, Graphics.LEFT | Graphics.TOP);

    }

    

    public void drawChar(){

        

    }

    

    private void clear(Graphics g){

        g.setColor(255,255,255);

        g.fillRect(0, 0, getWidth(), getHeight());

    }



}

你可能感兴趣的:(j2me)