Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  calculate FontSize  (Read 1536 times)
0 Members and 1 Guest are viewing this topic.
Offline JavaFreak

Junior Newbie





« Posted 2010-06-27 19:16:49 »

Hello Java Fans,
my question is, if there is an method or something equals, to calculate a FontSize to draw a text which must fit to a given Width or Height?

For example, if we have a area which is 200px width and we have a text like "This is a text" which we will draw with the font (yx) and these text should fit in the area with 200px width.

I haven't found something in FontMetrics but maybe there a other solutions?
The first witch i think is the bisection method to set a Size and get the Width witch FontMetrics' getStringWidth..., but i think this is very inefficient.


thx and sorry for my bad English
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2010-06-27 20:13:58 »

The problem here is that scaling a font in points doesn't necessarily equal scaling it by the same factor in pixels. Maybe when printing, but definitely not on a screen. So there is no real solution other than to converge to a solution using FontMetrics.stringWidth

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline JavaFreak

Junior Newbie





« Reply #2 - Posted 2010-06-27 22:11:55 »

thx for fast reply.
i programm the method now with the bisektion method. I know this is not the best solution but thats why i asks first ^^.
here is the code (not efficient if using a loop!!!!!).
if other ideas...tell me ^^
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
private void getFontSize(Graphics g,int xres,float size){
        boolean ende=false;
        float calcSize=32;
        FontMetrics metrics;
        do{
            font=font.deriveFont(calcSize);
            metrics = g.getFontMetrics(font);
            if((metrics.stringWidth(Text)>xres*(size-0.01f))&&(metrics.stringWidth(Text)<xres*(size+0.01f))){
                ende=true;
            }else{
                if(metrics.stringWidth(Text)>xres*size){
                    calcSize=calcSize-calcSize/2;
                }else{
                    calcSize=calcSize+calcSize/2;
                }
            }
        }while(ende==false);
    }


Font font and
String Text are global
size is the "percentually" size from xres to fits the Text
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2010-06-27 23:40:20 »

You are calling stringWidth three times, on the same font-metrics object, with the same parameters. Simply store the result in a variable.

Further, don't use the 'ende' variable. The line 'ende=true;' should be 'break;'

Also, you might get stuck in an infinite loop, as the string width might never be between xres*(size-0.01) and +0.01. So if stringWidth returns exactly the same value as the previous call, you can break from the loop.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline appel

JGO Wizard


Medals: 51
Projects: 4


I always win!


« Reply #4 - Posted 2010-06-28 12:27:18 »

I do something like:

1  
2  
3  
FontMetrics fontMetrics = g.getFontMetrics(); // of the currently set graphics font.
int width = fontMetrics.stringWidth("hello world");
int height = fontMetrics.getHeight();



Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2010-06-28 16:31:46 »

I do something like:

1  
2  
3  
FontMetrics fontMetrics = g.getFontMetrics(); // of the currently set graphics font.
int width = fontMetrics.stringWidth("hello world");
int height = fontMetrics.getHeight();




Cool, and how does that answer the question?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline appel

JGO Wizard


Medals: 51
Projects: 4


I always win!


« Reply #6 - Posted 2010-06-28 18:21:58 »

Cool, and how does that answer the question?
Grin Don't you just love it when people don't read the question, and just assume something. Alright, here's an idea:

You know the targetWidth and targetHeight. You also know the textWidth and textHeight. From it you could calculate some scaling factor for a new font instance.

getStringBounds and dervieFont are good methods for doing this stuff.

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2010-06-28 20:14:02 »

Grin Don't you just love it when people don't read the question, and just assume something. Alright, here's an idea:

You know the targetWidth and targetHeight. You also know the textWidth and textHeight. From it you could calculate some scaling factor for a new font instance.

getStringBounds and dervieFont are good methods for doing this stuff.

The thing is, as explained in my first reply (... Smiley) that scaling in font-size is not the same as scaling in pixel-size. Hence you can make some rough guesses, but the last few steps must be calculated through a 'binary search'.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline appel

JGO Wizard


Medals: 51
Projects: 4


I always win!


« Reply #8 - Posted 2010-06-29 11:29:00 »

The thing is, as explained in my first reply (... Smiley) that scaling in font-size is not the same as scaling in pixel-size. Hence you can make some rough guesses, but the last few steps must be calculated through a 'binary search'.

I wonder if this will scale in pixel-size.
Quote
Font    deriveFont(AffineTransform trans)
          Creates a new Font object by replicating the current Font object and applying a new transform to it.

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2010-06-29 12:50:31 »

I wonder if this will scale in pixel-size.

Depends on whether you enabled anti-aliasing for text. And even if you do, I think it's highly unlikely that the characters themselves that at floating point offsets in the 'regular range' (8-12pt).

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (44 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (32 views)
2014-09-21 01:30:30

moogie (40 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!