zoukankan      html  css  js  c++  java
  • 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());
        }
    
    }
    
  • 相关阅读:
    验证数字范围的小插件
    解决EJB懒加载问题
    JS获取按键的代码,Js如何屏蔽用户的按键,Js获取用户按键对应的ASII码(兼容所有浏览器)
    struts2标签之<s:select>
    c#(winform)中自定义ListItem类方便ComboBox和ListBox添加项完全解决
    辞职前须慎重考虑
    怎样把PDF文件在WinForm窗口中显示出来
    加载报表失败
    经典正则表达式 Javascript
    无法生成项目输出组“内容文件来自...
  • 原文地址:https://www.cnblogs.com/zhangweia/p/2144084.html
Copyright © 2011-2022 走看看