BufferedImage percent Scale Within Canvas - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Scale

Description

BufferedImage percent Scale Within Canvas

Demo Code


//package com.java2s;

import java.awt.Image;
import java.awt.image.BufferedImage;

public class Main {
    public static BufferedImage percentScaleWithinCanvas(BufferedImage img,
            double percent) {
        int scaleX = (int) (img.getWidth() * percent);
        int scaleY = (int) (img.getHeight() * percent);
        int startX = (int) (img.getWidth() * (1 - percent) / 2);
        int startY = (int) (img.getHeight() * (1 - percent) / 2);

        Image scaled = img.getScaledInstance(scaleX, scaleY,
                Image.SCALE_SMOOTH);
        BufferedImage buffered = new BufferedImage(img.getWidth(),
                img.getHeight(), img.getType());

        buffered.getGraphics().drawImage(scaled, startX, startY, null);
        return buffered;
    }//from  ww w.j  a v  a 2  s.c o m
}

Related Tutorials