Font sizing

Hi,

I’ve made my own Java2D menus for a game that runs in a resizable window. Sometimes the window is big, sometimes it is small.

How should I set the text/font in a button to be the right size so it scales properly?

I mean, say the button is 1/8 of the window, and the String painted in it is Font size 14 and it fits snuggly in the button. Then the window width and height is doubled (or the game runs on a screen with a higher resolution that’s double as big) and now the string is too small for the button since the button is now double as wide. Should I just scale up the font size by double (14*2)?

What do you guys do about this? I know many people just fix the window size, but I’d like to avoid that.

Thanks :slight_smile:

I think there’s a method that you can use to get the size of the string which then you can use to compare. But I think this might be tricky to get right. Maybe you should leave the font size and then have an option for the user to change it?

I think the method you’re thinking of is FontMetrics.stringWidth(String). (http://java.sun.com/javase/6/docs/api/java/awt/FontMetrics.html)

This is what I would like to do: Since I know the game is well-proportioned with my computer’s screen resolution with my selected window size with font size 12, when I run the game on another computer with a resolution or screen size that’s double, I just scale the font size by double. Does anyone do this?

From the API docs: http://java.sun.com/javase/6/docs/api/java/awt/Font.html#getSize()

The Java(tm)2D API adopts the convention that one point is equivalent to one unit in user coordinates

So, it seems like one a font size of 2 should be double the height of a font with size 1, so scaling the font size should work. I just wanted to know if this is a good idea and if anyone does this.

thanks!

Every time I try to mess with fonts in a pretty way it doesn’t work. I don’t think doubling the font size will be surefire across all applications, but you may as well try. The reason to use FontMetrics is so you can get an exact measurement. You can always loop through like 100 font sizes until one fits the proper dimensions.

Yea, I have never seen a Java program that auto changes it font size depending on screen size. But you could just compare you text width and height and if it doesn’t fit then do the font smaller. Or what many programs and some game have is a option to select the font size.

I tried using a resizable window for a game once but it got really annoying the further I got into the game. I personally think you should just fix the window size but thats me.

Depending on the OS your app is running on, you may run into this:
http://java.sun.com/products/java-media/2D/reference/faqs/index.html#Q_Why_does_eg_a_10_pt_font_in_Ja

This site offers a solution:
http://www.3rd-evolution.de/tkrammer/docs/java_font_size.html

As for scaling your Font to match the window and screen, FontMetrics should work.
Experimentation is going to give you the best idea of what will work best.

If it’s just a few buttons you could use several images for the text, one for each common screen size…
Not great but it works!

Thanks for the pointers guys.

I’ll go with the hit-and-miss/experimentation approach you suggested, to just dumbly scale up the font size and do checks on it using FontMetrics to make corrections.

I’ll let you know how it goes.

Thanks :slight_smile:
Keith