Size and location of text

J

Jacob

When rendering text I am concerned about its exact
pixel location. Apparently java.awt.font.TextLayout
is the class to use.

Following the example from the javadoc of the class
(the first example in the overview), I still get the
location off a pixel or two in some cases compared to
the expected result. Code:

Graphics2D g = ...;
int x = ...;
int y = ...;

// Render text
g.setColor (Color.BLACK);
Font font = new Font ("Dialog", Font.BOLD, 56);
TextLayout layout = new TextLayout ("d", font,
g.getFontRenderContext());
layout.draw (g, (float) x, (float) y);

// Render glyph box
g.setColor (new Color (1.0f, 0.0f, 0.0f, 0.3f));
Rectangle2D r = layout.getBounds();
r.setRect (r.getX() + x,
r.getY() + y,
r.getWidth(),
r.getHeight());
g.draw (r);


Playing with font size, the glyph is outside the box in
different ways, normally just a single pixel.

What am I doing wrong?

Thanks!
 
L

Larry Barowski

Jacob said:
When rendering text I am concerned about its exact
pixel location. Apparently java.awt.font.TextLayout
is the class to use.

Following the example from the javadoc of the class
(the first example in the overview), I still get the
location off a pixel or two in some cases compared to
the expected result.

See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5012037 .
Apparently an exact bound is not guaranteed. This
is currently undocumented.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top