目前java的drawstring()方法缺省地使用当前graphics对象的背景颜色作为文字的背
景颜色,并且不提供专门的方法来改变这种设置。因此,我们不能够直接画出具有不
一样背景的文字。一个可行的办法是获得文字信息的高度和宽度,在适当的位置先画
出背景色,然后再同样的位置画出文字。
一段实现了这个方法的程序如下:
color backcolor = color.red, forecolor = color.blue;
int currx = 10, curry = 10;
string info = "test string".
fontmetrics metrics = getfontmetrics(g.getfont());
int height = metrics.getheight();
int width = metrics.stringwidth(info);
g2.setpaint(backcolor);
g2.fillrect(currx, curry - height, width, height);
g2.setpaint(forecolor);
g2.drawstring(currx, curry);
闽公网安备 35060202000074号