Java ImageIcon Scale getScaledImageIcon(final String location, final int width, final int height)

Here you can find the source of getScaledImageIcon(final String location, final int width, final int height)

Description

Get an image as a resource and create an ImageIcon

License

Apache License

Parameter

Parameter Description
location The image icon location
width The scaled width
height The scaled height

Return

The corresponding ImageIcon loaded from the location and scaled

Declaration

public static ImageIcon getScaledImageIcon(final String location, final int width, final int height) 

Method Source Code


//package com.java2s;
/*/*from w  ww  .j  av a  2 s . co  m*/
 * Copyright 2008 the original author or authors.
 *
 * 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 javax.swing.*;
import java.awt.*;

import java.net.URL;

public class Main {
    /**
     * Get an image as a resource and create an ImageIcon
     *
     * @param location The image icon location
     * @param width The scaled width
     * @param height The scaled height
     *
     * @return The corresponding ImageIcon loaded from the location and scaled
     */
    public static ImageIcon getScaledImageIcon(final String location, final int width, final int height) {
        ImageIcon icon = null;
        URL url = Thread.currentThread().getContextClassLoader().getResource(location);
        if (url != null) {
            icon = new ImageIcon(url);
            icon = new ImageIcon(icon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH));
        }
        return (icon);
    }
}

Related

  1. getScaledImageIconHeight(ImageIcon ii, int h, boolean incr)
  2. rescale(ImageIcon src, Dimension newMinSize, ImageObserver observer)
  3. rescaleImageIcon(ImageIcon image, int size)
  4. scale(ImageIcon icon, int maxWidth, int maxHeight)