Java Swing Icon createChannelIcon(Icon ic)

Here you can find the source of createChannelIcon(Icon ic)

Description

Creates a scaled Version of the Icon.

License

Open Source License

Parameter

Parameter Description
ic a parameter

Return

ImageIcon

Declaration

public static ImageIcon createChannelIcon(Icon ic) 

Method Source Code


//package com.java2s;
/*/*from  w  ww .j  a v  a2s  .  c om*/
 * TV-Browser
 * Copyright (C) 04-2003 Martin Oberhauser (martin_oat@yahoo.de)
 *
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * CVS information:
 *  $RCSfile$
 *   $Source$
 *     $Date: 2010-11-14 17:44:44 +0100 (Sun, 14 Nov 2010) $
 *   $Author: bananeweizen $
 * $Revision: 6823 $
 */

import java.awt.Color;

import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.RenderingHints;

import java.awt.Transparency;

import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

import javax.swing.Icon;
import javax.swing.ImageIcon;

public class Main {
    /**
     * Creates a scaled Version of the Icon.
     *
     * The scaled Version will have a Background and a Border.
     *
     * @param ic
     * @return ImageIcon
     * @since 2.1
     */
    public static ImageIcon createChannelIcon(Icon ic) {
        BufferedImage img = new BufferedImage(42, 22, BufferedImage.TYPE_INT_RGB);

        if (ic == null) {
            ic = new ImageIcon("./imgs/tvbrowser16.png");
        }

        int height = 20;
        int width = 40;

        if ((ic.getIconWidth() != 0) && (ic.getIconHeight() != 0)) {
            double iWidth = ic.getIconWidth();
            double iHeight = ic.getIconHeight();
            if (iWidth / iHeight < 2.0) {
                width = (int) (iWidth * (20.0 / iHeight));
            } else {
                height = (int) (iHeight * (40.0 / iWidth));
            }
        }
        ic = scaleIcon(ic, width, height);

        Graphics2D g = img.createGraphics();

        g.setColor(Color.WHITE);
        g.fillRect(1, 1, 40, 20);

        int x = 1 + 20 - ic.getIconWidth() / 2;
        int y = 1 + 10 - ic.getIconHeight() / 2;

        ic.paintIcon(null, g, x, y);

        g.setColor(Color.BLACK);
        g.drawRect(0, 0, 42, 22);

        return new ImageIcon(img);
    }

    /**
     * Scale Icons to a specific width. The aspect ratio is kept.
     *
     * @param icon
     *          The icon to scale.
     * @param newWidth
     *          The new width of the icon.
     * @return The scaled Icon.
     */
    public static Icon scaleIcon(Icon icon, int newWidth) {
        if (icon == null) {
            return null;
        }
        return scaleIcon(icon, newWidth, (int) ((newWidth / (float) icon.getIconWidth()) * icon.getIconHeight()));
    }

    /**
     * Scales Icons to a specific size
     *
     * @param icon
     *          Icon that should be scaled
     * @param width
     *          scaled width
     * @param height
     *          scaled height
     * @return Scaled Icon
     */
    public static Icon scaleIcon(Icon icon, int width, int height) {
        if (icon == null) {
            return null;
        }
        int currentWidth = icon.getIconWidth();
        int currentHeight = icon.getIconHeight();
        if ((currentWidth == width) && (currentHeight == height)) {
            return icon;
        }
        try {
            // Create Image with Icon
            BufferedImage iconImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
                    BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = iconImage.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            AffineTransform z = g2.getTransform();
            g2.setTransform(z);
            icon.paintIcon(null, g2, 0, 0);
            g2.dispose();
            BufferedImage scaled = scaleDown(iconImage, width, height);
            // Return new Icon
            return new ImageIcon(scaled);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return icon;
    }

    /**
     * Convenience method that returns a scaled instance of the
     * provided {@code BufferedImage}.
     *
     * @param img the original image to be scaled
     * @param targetWidth the desired width of the scaled instance,
     *    in pixels
     * @param targetHeight the desired height of the scaled instance,
     *    in pixels
     * @return a scaled version of the original {@code BufferedImage}
     */
    public static BufferedImage scaleDown(final BufferedImage img, final int targetWidth, final int targetHeight) {
        if (targetWidth > img.getWidth() || targetHeight > img.getHeight()) {
            return scaleIconToBufferedImage(img, targetWidth, targetHeight);
        }

        int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
                : BufferedImage.TYPE_INT_ARGB;
        BufferedImage result = img;
        int w = img.getWidth();
        int h = img.getHeight();

        do {
            w /= 2;
            if (w < targetWidth) {
                w = targetWidth;
            }
            h /= 2;
            if (h < targetHeight) {
                h = targetHeight;
            }

            BufferedImage tmp = new BufferedImage(w, h, type);
            Graphics2D g2 = tmp.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2.drawImage(result, 0, 0, w, h, null);
            g2.dispose();

            result = tmp;
        } while (w != targetWidth || h != targetHeight);

        return result;
    }

    /**
     * Scales an image to a specific size and returns an BufferedImage
     *
     * @param img
     *          Scale this IMage
     * @param width
     *          new width
     * @param height
     *          new height
     * @return Scaled BufferedImage
     *
     * @since 2.5
     */
    public static BufferedImage scaleIconToBufferedImage(BufferedImage img, int width, int height) {
        return scaleIconToBufferedImage(img, width, height, img.getType());
    }

    /**
     * Scales an image to a specific size and returns an BufferedImage
     *
     * @param img
     *          Scale this image
     * @param width
     *          new width
     * @param height
     *          new height
     * @param type The type of the image.
     * @return Scaled BufferedImage
     *
     * @since 2.7
     */
    public static BufferedImage scaleIconToBufferedImage(BufferedImage img, int width, int height, int type) {
        return scaleIconToBufferedImage(img, width, height, type, null);
    }

    /**
     * Scales an image to a specific size and returns an BufferedImage
     *
     * @param img
     *          Scale this image
     * @param width
     *          new width
     * @param height
     *          new height
     * @param type The type of the image.
     * @return Scaled BufferedImage
     *
     * @since 3.0
     */
    public static BufferedImage scaleIconToBufferedImage(BufferedImage img, int width, int height, int type,
            Color backgroundColor) {
        // Scale Image
        Image image = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);

        BufferedImage im = new BufferedImage(width, height, type);

        Graphics2D g2 = im.createGraphics();
        if (backgroundColor != null) {
            g2.setColor(backgroundColor);
            g2.fillRect(0, 0, width, height);
        }

        g2.drawImage(image, null, null);
        g2.dispose();

        im.flush();
        return im;

    }
}

Related

  1. changeBrightness(Icon icon, float factor)
  2. clearMergedIconsCache()
  3. combineIcons(Icon underIcon, Icon overIcon)
  4. concatenateIcons(final Icon icon1, final Icon icon2)
  5. createBitmap(Icon icon)
  6. createColourIcon(int colour)
  7. createGrayIcon(Icon icon)
  8. createGrayIcon(Icon icon)
  9. createIcon(java.awt.Component component)