}
if (ty > 0) {
ty = 0;
}
final ScrollBar horizontal = getHorizontalBar();
horizontal.setIncrement(getClientArea().width / 100);
horizontal.setPageIncrement(getClientArea().width);
final Rectangle imageBound = sourceImage.getBounds();
final int cw = getClientArea().width, ch = getClientArea().height;
if (imageBound.width * sx > cw) { /* image is wider than client area */
horizontal.setMaximum((int) (imageBound.width * sx));
horizontal.setEnabled(true);
if ((int) -tx > horizontal.getMaximum() - cw) {
tx = -horizontal.getMaximum() + cw;
}
} else { /* image is narrower than client area */
horizontal.setEnabled(false);
tx = (cw - imageBound.width * sx) / 2; // center if too small.
}
horizontal.setSelection((int) -tx);
horizontal.setThumb(getClientArea().width);
final ScrollBar vertical = getVerticalBar();
vertical.setIncrement(getClientArea().height / 100);
vertical.setPageIncrement(getClientArea().height);
if (imageBound.height * sy > ch) { /* image is higher than client area */
vertical.setMaximum((int) (imageBound.height * sy));
vertical.setEnabled(true);
if ((int) -ty > vertical.getMaximum() - ch) {
ty = -vertical.getMaximum() + ch;
}
} else { /* image is less higher than client area */
vertical.setEnabled(false);
ty = (ch - imageBound.height * sy) / 2; // center if too small.
}
vertical.setSelection((int) -ty);
vertical.setThumb(getClientArea().height);
/* update transform. */
af = AffineTransform.getScaleInstance(sx, sy);
af.preConcatenate(AffineTransform.getTranslateInstance(tx, ty));
transform = af;