Posted By:
Rahi_Parsi
Posted On:
Thursday, June 13, 2002 09:43 AM
You have to get the FontMetrics from the particular Graphics object. For example:
public void renderRoutine(String text,Font font,int x,int y,Graphics g)
{ Font pOldFont=g.getFont();
g.setFont(font);
FontMetrics fm=g.getFontMetrics();
int textWidth=fm.stringWidth(text)+1;
int textHeight=fm.getMaxAscent()+fm.getMaxDescent()+fm.getLeading();
int actualDrawYpos=y+fm.getMaxAscent();
//background colour
g.setColor(Color.lightGray);
g.fillRect(x,y,textWidth,textHeight);
g.setColor(Color.black);
g.drawString(text,x,actualDrawYpos);
g.setFont(pOldFont);
}
Try it out and you'll see that it works.