Java BufferedImage Scale adjustScale(final BufferedImage bi, final JComponent component, final int insetX, final int insetY)

Here you can find the source of adjustScale(final BufferedImage bi, final JComponent component, final int insetX, final int insetY)

Description

DOCUMENT ME!

License

Open Source License

Parameter

Parameter Description
bi DOCUMENT ME!
component DOCUMENT ME!
insetX DOCUMENT ME!
insetY DOCUMENT ME!

Return

DOCUMENT ME!

Declaration

public static Image adjustScale(final BufferedImage bi, final JComponent component, final int insetX,
        final int insetY) 

Method Source Code

//package com.java2s;
/*/*from   www  .  ja  v  a2s.c o m*/
 *  Copyright (C) 2010 srichter
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Image;

import java.awt.image.BufferedImage;
import javax.swing.JComponent;

public class Main {
    /**
     * DOCUMENT ME!
     *
     * @param   bi         DOCUMENT ME!
     * @param   component  DOCUMENT ME!
     * @param   insetX     DOCUMENT ME!
     * @param   insetY     DOCUMENT ME!
     *
     * @return  DOCUMENT ME!
     */
    public static Image adjustScale(final BufferedImage bi, final JComponent component, final int insetX,
            final int insetY) {
        final double scalex = (double) component.getWidth() / bi.getWidth();
        final double scaley = (double) component.getHeight() / bi.getHeight();
        final double scale = Math.min(scalex, scaley);
        if (scale <= 1d) {
            return bi.getScaledInstance((int) (bi.getWidth() * scale) - insetX,
                    (int) (bi.getHeight() * scale) - insetY, Image.SCALE_SMOOTH);
        } else {
            return bi;
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @param   bi       DOCUMENT ME!
     * @param   targetW  DOCUMENT ME!
     * @param   targetH  DOCUMENT ME!
     * @param   insetX   DOCUMENT ME!
     * @param   insetY   DOCUMENT ME!
     *
     * @return  DOCUMENT ME!
     */
    public static Image adjustScale(final BufferedImage bi, final int targetW, final int targetH, final int insetX,
            final int insetY) {
        final double scalex = (double) targetW / bi.getWidth();
        final double scaley = (double) targetH / bi.getHeight();
        final double scale = Math.min(scalex, scaley);
        if (scale <= 1d) {
            return bi.getScaledInstance((int) (bi.getWidth() * scale) - insetX,
                    (int) (bi.getHeight() * scale) - insetY, Image.SCALE_SMOOTH);
        } else {
            return bi;
        }
    }
}

Related

  1. getScaledImageAsImageIcon(BufferedImage bufferedImage, int width, int height)
  2. ScaleToSize(BufferedImage bimg, int maxCols, int maxRows)