Package com.j3d.utility
Class ImageUtils
java.lang.Object
com.j3d.utility.ImageUtils
Utility class for image manipulation, specifically for cropping and scaling images
to fit specific dimensions while maintaining aspect ratio.
- Author:
- Lehlogonolo Poole
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ImageIconcreateCroppedIcon(File file, int width, int height, int multiplier) Creates anImageIconfrom a given file, cropping and scaling it to fit the specified target width and height, then multiplying its size.static ImageIconcreateCroppedIcon2(File file, int width, int height, int multiplier) Creates anImageIconfrom a given file, cropping and scaling it to fit the specified target width and height, then multiplying its size.
-
Constructor Details
-
ImageUtils
public ImageUtils()
-
-
Method Details
-
createCroppedIcon
Creates anImageIconfrom a given file, cropping and scaling it to fit the specified target width and height, then multiplying its size. The cropping logic prioritizes fitting the target aspect ratio by removing excess from the wider/taller dimension.- Parameters:
file- The image file to process.width- The target width for the cropped image before scaling.height- The target height for the cropped image before scaling.multiplier- A factor by which the cropped image's dimensions (width, height) will be scaled.- Returns:
- An
ImageIconrepresenting the cropped and scaled image. - Throws:
RuntimeException- if an error occurs during image reading or processing.
-
createCroppedIcon2
public static ImageIcon createCroppedIcon2(File file, int width, int height, int multiplier) throws IIOException Creates anImageIconfrom a given file, cropping and scaling it to fit the specified target width and height, then multiplying its size. This method uses a slightly different cropping logic compared tocreateCroppedIcon, ensuring the cropped area always fits within the original image boundaries while trying to match the target aspect ratio.- Parameters:
file- The image file to process.width- The target width for the cropped image before scaling.height- The target height for the cropped image before scaling.multiplier- A factor by which the cropped image's dimensions (width, height) will be scaled.- Returns:
- An
ImageIconrepresenting the cropped and scaled image. - Throws:
IIOException- if an error occurs during image reading (e.g., file not found, invalid format).RuntimeException- if another type of error occurs during image processing.
-