public static final CENTER_OFFSET
Indicates the baseline remains a fixed distance from
the center of the component. That is, for any height H the
difference between getBaseline(w, H) and
H / 2 is the same (plus or minus one depending upon
rounding error).
Because of possible rounding errors it is recommended
you ask for the baseline with two consecutive heights and use
the return value to determine if you need to pad calculations
by 1. The following shows how to calculate the baseline for
any height:
Dimension preferredSize = component.getPreferredSize();
int baseline = getBaseline(preferredSize.width,
preferredSize.height);
int nextBaseline = getBaseline(preferredSize.width,
preferredSize.height + 1);
// Amount to add to height when calculating where baseline
// lands for a particular height:
int padding = 0;
// Where the baseline is relative to the mid point
int baselineOffset = baseline - height / 2;
if (preferredSize.height % 2 == 0 &&
baseline != nextBaseline) {
padding = 1;
}
else if (preferredSize.height % 2 == 1 &&
baseline == nextBaseline) {
baselineOffset--;
padding = 1;
}
// The following calculates where the baseline lands for
// the height z:
int calculatedBaseline = (z + padding) / 2 + baselineOffset;