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

2014/3/3

Width and Height of a String in pixels.

1
2
Jak Jak

2014/3/3

#
Hello! I would like some help on my game I am developing. I have a Button class which currently has many parameter's so an image and text can be displayed for various parts of the home screen and game world, e.g Start and Instructions. I would like to cut down on the parameters by passing a font size in, cutting down imageWidth, imageHeight etc. I have been hard coding the width and height in the sub-classes to Button however I want to soft code. I essentially want to return the pixel length and pixel height of a given String with a given font. Is there any way I can do this in Greenfoot? Thanks.
danpost danpost

2014/3/3

#
You need to use methods included in the FontMetrics class.
Jak Jak

2014/3/3

#
Could you give me a generic example on a String? I have had multiple errors using FontMetrics, which code I have since deleted.
danpost danpost

2014/3/3

#
I have not had the need to use the class myself; but am aware of its existence and capabilities. Meaning that when the times comes when I do need it, I know where to go. Usually, when I do things in alternate fonts, it is just like on a title screen or something that does not change. So, I create the image elsewhere and store it in my scenario's image folder. Maybe if you tried again, and if you come across some errors, you could post the error and what you tried for help.
Jak Jak

2014/3/3

#
I will go with your idea of just creating images, I can't believe I didn't actually consider that option before! However I am interested if anyone has had success using font metrics in Greenfoot to determine the pixel width and length of a desired font, I would like to see the code.
danpost danpost

2014/3/3

#
You can see a sample of it being used in this discussion.
Jak Jak

2014/3/3

#
Thanks danpost! You and davmac have unknowingly helped me a lot with my Greenfoot issues with your responses to topics like this one.
Pointifix Pointifix

2014/3/3

#
The same question i asked some days before ;) i came to this solution
1
2
private Graphics g = getBackground().getAwtImage().createGraphics();
    private FontMetrics fm = g.getFontMetrics(new Font("MONOSPACED",Font.BOLD,textsize));
Now you can use fm to get a strings height or width like:
1
getBackground().drawString("Your Text",getBackground().getWidth()/2-fm.stringWidth("Your Text")/2,50);
to center your text in the screen as example I hope i could help, but always remember the difference between points and pixel, there you can do big mistakes. greetings Pointifix
Jak Jak

2014/3/4

#
So that code gets point size not pixel size?
danpost danpost

2014/3/4

#
Jak wrote...
So that code gets point size not pixel size?
I think that 'font.getSize()' will return in points. However, the API is not clear as to what 'fm.getHeight()' and 'fm.stringWidth()' return in. From the code Pointifix gave, I will have to presume they return in pixels. I would usually just create the text image in the size font wanted and then get the size of the image:
1
2
3
GreenfootImage image = new GreenfootImage("My text", 20, Color.black, null); // create text image
int imgWidth = image.getWidth(), imgHeight = image.getHeight();
getBackground().drawImage(image, getWidth()/2-imgWidth/2, 50);
Pointifix Pointifix

2014/3/4

#
getWidth() seams to return pixel, cause i always get my text into the center, but i dont know how to cente the height
danpost danpost

2014/3/4

#
Where are you trying to center it? If in the world 'getHeight()/2-imgHeight/2' would be used for the y in the background image of the world.
Pointifix Pointifix

2014/3/4

#
danpost wrote...
Where are you trying to center it? 'getHeight()/2-imgHeight/2'
This does not work i tried, i got it in points so it does not center vertically
danpost danpost

2014/3/4

#
Pointifix wrote...
This does not work i tried, i got it in points so it does not center vertically
Oh, you must not have been referring to the code I gave. You must still be using FontMetrics. That is why is does not work for you. Either that or you are using 'fm.getHeight()' where I was saying to use the World method 'getHeight' to get the vertical dimension of the current world background so the image can be drawn in the vertical center of the world background.
danpost danpost

2014/3/4

#
As an example, start a new scenario and put the following in the world constructor:
1
2
3
4
int fontSize = 64; // try miscellaneous values
String text = "GAME OVER"; // also try "GAME\nOVER" and "GAME\n\nOVER"
GreenfootImage image = new GreenfootImage(text, fontSize, Color.black, null);
getBackground().drawImage(image, fontSize, (getWidth()-image.getWidth())/2, (getHeight()-image.getHeight())/2);
There are more replies on the next page.
1
2