dcsimg
The number of pixels (width or end coordinates) a g.drawstring call will use
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Ari_Steinberg
Posted On:   Friday, May 24, 2002 08:46 PM

I want to know how many pixels the g.drawstring command uses either before or after I call it. Is there any way to determine the end coordinates of the command. I want to make sure that the next call does't over-write the previous call?

Re: The number of pixels (width or end coordinates) a g.drawstring call will use

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.
About | Sitemap | Contact