📄 font_layout_text.java
字号:
package ch10;
import javax.microedition.lcdui.*;
//该类实现字体布局的绘制
public class Font_Layout_Text
extends Canvas {
//声明一个代表基点水平取值的int型属性变量
int H;
//声明一个代表基点垂直取值的int型属性变量
int V;
//显示文本内容
String text = "Example Text";
/*
5.构造器
*/
public Font_Layout_Text() {
H = Graphics.LEFT;
V = Graphics.TOP;
}
//设置基点水平取值属性值
public void setH(int H) {
this.H = H;
}
//获取基点水平取值属性值
public int getH() {
return H;
}
//设置基点垂直取值属性值
public void setV(int V) {
this.V = V;
}
//获取基点垂直取值属性值
public int getV() {
return V;
}
/*
6.绘制字体布局
*/
protected void paint(Graphics g) {
//获取屏幕宽度和高度
int w = getWidth();
int h = getHeight();
//填充背景
g.setColor(0xffffff);
g.fillRect(0, 0, w, h);
//创建字体
g.setColor(0x000000);
Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_LARGE);
g.setFont(font);
//获取字符串宽度和高度
int SWidth = font.stringWidth(text);
int SHeight = font.getHeight();
//绘制左上角基点文本布局
g.setStrokeStyle(Graphics.DOTTED);
g.drawRect( (w - SWidth) / 2, (h - SHeight) / 2, SWidth, SHeight);
g.setColor(0x0000ff);
g.drawString(text, (w - SWidth) / 2, (h - SHeight) / 2, H | V);
g.setColor(0x000000);
g.setStrokeStyle(Graphics.SOLID);
//联合基点水平和垂直取值
int f = H | V;
//绘制各基点文本布局
switch (f) {
case 20:
g.drawString("基点-左上", 0, 0, Graphics.LEFT | Graphics.TOP);
break;
case 68:
g.drawString("基点-左基线", 0, 0, Graphics.LEFT | Graphics.TOP);
g.setColor(0x00ff00);
g.drawRect( (w - SWidth) / 2,
(h - SHeight) / 2 - font.getBaselinePosition(), SWidth,
SHeight);
break;
case 36:
g.drawString("基点-左下", 0, 0, Graphics.LEFT | Graphics.TOP);
g.setColor(0x00ff00);
g.drawRect( (w - SWidth) / 2, (h - SHeight) / 2 - SHeight, SWidth,
SHeight);
break;
case 17:
g.drawString("基点-中上", 0, 0, Graphics.LEFT | Graphics.TOP);
g.setColor(0x00ff00);
g.drawRect( (w - SWidth) / 2 - SWidth / 2, (h - SHeight) / 2, SWidth,
SHeight);
break;
case 65:
g.drawString("基点-中基线", 0, 0, Graphics.LEFT | Graphics.TOP);
g.setColor(0x00ff00);
g.drawRect( (w - SWidth) / 2 - SWidth / 2,
(h - SHeight) / 2 - font.getBaselinePosition(), SWidth,
SHeight);
break;
case 33:
g.drawString("基点-中下", 0, 0, Graphics.LEFT | Graphics.TOP);
g.setColor(0x00ff00);
g.drawRect( (w - SWidth) / 2 - SWidth / 2, (h - SHeight) / 2 - SHeight,
SWidth, SHeight);
break;
case 24:
g.drawString("基点-右上", 0, 0, Graphics.LEFT | Graphics.TOP);
g.setColor(0x00ff00);
g.drawRect( (w - SWidth) / 2 - SWidth, (h - SHeight) / 2, SWidth,
SHeight);
break;
case 72:
g.drawString("基点-右基线", 0, 0, Graphics.LEFT | Graphics.TOP);
g.setColor(0x00ff00);
g.drawRect( (w - SWidth) / 2 - SWidth,
(h - SHeight) / 2 - font.getBaselinePosition(), SWidth,
SHeight);
break;
case 40:
g.drawString("基点-右下", 0, 0, Graphics.LEFT | Graphics.TOP);
g.setColor(0x00ff00);
g.drawRect( (w - SWidth) / 2 - SWidth, (h - SHeight) / 2 - SHeight,
SWidth, SHeight);
break;
}
//绘制基点坐标点
g.setColor(0xff0000);
g.fillArc( (w - SWidth) / 2 - 2, (h - SHeight) / 2 - 2, 4, 4, 0, 360);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -