public class fontcanvas extends canvas {
private font msystemfont, mmonospacefont, mproportionalfont;
public fontcanvas() { this(font.style_plain); }
public fontcanvas(int style) { setstyle(style); }
public void setstyle(int style) {
msystemfont = font.getfont(font.face_system,
style, font.size_medium);
mmonospacefont = font.getfont(font.face_monospace,
style, font.size_medium);
mproportionalfont = font.getfont(font.face_proportional,
style, font.size_medium);
}
public boolean isbold() {
return msystemfont.isbold();
}
public boolean isitalic() {
return msystemfont.isitalic();
}
public boolean isunderline() {
return msystemfont.isunderlined();
}
public void paint(graphics g) {
int w = getwidth();
int h = getheight();
// clear the canvas.
g.setgrayscale(255);
g.fillrect(0, 0, w - 1, h - 1);
g.setgrayscale(0);
g.drawrect(0, 0, w - 1, h - 1);
int x = w / 2;
int y = 20;
y += showfont(g, "system", x, y, msystemfont);
y += showfont(g, "monospace", x, y, mmonospacefont);
y += showfont(g, "proportional", x, y, mproportionalfont);
}
private int showfont(graphics g, string s, int x, int y, font f) {
g.setfont(f);
g.drawstring(s, x, y, graphics.top | graphics.hcenter);
return f.getheight();
}
}
闽公网安备 35060202000074号