Java Image Scale scaleImage(Image image, float widthPercentage, float heightPercentage)

Here you can find the source of scaleImage(Image image, float widthPercentage, float heightPercentage)

Description

scale Image

License

Open Source License

Declaration

public static Image scaleImage(Image image, float widthPercentage,
            float heightPercentage) 

Method Source Code

//package com.java2s;
import java.awt.Dimension;
import java.awt.Image;
import javax.swing.JPanel;

public class Main {
    private static final JPanel IMAGE_OBSERVER = new JPanel();
    public static int SCALE_METHOD = Image.SCALE_DEFAULT;

    public static Image scaleImage(Image image, int width, int height) {
        return scaleImage(image, width, height, false);
    }/*from  www  .  jav a 2  s.c o  m*/

    public static Image scaleImage(Image image, int width, int height,
            boolean keepAspectRatio) {
        if (keepAspectRatio) {
            Dimension originalImageSize = new Dimension(
                    image.getWidth(IMAGE_OBSERVER),
                    image.getHeight(IMAGE_OBSERVER));
            float widthPercentage = (float) width / originalImageSize.width;
            float heightPercentage = (float) height
                    / originalImageSize.height;
            float scalePercentage = Math.min(widthPercentage,
                    heightPercentage);

            return scaleImage(image, scalePercentage, scalePercentage);
        } else {
            return image.getScaledInstance(width, height, SCALE_METHOD);
        }
    }

    public static Image scaleImage(Image image, float widthPercentage,
            float heightPercentage) {
        Dimension imageSize = new Dimension(image.getWidth(IMAGE_OBSERVER),
                image.getHeight(IMAGE_OBSERVER));
        imageSize.width *= widthPercentage;
        imageSize.height *= heightPercentage;

        Image scaledImage = image.getScaledInstance(imageSize.width,
                imageSize.height, SCALE_METHOD);
        return scaledImage;
    }
}

Related

  1. getScaledImage(Image srcImg, int w, int h)
  2. getScaledImage(ImageIcon icon, int w, int h)
  3. getScaledImage(ImageIcon srcImg, int h, int maxWidth)
  4. getScaleToFit(JPanel componentToFit, File imageFile)
  5. scaleImage(Image image, float scale)
  6. scaleImage(Image sourceImage, int width, int height)
  7. scaleImage(Image src, int width, int height)
  8. ScaleToSize(Image srcImg, int w, int h)