Here you can find the source of rescale(ImageIcon src, Dimension newMinSize, ImageObserver observer)
Parameter | Description |
---|---|
src | the original icon. |
newMinSize | the minimum size of the new icon. The width and height of the returned icon will be at least the width and height respectively of newMinSize. |
an | ImageObserver. |
public static ImageIcon rescale(ImageIcon src, Dimension newMinSize, ImageObserver observer)
//package com.java2s; /******************************************************************************* * Copyright 2012-2014 Esri//from w w w . j a va 2 s. com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.awt.image.ImageObserver; import javax.swing.ImageIcon; public class Main { /** * Rescales an icon. * @param src the original icon. * @param newMinSize the minimum size of the new icon. The width and height of * the returned icon will be at least the width and height * respectively of newMinSize. * @param an ImageObserver. * @return the rescaled icon. */ public static ImageIcon rescale(ImageIcon src, Dimension newMinSize, ImageObserver observer) { double widthRatio = newMinSize.getWidth() / (double) src.getIconWidth(); double heightRatio = newMinSize.getHeight() / (double) src.getIconHeight(); double scale = widthRatio > heightRatio ? widthRatio : heightRatio; int w = (int) Math.round(scale * src.getIconWidth()); int h = (int) Math.round(scale * src.getIconHeight()); BufferedImage dst = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = dst.createGraphics(); g2.drawImage(src.getImage(), 0, 0, w, h, observer); g2.dispose(); return new ImageIcon(dst); } }