Centering Text?

Discussion in 'Java' started by Chris Rehm, Jul 5, 2003.

  1. Chris Rehm

    Chris Rehm Guest

    I am using 1.4.2 on Windows XP.

    I print various geometric shapes on a page and I want to number them,
    printing the numbers centered in the middle of the shape.

    I've tried
    //fm is the FontMetrics for the current font.
    float cx = dl.getLocationX(); // Current coordinates
    float cy = dl.getLocationY();

    cy = cy + ((float) fm.getAscent() / 2);
    cx = cx - ((float) fm.stringWidth(numberString) / 2);

    g2.drawString(numberString, cx, cy);

    and
    Rectangle2D r = fm.getStringBounds(numberString,g2);

    float cx = dl.getLocationX();
    float cy = dl.getLocationY();

    cy = (float)(r.getHeight()/2) + cy;
    cx = cx - (float)(r.getWidth()/2);

    g2.drawString(numberString, cx, cy);

    but in both cases my text is moved too far left and too far down.

    The rest of the graphics I'm printing look great, I'd love to get this
    fixed. What am I missing in FontMetrics?

    --
    Chris Rehm


    Thou shalt not avenge, nor bear any grudge against the children of thy
    people, but shalt love thy neighbour as thyself. [Lev. 19:18]
    Chris Rehm, Jul 5, 2003
    #1
    1. Advertising

  2. Chris Rehm

    ZB Guest

    Try with :

    float cx = (float) dl.getBounds2D.getCenterX();
    float cy = (float) dl.getBounds2D.getCenterY();

    or something like that (without getBounds2D). Anyway, what type is variable
    dl ?

    Regards, Zoran.

    ----
    Serbs, stronger than ALL
    ....
    in Basketball !



    "Chris Rehm" <> wrote in message
    news:be726l$ldk$...
    > I am using 1.4.2 on Windows XP.
    >
    > I print various geometric shapes on a page and I want to number them,
    > printing the numbers centered in the middle of the shape.
    >
    > I've tried
    > //fm is the FontMetrics for the current font.
    > float cx = dl.getLocationX(); // Current coordinates
    > float cy = dl.getLocationY();
    >
    > cy = cy + ((float) fm.getAscent() / 2);
    > cx = cx - ((float) fm.stringWidth(numberString) / 2);
    >
    > g2.drawString(numberString, cx, cy);
    >
    > and
    > Rectangle2D r = fm.getStringBounds(numberString,g2);
    >
    > float cx = dl.getLocationX();
    > float cy = dl.getLocationY();
    >
    > cy = (float)(r.getHeight()/2) + cy;
    > cx = cx - (float)(r.getWidth()/2);
    >
    > g2.drawString(numberString, cx, cy);
    >
    > but in both cases my text is moved too far left and too far down.
    >
    > The rest of the graphics I'm printing look great, I'd love to get this
    > fixed. What am I missing in FontMetrics?
    >
    > --
    > Chris Rehm
    >
    >
    > Thou shalt not avenge, nor bear any grudge against the children of thy
    > people, but shalt love thy neighbour as thyself. [Lev. 19:18]
    >
    ZB, Jul 6, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry Camel

    Centering an image with text

    Jerry Camel, Sep 23, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    514
    John Timney \(ASP.NET MVP\)
    Sep 23, 2005
  2. FH

    centering gif and text

    FH, Mar 3, 2004, in forum: HTML
    Replies:
    7
    Views:
    506
  3. ppcguy
    Replies:
    7
    Views:
    744
    Toby Inkster
    Aug 26, 2005
  4. Replies:
    3
    Views:
    4,967
  5. Joe Delphi

    Centering Label Text

    Joe Delphi, May 16, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    5,497
    John Timney \( MVP \)
    May 16, 2006
Loading...

Share This Page