Text width

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Oskar Sigvardsson

    Text width

    Hello,

    I need to know how to get get the width of a string without having a
    java.awt.Graphi cs around (otherwise i'd just
    graphics.getFon tMetrics().stri ngWidth("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
  • Chris

    #2
    Re: Text width

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Oskar Sigvardsson wrote:
    [color=blue]
    > Hello,
    >
    > I need to know how to get get the width of a string without having a
    > java.awt.Graphi cs around (otherwise i'd just
    > graphics.getFon tMetrics().stri ngWidth("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[/color]

    Hi,
    I'm really not about to figure out how this works, but a good place to
    start looks like java.awt.Font.g etStringBounds( ), with an
    appropriately prepared FontRenderConte xt 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+0pnwj A8LryK2IRAntmAJ 95U1kqoC5yTl9BH 91XjdxVDOn3dwCg mjYy
    WB85ddQUAqdjU+R dHhEe8OU=
    =HesH
    -----END PGP SIGNATURE-----

    Comment

    Working...