By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 1,944 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

Text width

P: n/a
Hello,

I need to know how to get get the width of a string without having a
java.awt.Graphics around (otherwise i'd just
graphics.getFontMetrics().stringWidth("Hello I'm a string") )

The thing is that I have I function that returns an Image with the
text, and I want to create the Image inside the function. The size of
the Image is determined by how big the text is, but to get the size of
the text I need to have an Image, and to get the Image I need to know
the size of the text. So I'm in a catch-22 hell and I don't know how
to get out (I suppose I could create a temporary Image and use that
for determining the size and then discard it and create another Image
with the correct size but there has to be a better way.)

Any help appriciated
Oskar Sigvardsson
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Oskar Sigvardsson wrote:
Hello,

I need to know how to get get the width of a string without having a
java.awt.Graphics around (otherwise i'd just
graphics.getFontMetrics().stringWidth("Hello I'm a string") )

The thing is that I have I function that returns an Image with the
text, and I want to create the Image inside the function. The size
of the Image is determined by how big the text is, but to get the
size of the text I need to have an Image, and to get the Image I
need to know the size of the text. So I'm in a catch-22 hell and I
don't know how to get out (I suppose I could create a temporary
Image and use that for determining the size and then discard it and
create another Image with the correct size but there has to be a
better way.)

Any help appriciated
Oskar Sigvardsson


Hi,
I'm really not about to figure out how this works, but a good place to
start looks like java.awt.Font.getStringBounds(), with an
appropriately prepared FontRenderContext designed to simulate the
Image you'll eventually be creating. That'll give you the size you
need.

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQFAP+0pnwjA8LryK2IRAntmAJ95U1kqoC5yTl9BH91Xjd xVDOn3dwCgmjYy
WB85ddQUAqdjU+RdHhEe8OU=
=HesH
-----END PGP SIGNATURE-----
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.