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());
        }
    
    }
    
  • 相关阅读:
    RHEL7全新初始化进程管理systemd(图形启动和非图形启动切换)
    Linux系统添加硬盘设备(磁盘分区-格式化-挂载-使用)
    linux系统主要常见目录结构
    Linux系统文件访问控制列表
    Linux命令-sudo
    Linux系统文件的隐藏属性
    Linux系统文件权限&目录权限
    Linux系统VIM编辑器
    Linux功能-环境变量
    Linux系统PATH变量配置
  • 原文地址:https://www.cnblogs.com/zhangweia/p/2144084.html
Copyright © 2011-2022 走看看