Class ImageUtils

java.lang.Object
com.j3d.utility.ImageUtils

public class ImageUtils extends Object
Utility class for image manipulation, specifically for cropping and scaling images to fit specific dimensions while maintaining aspect ratio.
Author:
Lehlogonolo Poole
  • Constructor Details

    • ImageUtils

      public ImageUtils()
  • Method Details

    • createCroppedIcon

      public static ImageIcon createCroppedIcon(File file, int width, int height, int multiplier)
      Creates an ImageIcon from 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 ImageIcon representing 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 an ImageIcon from 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 to createCroppedIcon, 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 ImageIcon representing 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.