This site requires JavaScript, please enable it in your browser!
Greenfoot back
Pointifix
Pointifix wrote ...

2014/2/26

getHeight() of a text

Pointifix Pointifix

2014/2/26

#
Hello guys again, i just wanted to know, why the method getHeight() return the height of an text * 2? So if the text is 30 px it return 60 px and so on?
danpost danpost

2014/2/26

#
Please show an example of how your are using it.
Pointifix Pointifix

2014/2/26

#
yes sure sry i forgot:
g = getImage().getAwtImage().createGraphics();
        fm = g.getFontMetrics(new Font("MONOSPACED",Font.BOLD,height));
getImage().drawString(""+fm.getHeight(),400,400);
davmac davmac

2014/2/26

#
See the documentation for the 'getHeight' method. It: Gets the standard height of a line of text in this font. This is the distance between the baseline of adjacent lines of text. It doesn't get the height in pixels of the font as drawn. It returns the amount of space normally needed for a complete line of text rendered in the font, including space above and below that might be needed for ascenders/descenders. It also doesn't return the height of the text * 2.
davmac davmac

2014/2/26

#
By the way, in this line:
fm = g.getFontMetrics(new Font("MONOSPACED",Font.BOLD,height)); 
The 'height' is the height in points, not in pixels. They are different.
Pointifix Pointifix

2014/2/26

#
mhhmm but how the get the perfect height of as example the letter H from Monospaced? so i can center the text vertical as well
davmac davmac

2014/2/26

#
getHeight() should actually be fine for that purpose. Remember to add the ascent (getAscent()) to the position where you want the top of the text to be drawn.
Pointifix Pointifix

2014/2/26

#
okey, i just found that the size of points depends on the resolution of my image? so how can i get a text thats exactly as example 50 px high? Is there any implementation?
davmac davmac

2014/2/26

#
No, as far as I know the only way to do it is to loop through different point sizes until you find something that's close enough. Or otherwise, draw the text at a larger size and scale it down, although that might not look too great for smaller sizes.
Pointifix Pointifix

2014/2/26

#
mhmm seams to be the only way, scaling a text picture, k i´ll gonna look how i can implement this into my project, thanks for your help :)
You need to login to post a reply.