final float[] result = new float[3];
final FontRenderContext frc = g2.getFontRenderContext();
final Font f = g2.getFont();
final FontMetrics fm = g2.getFontMetrics(f);
final Rectangle2D bounds = TextUtilities.getTextBounds(text, g2, fm);
final LineMetrics metrics = f.getLineMetrics(text, frc);
final float ascent = metrics.getAscent();
result[2] = -ascent;
final float halfAscent = ascent / 2.0f;
final float descent = metrics.getDescent();
final float leading = metrics.getLeading();
float xAdj = 0.0f;
float yAdj = 0.0f;
if (anchor == TextAnchor.TOP_CENTER
|| anchor == TextAnchor.CENTER
|| anchor == TextAnchor.BOTTOM_CENTER
|| anchor == TextAnchor.BASELINE_CENTER
|| anchor == TextAnchor.HALF_ASCENT_CENTER) {
xAdj = (float) -bounds.getWidth() / 2.0f;
}
else if (anchor == TextAnchor.TOP_RIGHT
|| anchor == TextAnchor.CENTER_RIGHT
|| anchor == TextAnchor.BOTTOM_RIGHT
|| anchor == TextAnchor.BASELINE_RIGHT
|| anchor == TextAnchor.HALF_ASCENT_RIGHT) {
xAdj = (float) -bounds.getWidth();
}
if (anchor == TextAnchor.TOP_LEFT
|| anchor == TextAnchor.TOP_CENTER
|| anchor == TextAnchor.TOP_RIGHT) {
yAdj = -descent - leading + (float) bounds.getHeight();
}
else if (anchor == TextAnchor.HALF_ASCENT_LEFT
|| anchor == TextAnchor.HALF_ASCENT_CENTER
|| anchor == TextAnchor.HALF_ASCENT_RIGHT) {
yAdj = halfAscent;
}
else if (anchor == TextAnchor.CENTER_LEFT
|| anchor == TextAnchor.CENTER
|| anchor == TextAnchor.CENTER_RIGHT) {
yAdj = -descent - leading + (float) (bounds.getHeight() / 2.0);
}
else if (anchor == TextAnchor.BASELINE_LEFT
|| anchor == TextAnchor.BASELINE_CENTER
|| anchor == TextAnchor.BASELINE_RIGHT) {
yAdj = 0.0f;
}
else if (anchor == TextAnchor.BOTTOM_LEFT
|| anchor == TextAnchor.BOTTOM_CENTER
|| anchor == TextAnchor.BOTTOM_RIGHT) {
yAdj = -metrics.getDescent() - metrics.getLeading();
}
if (textBounds != null) {
textBounds.setRect(bounds);
}