PDA

View Full Version : drawString(String text, int alignment) in G2OutputTarget is



Anonymous
09-10-2002, 01:38 AM
Hi,

I just tried out JFreeReport076 and found out the alignment calculation in the drawString(String text, int alignment) is incorrect. Here is my correction...

//
public void drawString(String text, int alignment)
{
Rectangle2D bounds = getCursor().getDrawBounds();

float x = (float) bounds.getX();

FontRenderContext frc = g2.getFontRenderContext();
Rectangle2D textBounds = g2.getFont().getStringBounds(text, frc);
float textLength = (float) textBounds.getWidth();
//float elementLength = (float) (bounds.getX() + bounds.getWidth());
float elementLength = (float) bounds.getWidth();
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

GlyphVector gv = g2.getFont().createGlyphVector(frc, text);
FontMetrics fm = g2.getFontMetrics();
float baseline = (float) (bounds.getY() + fm.getMaxAscent());
if (alignment == Element.LEFT)
{
// no adjustment required
}
else if (alignment == Element.CENTER)
{
//x = (float) ((elementLength / 2) - (textLength / 2));
x = (float) (bounds.getX() + ((elementLength / 2) - (textLength / 2)));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
else if (alignment == Element.RIGHT)
{
//x = (float) (elementLength - textLength);
x = (float) (bounds.getX() + elementLength - textLength);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
int display = getFont().canDisplayUpTo(text);
if (display > 0 && display < text.length())
{
Log.warn("Unable to display the string completely. Can display up to " + display + " chars.");
}
g2.drawString(text, x, baseline);
}

//

Anonymous
09-10-2002, 06:53 AM
Hi,

yes you are right. No one has noticed as getX() returns 0 in most cases. Thank you for that fix, I added it to the cvs.
Have more fun,
said Thomas