カスタムの等幅フォントを使用できます。ペイントする可能性のあるすべての文字を含むPNGファイルを作成し、http: //smallandadaptive.blogspot.com.br/2008/12/custom-monospaced-font.htmlから以下のコードを使用します。
パブリッククラスMonospacedFont{
プライベート画像画像;
プライベート文字firstChar;
private int numChars;
private int charWidth;
public MonospacedFont(画像画像、char firstChar、int numChars){
if(image == null){
新しいIllegalArgumentException( "image == null");をスローします。
}
//最初に表示されるUnicode文字は'!'です (値33)
if(firstChar <= 33){
新しいIllegalArgumentException( "firstChar <= 33");をスローします。
}
//画像には少なくとも1文字が必要です
if(numChars <= 0){
新しいIllegalArgumentException( "numChars <= 0");をスローします。
}
this.image = image;
this.firstChar = firstChar;
this.numChars = numChars;
this.charWidth = image.getWidth()/ this.numChars;
}
public void drawString(Graphics g、String text、int x、int y){
//後で復元するために現在のグラフィッククリップ領域を保存します
int clipX = g.getClipX();
int clipY = g.getClipY();
int clipWidth = g.getClipWidth();
int clipHeight = g.getClipHeight();
char [] chars = text.toCharArray();
for(int i = 0; i <chars.length; i ++){
int charIndex = chars [i]-this.firstChar;
//現在の文字が画像に存在します
if(charIndex> = 0 && charIndex <= this.numChars){
g.setClip(x、y、this.charWidth、this.image.getHeight());
g.drawImage(image、x-(charIndex * this.charWidth)、y、Graphics.TOP | Graphics.LEFT);
x + = this.charWidth;
}
}
//最初のクリップ領域を復元します
g.setClip(clipX、clipY、clipWidth、clipHeight);
}
}
このクラスを使用するサンプルコードを次に示します。
画像画像;
試す {
img = Image.createImage( "/ monospaced_3_5.PNG");
MonospacedFont mf = new MonospacedFont(img、 '0'、10);
mf.drawString(g、 "9876543210"、40、40);
} catch(IOException e){
e.printStackTrace();
}