boolean (Image img, int x, int y, int width, int height, ImageObserver observer) { int maxWidth = Math.max(0, targetRect.width - 2 * (x - targetRect.x)); // assuming left and right insets are the same int maxHeight = Math.max(0, targetRect.height - 2 * (y - targetRect.y)); // assuming top and bottom insets are the same if (width > maxWidth) { height = height * maxWidth / width; width = maxWidth; } if (height > maxHeight) { width = width * maxHeight / height; height = maxHeight; } return super.drawImage(img, x, y, width, height, observer); }