1 2 3 4 5 6 7 8 9 10 11 12 13
| private static BufferedImage scaleWithBorder(Image source) {
float ratio = Math.min(
(float) (maxWidthElement / source.getWidth(null)),
(float) (maxHeightElement / source.getHeight(null));
if (ratio > 1) ratio = 1;
int newWidthImage = (int) (source.getWidth(null) * ratio);
int newHeightImage = (int) (source.getHeight(null)* ratio);
BufferedImage result = new BufferedImage(newWidthImage,newHeightImage,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = result.createGraphics();
g.fillRect(0, 0, newWidthImage, newHeightImage);
g.drawImage(source,border,border,newWidthImage,newHeightImage,null);
return (BufferedImage) result;
} |
Partager