Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.awt.Image;

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

import javax.swing.JComponent;

public class Main {
    /**
     * If icon is an instance of {@link ImageIcon}, 
     * returns an icon scaled to the height of the component.
     * Else return the icon given.
     */
    public static Icon scaleToHeight(JComponent c, Icon icon) {

        Icon sizedIcon = icon;
        if (icon instanceof ImageIcon) {
            int h = c.getPreferredSize().height - c.getInsets().top - c.getInsets().bottom;
            sizedIcon = new ImageIcon(((ImageIcon) icon).getImage().getScaledInstance(-1, h, Image.SCALE_SMOOTH));
        }
        return sizedIcon;
    }
}