Here you can find the source of getScaledImage(ImageIcon srcImg, int h, int maxWidth)
Parameter | Description |
---|---|
srcImg | source image icon |
h | height of the image |
maxWidth | a parameter |
public static ImageIcon getScaledImage(ImageIcon srcImg, int h, int maxWidth)
//package com.java2s; /*//from w w w .jav a2 s. com * PS3 Media Server, for streaming any medias to your PS3. * Copyright (C) 2012 Ph.Waeber * * 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; version 2 * of the License only. * * 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, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import javax.swing.ImageIcon; public class Main { /** * Method used to resize images to fit the cover into the label * @param srcImg source image icon * @param h height of the image * @param maxWidth * @return resized image icon */ public static ImageIcon getScaledImage(ImageIcon srcImg, int h, int maxWidth) { int w = srcImg.getIconWidth() * h / srcImg.getIconHeight(); //respect the max width constraint if (w > maxWidth) { w = maxWidth; h = srcImg.getIconHeight() * w / srcImg.getIconWidth(); } //don't make the image bigger then its original size if (w > srcImg.getIconWidth() || h > srcImg.getIconHeight()) { w = srcImg.getIconWidth(); h = srcImg.getIconHeight(); } BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = resizedImg.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(srcImg.getImage(), 0, 0, w, h, null); g2.dispose(); return new ImageIcon(resizedImg); } }