[java] Text of an arbitrary size.

Started by
5 comments, last by FlaiseSaffron 17 years, 7 months ago
I need to create a Font object that will make a Graphics object display text at a certain size so everything in the window lines up. Is there a conversion formula for a font point size making characters of a certain number of pixels?
Advertisement
Use the FontMetrics class to find out at runtime.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
If you would read my post more closely you would notice that I need to go from pixels to point size, not the other way around.
I did read it carefully and it sounded like you wanted to know how many pixels a point size takes up.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Not quite; I want to know how many point sizes a certain number of pixels takes up. If I have a cell 10x14 pixels, for example, what size font do I paste in there to fill it?
Found this:

http://www.experts-exchange.com/Programming/Game_Development/Game_Graphics/Q_20686438.html

Quote:
A point is 1/72 inch. With that formula you can convert between inches and points. What's missing is the number of pixels in an inch. You can retrieve that by calling GetDeviceCaps( hdc, LOGPIXELSY ) which returns the number of pixels in 1 logical inch of your screen. A logical inch isn't exactly 1 inch if you measure it, but that isn't a problem as the point size only relates to logical inches on your screen.


I think the default setting is 72 dpi. So a 10 point font will be 10 pixels by 10 pixels. However, a proportional font will be a max of 10 pixels wide.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Thanks; that should work.

This topic is closed to new replies.

Advertisement