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

📄 font_attribute_text.java

📁 本光盘是《J2ME无线移动游戏开发》一书的配套光盘
💻 JAVA
字号:
package ch10;

import javax.microedition.lcdui.*;

//该类实现字体的绘制
public class Font_Attribute_Text
    extends Canvas {

  //声明一个代表字体外观的int型属性变量
  int face;

  //声明一个代表字体样式的int型属性变量
  int style;

  //声明一个代表字体尺寸的int型属性变量
  int size;

  //显示文本内容
  String text = "Sample Text";

  /*
   5.构造器
   */
  public Font_Attribute_Text() {
    size = Font.SIZE_MEDIUM;
    face = Font.FACE_SYSTEM;
    style = Font.STYLE_PLAIN;
  }

  //设置字体外观属性值
  public void setFace(int face) {
    this.face = face;
  }

  //获取字体外观属性值
  public int getFace() {
    return face;
  }

  //设置字体样式属性值
  public void setStyle(int style) {
    this.style = style;
  }

  //获取字体样式属性值
  public int getStyle() {
    return style;
  }

  //设置字体尺寸属性值
  public void setSize(int size) {
    this.size = size;
  }

  //获取字体尺寸属性值
  public int getSize() {
    return size;
  }

  /*
   6.绘制字体
   */
  protected void paint(Graphics g) {
    //获取屏幕宽度和高度
    int w = getWidth();
    int h = getHeight();

    //填充背景
    g.setColor(0x000000);
    g.fillRect(0, 0, w, h);

    g.setColor(0xffffff);
    Font font = Font.getFont(face, style, size);
    g.setFont(font);

    //获取字符串宽度和高度
    int SWidth = font.stringWidth(text);
    int SHeight = font.getHeight();

    g.drawString(text, (w - SWidth) / 2, (h - SHeight) / 2,
                 Graphics.LEFT | Graphics.TOP);
  }
}

⌨️ 快捷键说明

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