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!
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!